How To Create A Web Service Project In .NET Using Visual Studio mesameergaikwad

 

How To Create A Web Service Project In .NET Using Visual Studio

Introduction

In this demo, today, we will try to create a web service project. We will also learn how to consume the web service in our project. Basically, at the end of this project, you should be able to use the web methods created in the web service in your code. So, if you are able to achieve this, you are good with this concept. Let’s go through each step one by one and see how it happens.

What is a Web service?

I hope you know the meaning of web service before you execute the demo. A web service is nothing but a software application that runs on the web having some exposed web methods which other applications can use over HTTP/ HTTPS protocols using technologies such as XML, SOAP, WSDL, and UDDI. Here in this demo, we will create one such web service and we will try to use its web methods. We will do all of this in a single web project but you can try it in different projects on the same machines as well as different projects on different machines. It should work.

Step 1

Click on File >> New >> Project as given below.

Once you click on Project, you will see the following pop-up window.

Step 2

Here, choose ASP.NET Web Application (.NET Framework) and give it a name as I have given — WebServiceProject. Click on OK.

Select the Empty template.

It creates a solution having the following solution structure.

Right-click the project.

Step 3

Once you click on the New Item, choose Web Service and give it a name as given below.

Step 4

Now, write the following code in WebService.asmx file.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. namespace WebServiceProject
  7. {
  8. /// <summary>
  9. /// Summary description for WebService
  10. /// </summary>
  11. [WebService(Namespace = “http://tempuri.org/")]
  12. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  13. [System.ComponentModel.ToolboxItem(false)]
  14. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  15. // [System.Web.Script.Services.ScriptService]
  16. public class WebService : System.Web.Services.WebService
  17. {
  18. [WebMethod]
  19. public string HelloWorld()
  20. {
  21. return “Hello World”;
  22. }
  23. [WebMethod]
  24. public int Add(List<int> listInt)
  25. {
  26. int result = 0;
  27. for(int i =0; i< listInt.Count; i++)
  28. {
  29. result = result + listInt[i];
  30. }
  31. return result;
  32. }
  33. }
  34. }

Here, WebMethod HelloWorld comes by default when you create a web service. You can change its implementation if you want. We have implemented another method that can take a list of integers as input and will give you some of all the lists of integers given as input.

Now, if you run this project and point the URL to WebService.ASMX, then you will get the following result, which indicates that your web service has been created.

Now you can find all the descriptions of services written on this page having the list of methods which are available via this web service. You can also see the service description to get a better picture. If you will click methods, then you will see its soap request-response structures and for simple generic methods, you will also see an option to invoke methods.

How to consume this Web service — there are multiple ways for doing that. We will here try to use it via a web reference method. We will add a Web reference of this service in our project for consumption.

How to add a web service reference to Project?

For adding Web reference of this service in the project, right-click on the project and click “Add Service Reference”. Once you do that you will see the following popup window.

So here we have entered the path to the web service and once you click on Go you will see the service structure like this. You can also see methods exposed by clicking on WebService over here. Just give it a namespace name that you want to use. For us, we are using ServiceReference1. Click OK and it will add a service reference of this web service in your project. mesameergaikwad

Now how to use this web service?

For this, I will add a webform page to our project. Here I have added WebForm.aspx having a button and a label. What I want to do here is on click of a button in this web form I should get the sum of an integer list using our web service method. So code looks like below in WebForm.aspx.

  1. <%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”WebForm.aspx.cs” Inherits=”WebServiceProject.WebForm” %>
  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. <div>
  10. <asp:Button ID=”Button1" runat=”server” Text=”Button” OnClick=”Button1_Click” style=”height: 26px” /><asp:Label ID=”Label1" runat=”server” Text=”Label”></asp:Label>
  11. </div>
  12. </form>
  13. </body>
  14. </html>

In WebForm.aspx.cs, I have written a handler like this.

  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. using WebServiceProject.ServiceReference1;
  8. namespace WebServiceProject
  9. {
  10. public partial class WebForm : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. WebService webService = new WebService();
  18. List<int> lstIntegers = new List<int> { 5, 6, 7 };
  19. Label1.Text = “Output of WebService: “ +webService.Add(lstIntegers).ToString();
  20. }
  21. }
  22. }

So now, if you run this project and point to this webform, you will get the sum of all these hardcoded 3 integers lists using the Add method. The output of this comes as below after clicking the button on the page.

The task is for this concept.

Conclusion

We have understood using the steps above how to create a web service and how to use a web service using the addition of web service in the C# web project in visual studio. There are other ways also available to consume this via SOAP and other web mechanisms. Do run all these steps and you should not be getting any issues in setting all of the above things on your machine.


Post a Comment

0 Comments