Web Service in C# - c# - c# tutorial - c# net




What is Web Service in C#?

  • Web Service in C# is cross-platform and it is a service which is written in one language and it can be invoked by an application
  • Web Service is built on XML-based standards for exchanging data.
  • Web Service is limited to the set of data types recognized by the XML Schema standard
  • Web Service is limited to the set of data types recognized by the XML Schema standard
  • Hence Web Service is not limited to the .NET Framework in C#
  • The only way to access Web Service is an internet connection to make the HTTP request

Create a web service

  • This topic is tell us as per how to create a web service
  • Hence we create a web service which will return the sum of two integers

Steps to create a web Service

Step 1:

Open Visual Studio in Administrator mode

Step 2:

To create a new project, click File → New → Project and select .Net Framework 3.5 (on the top)

Step 3:

Select ASP.NET web service application and name the project (which can be named as MyWebServiceDemo) and click the OK button.

 create web service in webservices in c#

Learn c# - c# tutorial - create web service in webservices in c# - c# examples - c# programs

Step 4:

Visual Studio will create a web service (Service1.asmx and Service1.asmx.cs) for the project which is given.

Step 5:

The web service that will return two integers has been created successfully in the Visual Studio for C#

Implementing a web service

  • After creating the web services, we need to implement the web services
  • In order to implement the web service, rename "Service1" file to "MyService" and after renaming the web service, change the class name from Service1 to MyService
  • Then open the mark up (asmx) page
  • Thus, change the "CodeBehind" property from "Service1.asmx.cs" to "MyService.asnx.cs" as we rename the file

MyService.asmx

<%@ WebService Language="C#" CodeBehind="MyService.asmx.cs"  
Class="MyWebServiceDemo.MyService"%> 
click below button to copy the code. By - c# tutorial - team

MyService.asmx.cs

using System.Web.Script.Serialization;  
using System.Web.Services;  
  namespace MyWebServiceDemo  
{     
    [WebService(Namespace = "https://www.wikitechy.com/")]      
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
    [System.ComponentModel.ToolboxItem(false)]  
    [System.Web.Script.Services.ScriptService]  
    public class MyService : WebService  
    {  
        [WebMethod]  
        public int SumOfNums(int First, int Second)  
        {  
            return First + Second;  
        }  
        
        
    }  
} 
click below button to copy the code. By - c# tutorial - team
  • The service has been implemented successfully

Test a web service

  • After implementing the web service, the next step is to compile and test the web service
  • The "http://localhost:56655/MyService.asmx" page will open which has a link for the Service description (the WSDL document, documentation for web service)
  • We can use method overloading of the OOP concept.
  • Add the following WebMethod in MyService class.
[WebMethod]  
public float SumOfNums(float First, float Second)  
{  
      return First + Second;  
} 
click below button to copy the code. By - c# tutorial - team
 test web service in web services in c#

Learn c# - c# tutorial - test web service in web services in c# - c# examples - c# programs

  • Here is a program which demonstrate the test of Web Service

Program:

using System.Web.Script.Serialization;  
using System.Web.Services;  
  namespace MyWebServiceDemo  
{  
    [WebService(Namespace = "https://www.wikitechy.com/")]  
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]  
    [System.ComponentModel.ToolboxItem(false)]  
    [System.Web.Script.Services.ScriptService]  
    public class MyService : WebService  
    {  
        [WebMethod]  
        public int SumOfNums(int First, int Second)  
        {  
            return First + Second;  
        }  
        [WebMethod(MessageName = "SumOfFloats")]  
        public float SumOfNums(float First, float Second)  
        {  
            return First + Second;  
        }  
    } 
click below button to copy the code. By - c# tutorial - team


Related Searches to Web Service in C#

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×