ASP.NET Web Services Part 2

You may have noticed that this post is about two months late. Time flies, doesn’t it? The previous post on an introduction to web services and how they work with ASP.NET provided you with a cursory glance. This post will provide you will how to implement a very basic web service both on the server side for consumption and the client side for use in an application.

Prerequisites

  • Visual Studio 2005 or 2008
  • Basic knowledge of C#
  • Basic knowledge of ASP.NET Web Services

Step 1 – Create the ASP.NET Web Service

  • Visual Studio –> File –> New –> Project… –> Visual C# –> Web –> ASP.NET Web Service Application

Once the web service application project is created, notice that a templated web service was also created (Service1.asmx). The only section of this template to be worried about at the moment is the HelloWorld method marked with the [WebMethod] attribute. Marking methods as a [WebMethod] identifies that they will be used by an external caller through XML SOAP communication and are required to be exposed by the web service WSDL described in the previous post.

  • Make sure the HelloWorld method looks exactly like below.

  • That is literally all it takes to expose a web service method to the world! Press F5 to begin debugging this application in a local web server and continue to Step 2.

Step 2 – Utilize the Exposed Web Service Methods

One thing to note is that because web services are implemented via XML, any application that can properly serialize commands into the correct format as described by the web service WSDL has the ability to utilize web services. That said, the following section will be written using a simple C# console application.

  • Visual Studio –> File –> New –> Project… –> Visual C# –> Windows –> Console Application
  • Like library references, you need to add a reference to the URL path of the hosted web service. In the case of this article, the web service is hosted locally in the previous step.
  • In the main project of the solution explorer, right click Web References –> Add New Web Reference…
  • In the URL textbox, copy and paste the URL found in your browser that popped up from Step 1 when you pressed F5 to debug.
  • Press the Go button to the right of the URL textbox to search for any exposed methods at the URL provided.
  • When found, enter a name for the Web reference in the bottom right (I named it localhost.)
  • Press add reference to allow Visual Studio to create classes that allow consumption of the web service.
  • Add the following code to the Main method. Make sure you change “localhost” to whatever you called your web reference and change Service1 to the name of your exposed web service class.

  • Assuming that the web service local server is running properly from Step 1, press F5 to debug the console application. The output in the console should be the return value of the HelloWorld method (“Hello World”.)

Wrap up

One thing that certainly stands out about this approach to web service client/server creation is the fact that Visual Studio and the .NET framework take nearly all of the XML generation away from the developer. In fact, no knowledge of XML is even required to get .NET to .NET communication working via web services. Obvious advantages of this is that time is hugely saved and many of the errors in XML serialization are handled by the framework. A big disadvantage is that many developers do not understand what is really happening with web services. XML knowledge is required if you are looking to consume ASP.NET web services from an application that does not utilize a framework capable of abstracting the XML serialization process.

One thought on “ASP.NET Web Services Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *