Create a Web Service and Consume in Application

In this article we will create a Web Service and consume it in a console application.

WebService

Web Services are the business logic components that run over HTTP using SOAP. Web Services have a *.asmx extension. It is platform independent.

Procedure to create a web Service

  1. Go to Visual Studio. Create a Web Application Project.
  2. Then right-click on the project then seelct Add -> New Item as in the following:



  3. Seelct Web in the left then select Web Services in the middle pane. Name it something. Click Add.


When the webservice is added to the application the following code is generated in the *.asmx.cs file.

  1. /// <summary>  
  2. /// Summary description for DemoWebService  
  3. /// </summary>  
  4. [WebService(Namespace = "http://tempuri.org/")]  
  5. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  6. [System.ComponentModel.ToolboxItem(false)]  
  7. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  8. // [System.Web.Script.Services.ScriptService]  
  9. public class DemoWebService: System.Web.Services.WebService  
  10. {  
  11.   
  12.     [WebMethod]  
  13.     public string HelloWorld()   
  14.     {  
  15.         return "Hello World";  
  16.     }  
  17. }  
If we run this webservice in a web browser then we get the following screen that displays the web method.



Now we will create one web method to add two numbers. Write the following code in the *.asmx.cs file.
  1. [WebMethod]  
  2. public int Add(int a,int b)  
  3. {  
  4. return a + b;  
  5. }  
It is a simple method that accepts two input parameters and calculates their sum.

After adding the method, let us again run the webservice to see if the method is added to the list.



We can see our method is included in the list of webmethods. Click on the web method and we get the following screen.



Enter numbers in the TextBox. These numbers are basically arguments to be passed to the web service. When we click on Invoke we get the sum of the numbers. Check the following screen.



Let us now use this webservice in a console application and pass data from the console application to the web service. Go to Visual Studio then select New Project -> Console Application.

Now we will add a Web Service Reference to this console application.

Right-click on the References then select Add Service Reference.



Click on Advanced.



Click on Add Web Reference.



Enter the web service URL in the space provided. Provide a meaningful reference name. Click Add Reference.



Once the reference is added in the console application we get the following changes in the application directory.



Also in the App.Config file we get the following code auto generated that specifies the Web Service URL along with the name.
  1. <applicationSettings>  
  2. <ConsumeWebService.Properties.Settings>  
  3. <setting name="ConsumeWebService_DemoService_DemoWebService"  
  4. serializeAs="String">  
  5. <value>http://localhost:1895/DemoWebService.asmx</value>  
  6. </setting>  
  7. </ConsumeWebService.Properties.Settings>  
  8. </applicationSettings>  
Now we are done with adding the reference. All we need to do now is to write some code to invoke this web service. Write the following code in the Program.cs.
  1. class Program  
  2. {  
  3.     static void Main(string[] args)   
  4.     {  
  5.   
  6.         DemoService.DemoWebService obj = new DemoService.DemoWebService();  
  7.         int result = obj.Add(14, 15);  
  8.         Console.WriteLine(result);  
  9.         Console.ReadLine();  
  10.     }  
  11. }  
We create the object of the Web Service attribute in the App.Config. And we get the Add method. Pass the values to the function. Run the application and check the following output.


Similar Articles