Home > Tutorial for Beginners > How to Access and create XML Web Services in C Sharp

How to Access and create XML Web Services in C Sharp

Web service is very important, we can create web services using .Net either C# or VB.net or any .Net supported languages. Web Services are platform independent and can be used remotely.  If your OS supports to HTTP protocols then web services can be consumed by any program so they are platform independent we can say.  To understand the web service you must have at least basic knowledge of C Sharp or any programming language and Ado.net.

 
In .Net framework we can create two types of Web service
  1. Xml Web services: which are used by normal programs and
  2. Web Services of Remoting.

Web services are called by SOAP (Simple Object Access Protocol) or HTTP (Hyper Text Transfer Protocol) protocols and they always returns a data in XML format (here I am considering xml web service).


Before moving ahead you must be aware of the following terminology

  • WSDL: It stands for Web Service description language. It is used to describe your web service. It uses XML.  You can create the proxy of your Web Services by using WSDL
  • UDDI: It means Universal Description, Discovery and Integration. It used to find the appropriate web service on the web.
  • SOAP: It means Simple Object Access Protocol. It is very important. You can transfer your data by using SOAP it uses XML format. You can also use HTTP-Get or HTTP-Post to passing a data
  • Disco: It is known as discovery and it has list of WSDL documents.
    How to create Sample web service

  • Open visual studio -> Select New Web Site then choose Asp.net Web Service from Templates
  • Give a meaning full name, your first web service is ready, that’s it.
  • Run your web site you will see your first web service

Xml Web services CSharp

Xml Web services CSharp

Extension of WebService project will be .asmx it contains two files

  • .asmx: asmx files contains the <%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %> attribute.
  • .cs: Here you can write your complete business logic
    Web services are inherited from System.Web.Service class.
     

    [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 Service : System.Web.Services.WebService
    {
       public Service () {

           //Uncomment the following line if using designed components
           //InitializeComponent();
       }

       [WebMethod]
       public string HelloWorld()
       {
           return "Hello World";
       }
     

    }

 

[WebMethod] tells the compiler, that the following method is going to expose.

So if you have any method which you want to expose, you must be add [WebMethod] attribute before the method name.

 

To consume a Web Service: Add another project in your current solution then add the web reference of your web service in your newly created project.

XML WebServices C#

 

Choose Service name, I have given myWS name to my web service see below screen

Now you are ready to use your webservice’s methods as

XML Web Services

public partial class _Default : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           myWS.Service obj = new wsClient.myWS.Service();
           string str =obj.HelloWorld();
       }
 

   }

That’s it. Feel free to contact us for any query

Post Comments Below

  1. No comments yet.
  1. No trackbacks yet.