WebService Attribute's Property in Web Service


Introduction:

The WebService attribute is used for giving a description (generally, about using the service ) of a web service by adding some information. Here I am describing it's properties by which we add description to web service.

  • Description
  • Name
  • Namespace

At first we create a web service without using any properties (Given above) of the Web Service. Then we will use these properties in the web service.

Creating a Simple Web Service:

Use the following steps to create a web service.

  • Go to Visual Studeo 2010  and create a New Project
  • Select .NET Framework 3.5
  • Create an ASP.NET Web Service Application
  • Give it a name and click ok button
Replace the following code by the code given below:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;

namespace WebSA
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string show()
        {
            return "This is example for WebService Attribute's Property";
        }
    }
}

Run the service application.

Output:

webservice property

Creating Web Service using Property:

Description:
The Description property of WebService is used for adding description to web service. We simply assign description text to this property.

Now we create a web service using the Description property. Take a ASP.NET
Web Service Application and replace the code with following code.

using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
 

namespace
WebSA
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Description="Hello")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string show()
        {
            return "This is example for WebService Attribute's Property";
        }
    }
}
 
Run the application

Output:

name property of webservice

Name:
The Name property of WebService is used to assign name to web service. Default service name is same as class name.

Now create a web service using this property. Replace the code with following code:

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
 

namespace
WebSA
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Description="Hello",Name="A Simple WebService")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string show()
        {
            return "This is example for WebService Attribute's Property";
        }
    }
}

Run the application.

Output:

name property of webservice

Namespace:
The Web Service as well as web method uniquely identifies by this property. Default namespace is "http://tempuri.org/" (As you have seen in first example.). Now we create web service by using the namespace property. Again create an ASP.NET Web Service Application and replace the code with following code. 

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
 

namespace
WebSA
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Description="Hello",Name="A Simple WebService",Namespace="http://abc.com")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string show()
        {
            return "This is example for WebService Attribute's Property";
        }
    }
}
 
Run the application. On output window click on Service Description link.

Output:

namespace property of webservice

Up Next
    Ebook Download
    View all
    Learn
    View all