creating a web serviceksuweb.kennesaw.edu/.../webservicelab.doc · web viewwriting a web service in...

13
CREATING A .NET WEB SERVICE CONSUMED BY ASP CLIENT Set up IIS 1. Go to http://learn.iis.net/page.aspx/28/installing-iis-7- on-windows-vista-and-windows-7/ and learn how to install IIS(The IIS features should include all about .NET and ASP) Open Start->Settings->Control Panel->Administrative tools->Internet Services Manager. We will publish this Web Service on IIS running on a local machine. Let's start with configuring the IIS. The address http://localhost will show the IIS welcome information if the setting is correct. If you have set the xampp, tomcat, skype, dropbox or some other softwares before, there will be a port conflict error. The default port of IIS Default Web Site is 80. Here, you have at least two options: Option 1: Stop all the services which is occupying port 80(you may need to do it every time when you power on the computer) Option 2: Modify Default Web Site to other ports: Right click the Default Web Site-> Edit Bindings-> edit the port of http 80 to other numbers like 8088, but that means when entering a website in the Default Web Site, you need to always take the localhost as http://localhost:8088 , There are a lot of ways to fix the port conflict. You can find a way from the internet by yourself. Here we just use the default port 80.

Upload: others

Post on 02-Nov-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

CREATING A .NET WEB SERVICE CONSUMED BY ASP CLIENT Set up IIS1. Go to http://learn.iis.net/page.aspx/28/installing-iis-7-on-windows-vista-and-windows-7/ and learn how to install IIS(The IIS features should include all about .NET and ASP)

Open Start->Settings->Control Panel->Administrative tools->Internet Services Manager.

We will publish this Web Service on IIS running on a local machine. Let's start with configuring the IIS. The address http://localhost will show the IIS welcome information if the setting is correct. If you have set the xampp, tomcat, skype, dropbox or some other softwares before, there will be a port conflict error. The default port of IIS Default Web Site is 80. Here, you have at least two options:Option 1: Stop all the services which is occupying port 80(you may need to do it every time when you power on the computer) Option 2: Modify Default Web Site to other ports: Right click the Default Web Site-> Edit Bindings-> edit the port of http 80 to other numbers like 8088, but that means when entering a website in the Default Web Site, you need to always take the localhost as http://localhost:8088,

There are a lot of ways to fix the port conflict. You can find a way from the internet by yourself. Here we just use the default port 80.

Expand and right-click on [Default Web Site]; select Add Virtual Directory.

Page 2: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

The "Virtual Directory Alias" screen opens. Type the virtual directory name—for example, MyWebServices—and enter the Physical path like “c:\MyWebServices”. and click OK.

Writing a Web Service in .Net

Page 3: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

from visual studio: Choose ->File->New Web Site

Page 4: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

Choose .NET Framework 4->ASP .NET Web Site, locate it “c:\MyWebServices\WebService”->click OK.

Right click on the project in the solution explorer-> add new item

Page 5: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

Choose Web Service, name it Web Service .asmx

Add more WebMethod functions into the “WebService.cs” file(Note that every function has a head of [WebMethod], which means it can be used by web) [WebMethod] public int Add(int a, int b) { return a + b; }[WebMethod] public int sub(int a, int b)

Page 6: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

{ return a - b; }[WebMethod] public int mul(int a, int b) { return a * b; }[WebMethod] public int div(int a, int b) { return a / b; }Save and debug the Web Site in Visual Studio by pressing “F5”It will show like this

Skip rest of the lab

Now, it is only running in visual studio’s server, let’s stop visual studio and run it in IIS.Here is the step to setup a website in the IIS:First, go to IIS, you can see a folder in “Default Web Site”-“MyWebServices”-“WebService”(you can try refresh if it can’t be seen)Second, right click it and choose “Convert to Application”, click OKThird, go to “Application Pools” , check the “Basic Setting” of “Default Web Site” to “.NET 4.0”

Page 7: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

Now open the browser and browse this addresshttp://localhost/MyWebServices/WebService/WebService.asmxIt will show the exactly same as it shown in visual studio.

After building the Web Service on IIS, we can start a Consumer’s website to remotely call the [WebMethod].

Web-Based Service ConsumerUse Visual Studio to create a new website, name it “WebClient”:

Page 8: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

Add a new Item, choose “Web Form(Visual C#)” and name it “Client”

Right click on the website in the Solution Explorer, click “Add Web Reference”, enter the service side address click the green button on the right of the address, it will show the functions in the service site we can call and use. Name the web reference “ServiceReference”

Page 9: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

Click the triangle before “Service Reference” and “WebService.discomap”. You can see the wsdl proxy is already automatically generated by Visual Studio, it helps the client side to use the function from service side.

Go back to Client.aspx, there is a file named Client.aspx.cs under it, add a using statement in that file “using ServiceReference;”Drag Two Textbox, one button, and several Label on the “Client.aspx” and do the

Page 10: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

following layout.

Double click on the Button, copy and paste the following code into the function “Button1_Click1”

ServiceReference.WebService mySvc = new WebService(); Label1.Text = mySvc.Add(Int32.Parse(TextBox1.Text), Int32.Parse(TextBox2.Text)).ToString(); Label2.Text = mySvc.sub(Int32.Parse(TextBox1.Text), Int32.Parse(TextBox2.Text)).ToString(); Label3.Text = mySvc.mul(Int32.Parse(TextBox1.Text), Int32.Parse(TextBox2.Text)).ToString(); Label4.Text = mySvc.div(Int32.Parse(TextBox1.Text), Int32.Parse(TextBox2.Text)).ToString();

Page 11: Creating a Web Serviceksuweb.kennesaw.edu/.../WebServiceLab.doc · Web viewWriting a Web Service in .Net from visual studio: Choose-> File->New Web Site Choose .NET Framework 4->ASP

Press “F5” to debug it, the result runs like this:

Then stop it and test it in the IIS, you also need to convert the WebClient to ApplicationFirst, go to IIS, you can see a folder in “Default Web Site”-“MyWebServices”-“WebClient”(you can try refresh if it can’t be seen)Second, right click it and choose “Convert to Application”, click OKThen you can use browser to see http://localhost/MyWebServices/WebClient/Client.aspxAnd test the function.