Showing posts with label webservices in dotnet. Show all posts
Showing posts with label webservices in dotnet. Show all posts

Friday, September 11, 2009

Web Services in C#

Introduction

Web Service is known as the software program. These services use the XML to exchange the information with the other software with the help of the common internet Protocols. In the simple term, we use the Web Service to interact with the objects over the internet.

Here are some points about the Web Service

  1. Web Service is not dependent on any particular language.
  2. Web Service is a protocol Independent.
  3. Web Service is platform-independent.
  4. Web Service is known as the Stateless Architecture. These services are dependent only on the given input.
  5. Web Service is also Scalable.
  6. Web Service is based on the XML (Open, Text-Based Standard).

Technology Used in Web Service:

XML: Web Service specifies only the data. So, the application which understands the XML regarding the programming language or the platform can format the XML in different ways.

SOAP: The communication between the Services and the application is set up by the SOAP.

WSDL: WSDL gives us a uniform method that is helpful to specify the Web Services to the other programs.

UDDI: With the help of UDDI, we can search the Web Service registries.

At the time of the deployment of these technologies, this allows the developers to do the packaging of the applications in the form of the Service and publishing of the Service on the network.

Advantages of Web Services:

  1. Web Services always use the open, text-based standard. Web service uses all those components even they are written in various languages for different platforms.
  2. Web Service promotes the modular approach of the programming so that the multiple organizations can communicate with the same web service.
  3. Web Services are easy to implement but expensive because they use the existing infrastructure, and we can repackage most of the applications as the Web Service.
  4. Web Service reduces the cost of enterprise application integration and B2B communications.
  5. Web Services are the interoperable Organization that contains the 100 vendors and promote them interoperability.

Limitations of Web Services:

Limitation of Web Services are:

  1. One of the limitations of the Web Services is that the SOAP, WSDL, UDDI requires the development.
  2. Supports to the interoperability is also the limitation of the Web Service.
  3. The limitation of web service is also royalty fees.
  4. If we want to use web services for the high-performance situation, in that case, web service will be slow.
  5. The use of web service increases the traffic on the network.
  6. The security level for Web Services is low.
  7. We use the standard procedure to describe the quality of particular web services.
  8. The standard for the Web Service is in the draft form.
  9. To retain the intellectual property of the specific standard by the vendor is also the limitation of the web service.

Example of the Web Service:

Web Service can do almost any kind of task.

Web Portal: Web portal is used to fetch the headline news from the linked web service.

Weather Reporting: For the reporting of weather, we will use Weather Reporting Web Service for displaying the information about the weather on our website.

Stock Quote: The latest information about the share market with the Stock Quote can display on our website.

News headline: By using the news headline Web Service, we can show the latest update of the news on our website.

We can make our web service and can give them back to use. Here we are taking an example like we can make the Free SMS Sending Service with the footer for the company advertisement. So, whenever any person uses this Service, they will indirectly advertise our company. For taking advantage of the web service, we can apply the N number of ideas.

For creating the Web Service first, we will think about a scenario. For creating any web service, firstly, we should have to get to know why we need Web Service.

The need for Web Service:

We will think about a scenario which we want to show on our website. On our website, we want to show the information about the region, nation and about the international as well. Here if we are thinking of writing the code for all these functionalities, this will take lots of time and the effort to write the code for all these functionalities. All of the above information is already provided by some existing sites, so in that case, we want to use that current logic of other sites. But there is a problem arises how could I use the existing logic in my application.

The solution to this problem is Web Services.

By using the Web Service, we can reuse someone else's business logic, instead of replicating this. To use the business logic of someone else, we just have to write a few lines of the code. This technique is similar to the libraries of API, DLLs, and plug-ins.

The only difference between the libraries of API and the Web Service is that the Web Service is located remotely on another server.

Web Services can be invoked by the other applications. Web Services are known as the pieces of the business logic, which is hosted on the internet, and the other application can use them.

Here we have some points about the Web Services.

Note1: Web Services are not limited only to the .Net Framework. The standards of Web Services were already defined before the release of the .NET. Web Services and supported by the vendors other than Microsoft.

Note:2: Web Services can also work on the cross-platform. If the services were written in one language, these could be used by the other application despite, and the application used the other language. If we want to use the web services, the only way for that is we only need the internet to connect where we will make the HTTP request.

As we know that the Web Service is cross-platform, but despite this, there should be an understandable language so that we can make a request for the services and can get the Service in their response. Web Services use the XML, which can be understood easily.

This is the only reason why the web services were built with the XML based standards of exchanging the data.

Web Services uses the Set of Data type. The XML Schema easily recognizes these data types.

Web Services uses a simple data type like strings and numbers. These data types are helpful for communication with Web Services. And we cannot send the proprietary .NET objects like image, FileStream, or the EventLogs. The other programming language does not have any way to contact these .NET objects. If we use some devices to send them to the client, still the different programming languages will not be able to interpret.

Note:3 If we want to work with the .NET objects, we can use the .NET remoting. .NET remoting is known as distributed technology through which we can use the .NET objects. But the non-.NET client can't use it.

