UNIT -6
Developing Web Services
Using ASP.Net application using c# programming environment.
Example : How to write simple Web service named Math Service that exposes methods for adding, subtracting, dividing, and multiplying two numbers.
Requirements:
The recommended hardware, software, network infrastructure, skills , and service package are:
- Micros oft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
- Microsoft Internet Information Server 4.0 or Internet Information Server 5.0
- Microsoft Visual Studio .NET
Write a Simple .asmx Web Service
- Open Visual Studio .NET.
- On the File menu, click New and then click Project.
- Under Project types click Visual C# Projects, then click ASP.NET
- Web Service under Templates.
- Type MathService in the Location text box to change the default name (WebService1) to MathService.
- Change the name of the default Web service that is created from Service1.asmx to MathService.asmx.
- Click Click here to switch to code view in the designer environment to switch to code view.
- Define methods that encapsulate the functionality of your service.
- Each method that will be exposed from the service must be flagged with a WebMethod attribute in front of it. Without this attribute, the method will not be exposed from the service.
NOTE: Not every method needs to have the WebMethod attribute. Only WebMethod methods will be remotely accessible as Web services.
Add the following method to the MathServices class that you just created:
[WebMethod]
Public int Add(int a, int b)
{
Return(a + b);
}
[WebMethod]
Public System.Single Subtract(System.Single A, System.Single B)
{
Return (A - B);
}
[WebMethod]
Public System.Single Multiply(System.Single A, System.Single B)
{
Return A * B;
}
[WebMethod]
Public System.Single Divide(System.Single A, System.Single B)
{
If(B == 0)
Return -1;
Return Convert.ToSingle(A / B);
}
- Click Build on the Build menu to build the Web service.
- Browse to the MathService.asmx Web service page to test the Web service.
- If you set the local computer to host the page, the URL is http://localhost/MathService/MathService.asmx.
The ASP.NET runtime returns a Web Service Help Page that describes the Web service. It also enables you to test different Web service methods.
Consume a Web Service
- Open Visual Studio .NET.
- Under Project types click Visual C# Projects, then click Console Application under Templates.
- Add a reference for the MathService Web service to the new console application.
This step creates a proxy class on the client computer. After the proxy class exists, you can create objects based on the class. Each method call that is made with the object then goes out to the uniform resource identifier (URI) of the Web service (usually as a SOAP request).- On the Project menu, click Add Web Reference.
- In the Add Web Reference dialog box, type the URL for the Web service in the Address text box and press ENTER.
- If you set the local computer to host the Web service, the URL is http://localhost/MathService/MathService.asmx.
- Click Add Reference. Alternatively, you can type the URL to the discovery file (MathService.vsdisco) or click Web References on Local Web Server in the left pane to select the MathService service from the list.
- Expand the Web References section of Solution Explorer and note the namespace that was used.
- Create an instance of the proxy object that was created. Place the following code in the function called Main:
- Localhost.Service1 myMathService = new localhost.Service1();
- Invoke a method on the proxy object that you created in the previous step, as follows:
- Console.Write("2 + 4 = {0}", myMathService.Add(2,4));
- Click Build on the Build menu to build the console application.
- Click Start on the Debug menu to test the application.
- Close and save the project.