RSS

Web Services in C#

03 Sep

Web Services are the basic fundamental building blocks of invoking features that can be accessed by an application.

The accessibility to provide the features across different platforms that are developed using different languages, and can be used by any remote application, over the internet, etc.., makes the web service more useful at times by means of providing the user only the specific and relevant information, avoiding the accessibility to resources that have to be securely maintained.

Web Services comprises of the following main aspects that are included,

  1. XML Web Services: The communication between the service and the application is through a standard format called XML (eXtensible Markup Language) which is universal and is accepted on any platform across the distributed network.
  2. WSDL (Web Service Description Language): It consists of the description about the web service, which is basically a file with .wsdl extension that you can find in your application folder. It includes the information about the namespace of the xml file which is the tag <xmlns:”name”>. it also holds the description of the elements that the service consists of and also the information relevant to the parameters supplied.
  3. SOAP (Simple Object Access Protocol): SOAP is a protocol that by means of which Web Services is enabled; it is the format that is acclaimed universally due to its nature of standardization and rules that it implements. The inter-communication in web services uses SOAP for the efficient communication.
  4. UDDI (Universal Discription, Discovery and Integration): UDDI is specifically a governing body that monitors the publication and discovery of the web services implementation with respect to message communication between the application and also at the enterprise level.
  5. IIS(Internet Information Server for MicroSoft Visual Studio .Net) : It is the core aspect in web technology which deals with the Client/Server model, as the web service is a module available throughout the web for usage. Whenever a client sends the request to find the web service to the UDDI , the UDDI in turn contacts the web server IIS(for Dot Net) and the discovery, description of the web service is returned after which the client sends a request to consume the web service.

A Simple Example:

A simple Example to create a Web Service:

Service Application

From the Visual Studio .net IDE select the file–> new –> project, select Visual C# projects from the Project Types and Select an ASP.NET Web Service from the templates available and name it as addnumservice. The extension of web service is of type .asmx file, change the name of the service.asmx page as ‘testservice.asmx’ or as per the naming convention that you are accustomed to but do adopt to a format.

Now your service application is ready to be coded for depending on your requirements. Now in the window pane select the ‘click here to switch to code view’ and you are taken to the code-behind file where you can write your code.

In the attribute [WebMethod] you can see a default simple method that is used to display a simple text “Hello World” , using the [WebMethod] attribute the code for your service is included,

Write the function for the above discussed web service method that is shown as below. In the Web method code include the following content,

[WebMethod] public int add(int a,int b) { return (a+b); }

Now that, your have written your method, the project needs to be built. To do that right click on your project name and from the context menu available select the build option which will complete your service method creation.

Environment: Windows, IIS, .Net

Client Application

This is the application that invokes the Web Service that is defined above.

Create a new project, select Visual C# Projects from the Project Types and select an ASP.NET Web Application from the Templates available and name it as clientusingwebservice or as per your convenience.Your web form should consists of two textbox controls for accepting the values from the user, two label controls for naming against the textboxes, a button control and a label control to display the message.

Now specify the relevant control ID’s naming as per your convenience and then the most important thing, in the Solution Explorer right-click on the References folder that you can find and select the Add Web Reference option. A Dialogue box pops up where you can find the list of options related to the repository of the web services, select the option (Web services on the local machine)™ after clicking on the link you are displayed with all the services currently available on the local machine. Select the relevant service that you want to include onto your application, in our example it being the (testservice)™.

Next follows the code that is included in the application that has to use the service, for this create an object of the service type and by using that object we call the method that is present in the service, as shown below;

write this code just above the Page_Load method. //creating an object for the service that has to be called for clientusingservice.localhost.Service1 serviceobj=new clientusingservice.localhost.Service1(); private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here }

And now for the Button click event from where you will perform the addition of two numbers accepted from the textboxes through the method in the service will be written, code as follows;

private void btnadd_Click(object sender, System.EventArgs e) { int sum; sum=serviceobj.add(int.Parse(txtnum1.Text),int.Parse(txtnum2.Text)); lblmsg.Text="Addition performed using service" +a; }

Now you are ready to use and see the working of the web that you have created by following the procedure as mentioned above build the solution and press F5 to view the results.In the next publication you will learn more about the enhancements that the web service can implement WSE, which include some advance features that are included in WS-*, like the WS-security, configuration, Addressing, etc..,.

Advertisements
 
Leave a comment

Posted by on September 3, 2009 in C#

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: