React JS Training Course Online
- 21k Enrolled Learners
- Weekend
- Live Class
Ever thought of helping someone and did not find a way to do it? Yes, I know Software developers don’t get time to do all this. In fact, part of our lifetime goes in coding for some application or the other. So all this kept me thinking, as a developer is there anything good I can do to my fellow developers.
Why not write a code that could be used by any developer coding in any language? Wouldn’t that be awesome?
Yes! And guess what ? The solution is – “Web Services”
Web services by definition of W3C is “a software system designed to support interoperable machine-to-machine interaction over a network.”
Web services developed in one language can be used in any other language, and the best part is it helps to bring connectivity from one application to another.
When two systems communicate with each other, the software system that requests for service is called as the service requester and the software system that will process the request and provide the service is called as service provider.
Now, if you are pondering how does the communication happen, let’s get to it.
There are certain rules that define how communication can happen between different systems. These rules include how one system can request data from another system, the parameters that are needed in data request, the structure of data produced and the error messages that are displayed when certain rules are broken. These rules are defined in a file called WSDL (Web Services Description Language) with extension .wsdl.
After a web service is made, a WSDL file is generated to describe the web service that uses the soap protocol () to publish or register the service in the UDDI (Universal Distribution Discovery and Interoperability) so that our service is made available to others.
UDDI defines which software system should be contacted for which type of data.
The service requester contacts the UDDI and checks for the provider who gives the data it needs. Then it contacts the service provider using soap protocol. The service provider validates the request by referring to the WSDL and sends back structured data in XML using the Soap Protocol. This XML is again validated using XSD (XML Schema Definition- a document that defines rule or elements for XML).
Now that you have understood how it works, let us develop a web service.
There are mainly three aspects that govern the web service development in .NET.
1. Creating a web service
2. Creating a proxy
3. Consuming the web service that is created.
To create a web service in .net:
1. Right click on your project> Add > New Item.
1. Web > Web Service
2. Put in a name for your web service
3. Click add.
Note that web service files ends with .asmx
Now you have created a default web service, you will see the following:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebApplication5 { /// <summary> /// Summary description for WebService2 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class WebService2 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } }
In the above code snippet, the web service class inherits from the System.Web.Services.WebService which is surrounded by a Web service attribute [WebService], where you can specify the namespace and provide a brief description for the web service.
The WebMethod attribute [WebMethod], is used to declare a method as part of a web service. It must be placed before the declaration of each method you want to expose in the web service and also all the methods should be declared public.
To make your web service method, add your code into a public method with the web method attribute into the WebService1 class. For example:
[WebMethod] public int ConvertToFarenheit(int celsius) { int f = 0; f = (celsius * 9 / 5) + 32; return f; }
After that run the web service, we will get the page below:
This includes the methods that we expose in the web service and also a link to the service description which is WSDL file.
Now to test your web service, click on the method, to get the below page. Pass the parameter value and click invoke.
You will get the output of the web service like this:
Now, let’s see how to consume the web service we have created in our application.
1. In your project, right click on references > add service reference
As given in the below screen shot:
1. Give the URL of the service descriptor or WSDL file into address. And click go.
It finds the web service we have created .
2. Click OK
In the solution explorer we can see, a service reference directory is created under the References. It includes a folder that contains all files related for consuming the web service
Here, ServiceReference1 is the web service we created.
In the ServiceReference1>Reference.svcmap> reference.cs there is a proxy class created (WebService1SoapClient) with which we can call methods defined in our web service.
To use it in our application:
1. Create the object of the proxy class.
2. Call the methods available in the web service through the object of the proxy class.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using ServiceConsumeApp1.ServiceReference1; using ServiceConsumeApp1.ServiceReference1; namespace ServiceConsumeApp1 { public partial class WebForm1 : System.Web.UI.Page { protected void btnConvertRates_Click(object sender, EventArgs e) { WebService1SoapClient fn1 = new WebService1SoapClient(); TextBox2.Text = fn1.ConvertToFarenheit(Convert.ToInt32(TextBox1.Text)).ToString(); } } }
When we run the application, the output will be displayed as follows, after we have placed the appropriate controls in the web form to display our result.
So, this is how we create and consume a web service in .NET.
You can create many web services in .NET with the above steps mentioned. These web services can be accessed by any application built in any language. Web services hence addresses the communication problems between different applications and proves a real deal. Check out this Full Stack developer course to learn more about web services.
Check out the Angular Certification program by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Angular is a JavaScript framework that is used to create scalable, enterprise, and performance client-side web applications. With Angular framework adoption being high, performance management of the application is community-driven indirectly driving better job opportunities.
Got a question for us? Please mention it in the comments section and we will get back to you.
Related Posts:
Course Name | Date | Details |
---|---|---|
Web Developer Certification Training Course | Class Starts on 2nd November,2024 2nd November SAT&SUN (Weekend Batch) | View Details |
Web Developer Certification Training Course | Class Starts on 4th January,2025 4th January SAT&SUN (Weekend Batch) | View Details |
edureka.co