Creating a simple ASP.NET Web Service


Introduction

This blog demonstrates how to create a simple hallow world ASP.NET Web Service using C Sharp.

Getting Started

A web service is nothing but web based application, which exposes classes, functions to out of the world. This application does not contains UI interface but , It also follows a code-behind architecture such as the ASP.NET web pages.

A web service is designed or creating using WEBService and WebMethod attribute, it support HTTP and HTTPS protocols. Web service supports one-way and Request-Response operation.

Web Service uses System.Xml.Serialization to serialize, but It can serializes only those collections which implement IEnumerable and ICollection means it cannot serialize HashTable.

Example:-

This demonstrates creates an ASP.NET Web Service which exposes student details like list of students, a particular student details etc.

Creating an ASP.NET Web Service is as simple as creating other .net application like ASP.NET Web application. You need to follow certain steps to get that, below details are the steps to create an ASP.NET Web Service.

Steps To Create

  1. Open your Visual Studio->On the File menu, click New and then click Project. Under Project types click Visual C# Projects, then click ASP.NET Web Service under Templates.
  2. Type StudentManagerService in the Location text box to change the default name (WebService1) to StudentManagerService.
  3. You can find in the solution explorer a web service file called WebService.asmx and its code behind file, Service.cs is created in the project.
  4. You can find in the solution explorer a web service file called WebService.asmx and its code behind file, Service.cs is created in the project.
  5. The .asmx file contains webService directive on it like below code .
     <%@ WebService Language="C#" CodeBehind="~/App_Code/WebService1.cs" Class="WebService1" %>  
    
  6. Change the name of the default Web service that is created from WebService1.asmx to StudentManagerService.asmx, in .asmx change in CodeBehind and class directive.
     <%@ WebService Language="C#" CodeBehind="~/App_Code/StudentManagerService.cs" Class="StudentManagerService" %>  
    
  7. Double click here to switch to code view in the designer environment to switch to code view. The default web service code behind file looks like the following.
     using System;  
     using System.Collections.Generic;  
     using System.Linq;  
     using System.Web;  
     using System.Web.Services;  
     /// <summary>  
     /// Summary description for WebService  
     /// </summary>  
     [WebService(Namespace = "http://tempuri.org/")]  
     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
     // 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 WebService : System.Web.Services.WebService {  
       public WebService () {  
         //Uncomment the following line if using designed components   
         //InitializeComponent();   
       }  
       [WebMethod]  
       public string HelloWorld() {  
         return "Hello World";  
       }  
     }  
    
  8. In web service, each method that will be exposed from the service must be flagged with a WebMethod attribute in front of it. Without this attribute, the method will not be exposed from the service.
  9. No need WebMethod to have in each method of service, it is useful to hide some implementation details called by public Web service methods or for the case in which the WebService class is also used in local applications. A local application can use any public class, but only WebMethod methods will be remotely accessible as Web services.
  10. Replace the code of WebService class with below code to expose student list and student details
  11.  using System;  
     using System.Collections.Generic;  
     using System.Linq;  
     using System.Web;  
     using System.Web.Services;  
     /// <summary>  
     /// Summary description for WebService  
     /// </summary>  
     [WebService(Namespace = "http://tempuri.org/")]  
     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
     // 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 StudentManagerService : System.Web.Services.WebService  
     {  
       public List<Student> Students  
       {  
         get { return this.getStudents(); }  
       }  
       public StudentManagerService()  
       {  
         //Uncomment the following line if using designed components   
         //InitializeComponent();   
       }  
       [WebMethod]  
       public List<Student> Get()  
       {  
         return this.Students;  
       }  
       [WebMethod]  
       public Student GetStudentDetails(int ID)  
       {  
         return this.Students.FirstOrDefault(s=>s.ID==ID);  
       }  
        [WebMethod]  
       private List<Student> getStudents()  
       {  
         List<Student> students = new List<Student>();  
         for (int ind = 1; ind <= 100; ind++)  
           students.Add(new Student() { ID = ind, Name = "Student" + ind, Class = ind.ToString() });  
         return students;  
       }  
     }  
     public class Student  
     {  
       public int ID { get; set; }  
       public string Name { get; set; }  
       public string Class { get; set; }  
     }  
    
  12. Right on StudentManagerService.asmx file, click on Set As Start Page, Build your Web service application and run.
  13. if there is no error in code, in browser you well get service details like below image.
  14. In above you can find the functions we have declare with web method. if you click on the function name it will ask you out invoke ,then click on invoke button, the browser will invoke the method and display the output. if you get our output, you can sure the your service is working.

Summary

Hope this article may help you to build you web service application. Happy coding and enjo........y

Thanks
Kailash Chandra Behera


No comments:

Post a Comment