Web Services Using C# - Chapter 3 - Creating Web Services

Before reading this blog, I would recommend you to read the previous parts of it.

Let's start now.
 
STEP 1

Open Visual Studio > File > New > Web Site…
 


Step 2

Create “ASP.NET Empty Web Site”. (name it say “MyWebService”) and save it in the desired location.



STEP 3

Right-click on Project Folder in Solution Explorer on the right side > Add > New Item.



Step 4

Select your language (C# in my case) > Select Web Service (ASMX) >Click Add after naming it (say MyWebService.asmx)



Explanation of the default code



[WebService(Namespace = "http://tempuri.org/")]

Each XML Web service needs a unique namespace in order for client applications to distinguish it from other services on the Web.

tempuri.org is the test default namespace URI. It is available for XML Web services that are under development, but published XML Web services should use a more permanent namespace. The term is strictly a placeholder and all instances of it should be replaced with a more meaningful URI in production systems.

An XML Web service should be identified by a namespace that is controlled by its company. For example, a company's Internet domain name could be used as part of the namespace. Although many XML Web service namespaces look like URLs, they need not point to actual resources on the Web.

  • Cannot have more than one [WebService] tag
  • Changing your namespace will have no effect on performance. It is just a unique identifier

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

Web service does not support method overloading directly. In order to allow overloading in the web services,

We have to add the MessageName property to make difference between methods and change

[WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)] to

[WebServiceBinding(ConformsTo = WsiProfiles.None)].

Example

  1. [WebMethod(MessageName = "twoparameter", Description = "addition of two integers")]  
  2. public int add(int a, int b) {  
  3.         return (a + b);  
  4.     }  
  5.     [WebMethod(MessageName = "threeparameter", Description = "addition of three integers")]  
  6. public int add(int a, int b, int c) {  
  7.     return (a + b + c);  
  8. }  

[System.Web.Script.Services.ScriptService]

To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

// [System.Web.Script.Services.ScriptService]

[WebMethod]

This attribute is used at the top of the method that you write in the Web Service. This is used to specify that this method is exposed to the user/Client to access.

If you remove this attribute, the client will not be able to see the method details, hence can't implement it.

Proxy Class OR WSDL

Visual Studio generates a proxy class using the WSDL (Web Service Description Language) document of the web service. The WSDL document formally defines a web service. It contains,

  1. All the methods that are exposed by the web service
  2. The parameters and their types
  3. The return types of the methods

This information is then used by Visual Studio to create the proxy class. The client application calls the proxy class method. The proxy class serializes the parameters, prepares a SOAP request message, and sends it to the web service. The web service executes the method and returns a SOAP response message to the proxy. The proxy class will then deserialize the SOAP response message and hands it the client application. We don't have to serialize or deserialize dot net CLR objects to and from SOAP format. The proxy class takes care of serialization and deserialization and makes the life of a developer much easier.

In the next chapter, we will learn how to consume Web Services in client applications.