Data Types supported by the Web Service:

  • Built-in Types (Basic):
    Data Types used by the Web Services are:
    Web Services uses the built-in C# data types like short, int, long, short, float, char, byte, string, and DateTime.
  • Objects: WPF uses the object of the user-defined class. In spite of this thing that the class contains the methods, the class will not be possible to transmit to the client.
  • Arrays: WPF uses the arrays of any supported data type (built-in or customs). We can also use the ArrayList in WPF.
  • Enumerations: WPF supports enum. For the value of the enumeration, WPF uses the string name. WPF will not use integer.
  • XmlNode: Objects which are based on the System.Xml.XmlNode represents the portion of the XML document. We can use the XmlNode to send the arbitrary XML.

DataSet and DataTable: The WPF supports dataSet and DataTable, but the WPF does not support the ADO.NET data objects like DataColumns and DataRows.

Consume/Calling the Web Service from the Client Script in C#

 We can use web service in any type of application. Here we will create an application .Net web application. For that, we will follow the following steps:

Step1: Right-click on the Solution explorer->Add->New Project as shown in the below screenshot:

How to create Web Service

Step2: After clicking on the New Project a new window will open as shown in the below screenshot:

How to create Web Service

Step 3. After this, to communicate with the web service, we have to create a proxy class. To create the proxy class, we have to follow the following steps:

Proxy Class

For creating the proxy class, we have to the right click on the References->Select Add Service Reference as shown in the below screenshot:

How to create Web Service

After clicking on the Add Service Reference, a new window will appear as shown in the below screenshot:

How to create Web Service

Explanation of the above window:

Address: Here, we will paste the URL of the web service created by us and then click on Go.

After clicking on the Go button, this will search the provided address.

Namespace: in the namespace, we will provide the name of the Service and then click on the OK button. This will add the reference to the web service in the project.

Now we will add the Web Form in our application. For that, we will follow the following steps:

1. Right-click on the project in the solution explorer-> select add->choose web form as shown in the below screenshot:

How to create Web Service

After clicking on the Web Form, this will open a new window where we have to provide the name of the web form as shown in the below screenshot:

How to create Web Service

Here in the above screenshot, we gave the name of the web form and then click on OK.

In the Web Form, we will create a table to use the predefined method with the help of the Web Service.

In the WebForm1.aspx, we have done the following coding as shown in the below code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CalculatorWebApplication.WebForm1" %>    
  2. <!DOCTYPE html>    
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <table style="font-family:Arial">  
  10.             <tr>  
  11.                 <td>  
  12.                     <b>First Number</b>  
  13.                 </td>  
  14.                 <td>  
  15.                     <asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox>  
  16.                 </td>  
  17.             </tr>  
  18.             <tr>  
  19.                 <td>  
  20.                     <b>Second Number</b>  
  21.                 </td>  
  22.                 <td>  
  23.                     <asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox>  
  24.                 </td>  
  25.             </tr>  
  26.              <tr>  
  27.                 <td>  
  28.                     <b>Result</b>  
  29.                 </td>  
  30.                 <td>  
  31.                     <asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>  
  32.                 </td>  
  33.             </tr>  
  34.             <tr>  
  35.                 <td>  
  36.                     <asp:Button ID="btnadd" runat="server" Text="Add" OnClick="btnadd_Click" />  
  37.                     </td>                    
  38.             </tr>  
  39.         </table>  
  40.     </form>  
  41. </body>  
  42. </html>  

In the above code, create a table where we take the two textboxes, textbox1, for entering the firstnumber and the textbox2 for entering the SecondNumber. Create a button with the help of which we will add the two numbers.

The designing view of the above code is as shown in the below screenshot:

How to create Web Service

Here we have taken two textboxes for entering the firstnumber and secondnumber. The add button is used to add the value of both the textbox.

After double-clicking on the Add button, this will switch us to the coding page. The page is WebForm.aspx.cs.

To use the method of the Web Service, we will do the following code on the button click.

WebForm.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System. Linq;  
  4. using System. Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;    
  7. namespace CalculatorWebApplication  
  8. {  
  9.     public partial class WebForm1 : System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {   
  13.         }    
  14.         protected void btnadd_Click(object sender, EventArgs e)  
  15.         {  
  16.             CalculatorService.CalculatorWebServiceSoapClient client = new CalculatorService.CalculatorWebServiceSoapClient();  
  17.                 int result= client.Add(Convert.ToInt32(txtFirstNumber.Text),  Convert.ToInt32(txtSecondNumber.Text));  
  18.             lblResult.Text = result.ToString();  
  19.         }  
  20.     }  
  21. }  

Now we will press F5 to start the web service. The output will look like as shown in the below screenshot:

OutPut

How to create Web Service

Wrap Up

Here we got the benefit of the web service in this application. We didn't write any logic for the addition of the two numbers. Here in our calculator web application, we just used the method of the web service. We didn't use any logic for the addition. The above screenshot shows the output of the addition of two numbers, i.e., 23.

Popular Posts