Web Services in C# - ASP.Net Web Services
Web Services in C#
- Web Service is otherwise known as software program and In the simple term, we use the Web Service to interact with the objects over the internet.
- Those services use the XML to exchange the information with the other software with the help of the common internet Protocols.
- Web Service is not dependent on any particular language and it is a protocol Independent.
- Web Service is platform-independent and it is also scalable.
Technology Used in Web Service
- Web Service specifies only the data and so, the application which understands the XML regarding the programming language or the platform can format the XML in different ways.
- SOAP is used to set up the communication between the Services and the application.
- WSDL gives us a uniform method that is helpful to specify the Web Services to the other programs.
- We can search the Web Service registries with the help of UDDI.
Advantages of Web Services
- Web Services always use the open, text-based standard and it uses all those components even they are written in various languages for different platforms.
- With same web service, it promotes the modular approach of the programming so that the multiple organizations can communicate.
- They are easy to implement but expensive because they use the existing infrastructure, and we can repackage most of the applications as the Web Service.
- It reduces the cost of enterprise application integration and B2B communications.
- They are the interoperable Organization that contains the 100 vendors and promote them interoperability.
Limitations of Web Services
- One of the limitations of the Web Services is that the WSDL, SOAP, UDDI requires the development.
- Supports to the interoperability is also the limitation of the Web Service.
- The use of web service increases the traffic on the network and security level for Web Services is low.
- We use the standard procedure to describe the quality of particular web services and standard for the Web Service is in the draft form.
- Whenever we want to use web services for the high-performance situation, in that case, web service will be slow.
Example of the Web Service
- It is used to fetch the headline news from the linked web service.
- We will use Weather Reporting Web Service for displaying the information about the weather on our website, for the reporting of weather.
- The latest information about the share market with the Stock Quote can display on our website.
- We can show the latest update of the news on our website, by using the news headline Web Service.
Data Types supported by the Web Service
Built-in Types (Basic)
- In C#, web services use the built-in data types like short, int, long, short, float, char, byte, string, and Date Time.
- WPF uses the object of the user-defined class.
- The class will not be possible to transmit to the client, in spite of this thing that the class contains the methods.
- WPF uses the arrays of any supported data type like built-in or customs and We can also use the Array List in WPF.
- WPF supports enum and it will not use integer.
- WPF uses the string name, for the value of the enumeration.
- Objects which are based on the System.Xml.XmlNode represents the portion of the XML document.
- Here we can use the Xml Node to send the arbitrary XML.
Data Set and Data Table
- WPF supports data Set and Data Table, but the WPF does not support the ADO.NET data objects like Data Columns and Data Rows.
How to create Web Service
- First, we will create a Web Application for creating the Web Service.
- For that, we will click on the File-> Select Project as shown in the below screenshot:
- We will select the Web->Asp.Net Web Application->Name of the Web Application-> Click on OK.
- After this a new window will be shown us like as shown below
- We will do the right click on the project name-> Click on Add-> Add New Item. As shown in the below screenshot:
- After this, a new window will appear. From where we have to click on the Web-> Select Web Service(.asmx page)-> Give a name to the Web Service as shown in the below screenshot:
- After adding the Web Service to the application, a new window will appear which is shown as below:
Consume the Web Service from the Client Script
- In any type of application, we can use web application and here we will create an application .Net web application.
- Right-click on the Solution explorer->Add->New Project as shown in the below screenshot:
- After clicking on the New Project, a new window will open as shown in the below screenshot:
- 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:
- To create the proxy class, we have to the right click on the References->Select Add Service Reference as shown in the below screenshot:
- After clicking on the Add Service Reference, a new window will appear as shown in the below screenshot:
Implementing a web service
- As we can see here Visual Studio is unable to resolve “Service1” in the class property, since the class indicates a fully qualified name of the service and we renamed our Service1 class to MyService. So Visual Studio is unable to resolve it. So, now change the class property to “MyWebServiceDemo.MyService”.
- Change the “CodeBehind” property from “Service1.asmx.cs” to “MyService.asmx.cs” as we renamed the file also.
Test a web service
- Let's run the project by hitting F5. The “http://localhost:56655/MyService.asmx” page will open that has a link for the Service description (the WSDL document, documentation for web service) another link for SumOfNums, which is for test page of SumOfNums method.
- Let's use method overloading of the OOP concept. Add the following WebMethod in MyService class:
- Hit F5 to run the application, you will get “Both Single SumOfNums(Single, Single) and Int32 SumOfNums(Int32, Int32) use the message name 'SumOfNums'.
- Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods” error message”.
- We just used method overloading concept, so why this error message? This is because these methods are not unique for a client application.
let's use the MessageName property of the WebMethod attribute as shown below:
- Now, compile and run the application.
- Again it's showing some different error message “Service 'MyWebServiceDemo.
- MyService' does not conform to WS-I Basic Profile v1.1”.
- As, WsiProfiles.BasicProfile1_1 doesn't support method overloading we are getting this exception.
- Now, either remove this “[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]” attribute or make it “[WebServiceBinding(ConformsTo = WsiProfiles.None)]”.
The WSDL document
- Web Services are self-describing, that means ASP.NET automatically provides all the information the client needs to consume a service as a WSDL document.
- The WSDL document tells a client what methods are present in a web service, what parameters and return values each method uses and how to communicate with them. It is an XML standard.
Host a Web Service
- Since we will add a reference to this service and consume it from various applications and the port number is supposed to change, let's host this service on IIS-Internet Information Server to have a specific address of a service.
- Open IIS, go to the default web sites node then select Add Application then provide an alias name (I gave it WebServiceForBlog) then browse to the physical location of your service for the physical path field then click “OK”.
- We can now browse with an alias name (like http://localhost/WebServiceForBlog/) to test if the application was hosted properly.
- We will get a “HTTP Error 403.14 – Forbidden” error since there is no default document set for this application.
- Add a “MyService.asmx” page as a default document.
- Now we can browse our service.
How to access a web service from a client application
- Compile this application, Since we will consume this service from other applications we need a fixed address for this service, the port numbers are supposed to vary.So, host the web service on IIS (refer to the previous article for hosting the web service).
- I hosted it with the virtual directory “WebServiceForBlog”.
How to consume a web service
How to consume a web service from a client script
- Add another project to your solution and named it “Consume Service from Client Script”.
- We can define one or more services that a page can call asynchronously to send or receive data using the ASP.NET AJAX “ServiceReference” control and assigning the Web Service URL to the control's “Path” property.
Add a ScriptManager control like the following inside the form tag:
- Add an empty Web form to your project. I named it “Default.aspx”:
Call SOAP-XML Web Services directly
- In order to call the web service, we need to supply an XML message that matches the operation definition specified by the web service's WSDL.
- In the test page “http://localhost/WebServiceForBlog/MyService.asmx?op=SumOfNums” we can see a sample of SOAP 1.1/1.2 request and response format.
- From which we can get the operations schema.
- Now, we just need to exchange the type names for the operation's parameters, with their actual values.
- Copy the SOAP envelope to the Default.aspx page and replace the place holders (type names) with an actual value.
- A sample for this type of call is included in the code snippet above.
- The variable “SoapMessage” in the preceding example contains the complete XML message that we're going to send to the web service.
- On completion of the ajax request, we will call a call back method named “ShowResult”.