Search This Blog

Thursday, 16 June 2011

Warming up to WCF -- Web Services Part 2

Now, since we have already covered some basics let’s jump to questions that we asked at the very beginning:-

Do web services support method overloading?

Web services do support method overloading but it’s turned off by default due to web service internationalization. Since web services are designed so that they can be used everywhere (all technologies) so some standards must be followed while creating your service so that they follow basic language rules followed by all programming languages.

This is how your web service looks like…

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

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";
    }
   
}

Have a look at WebServiceBinding attribute above... What is it doing here?
It says that web service conforms to BasicProfile1_1 which is actually the standards that you must follow to internationalize your web service.

WS-I is web service internationalization. You can find more information here

Since this is the default value of attribute, if you try to overload the service it will throw error when you try to run the service. Reason is simple enough… Not all programming languages of world support method overloading so if your web service is internationalized it should also not expose overloaded methods. What if I am developing web service for my company’s intranet and I know that all my clients are going to be .Net clients… In that case you can modify your service to enable method overloading but you need to follow below steps:-

1.)   As a first step you need to change WsiProfiles.BasicProfile1_1 to WsiProfiles.None

2.)   Secondly, Provide a MessageName attribute to one of your methods.
           
        [WebMethod (MessageName="Test")]
      public string HelloWorld() {
           return "Hello World";
      }

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


        What is the purpose of this MessageName attribute?
   
    It all boils down to WSDL. As you should know that all web services expose a WSDL file to clients which contains all the operations (methods) exposed by the service and their equivalent SOAP representations. So for a method accepting an input and returning an output there will be one SOAP INPUT element and one SOAP OUTPUT element in WSDL. The SOAP INPUT will have same name as that of operation (method) and SOAP OUTPUT will be operation name with Response appended to it…Lets see the SOAP part of method after overloading…


<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="Test">
  <s:complexType />
  </s:element>
<s:element name="TestResponse">
<s:complexType>
<s:sequence>
  <s:element minOccurs="0" maxOccurs="1" name="TestResult" type="s:string" />
  </s:sequence>
  </s:complexType>
  </s:element>
<s:element name="HelloWorld">
<s:complexType>
<s:sequence>
  <s:element minOccurs="0" maxOccurs="1" name="name" type="s:string" />
  </s:sequence>
  </s:complexType>
  </s:element>
<s:element name="HelloWorldResponse">
<s:complexType>
<s:sequence>
  <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
  </s:sequence>
  </s:complexType>
  </s:element>
  </s:schema>
  </wsdl:types>


Thus if you have two methods with same name without a MessageName attribute, in that case there will be 2 same soap request and response generated in WSDL which is not a valid scenario… Thus that won’t compile.

No comments:

Post a Comment