We usually do method overloading with our normal csharp
classes yet when we casually try to overload a web method we come across an
error while consuming it.Today we will take this matter to logical end as we are not in hurry to complete the task.
Lets add a webservice to your website add two method to add
integers as follows
[WebMethod]
public int AddInt(int num1,int num2) {
return
num1 + num2;
}
[WebMethod]
public int AddInt(int num1, int num2,int num3)
{
return
num1 + num2+ num3;
}
Code in your aspx page should look similar to
[WebService(Namespace
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
public class
Math : System.Web.Services.WebService {
public Math
() {
//Uncomment
the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public int AddInt(int num1,int num2) {
return
num1 + num2;
}
[WebMethod]
public int AddInt(int num1, int num2,int num3)
{
return
num1 + num2+ num3;
}
}
Now compile code you will not get any error, make this asmx
page as start page and run it you will be greeted with following error
Both Int32 AddInt(Int32, Int32, Int32) and Int32 AddInt(Int32, Int32) use the message name 'AddInt'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.
Now let’s find what went wrong
Error ask us to give different MessageName to overloaded methods lets
do the same
[WebMethod(MessageName="AddTwoInts")]
public int AddInt(int num1,int num2) {
return
num1 + num2;
}
[WebMethod(MessageName
= "AddThreeInts")]
public int AddInt(int num1, int num2,int num3)
{
return
num1 + num2+ num3;
}
Modify your code to
accommodate different MessageName to overloaded methods re
run
Again we got some error
message this time bit long
Service 'Math' does not conform to WS-I Basic
Profile v1.1. Please examine each of the normative statement violations below.
To turn off conformance check set the ConformanceClaims property on
corresponding WebServiceBinding attribute to WsiClaims.None.
R2304: Operation name overloading in a wsdl:portType is disallowed by the
Profile. A wsdl:portType in a DESCRIPTION MUST have operations with distinct
values for their name attributes. Note that this requirement applies only to
the wsdl:operations within a given wsdl:portType. A wsdl:portType may have
wsdl:operations with names that are the same as those found in other
wsdl:portTypes.
- Operation 'AddInt' on portType 'MathSoap' from namespace
'http://tempuri.org/'.
To make service conformant please make sure that all web methods belonging to
the same binding have unique names.
After reading this bulky error message it can be concluded that finger is pointed toward following line in our code
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
WS-I-BASIC PROFILE 1.0
specification seems to not allowing our beloved method overloading lets not
confirm to it.
Modify this line as bellow
[WebServiceBinding(ConformsTo
= WsiProfiles.None)]
Now rerun now you should be
able to consume this webservice without any error which has method overloading.
Now let’s analyze why we have to improvise our
code?
The WS-I
organization (Web Services Interoperability Organization) publishes non-proprietary Web
services specifications to promote the interoperability of Web services across
platforms. WsiProfiles
enumeration is used by the ConformsTo property of the WebServiceBindingAttribute
attribute to indicate the WSI specification to which the Web service claims to
conform.
WS-I Basic Profile 1.0 specification
relates to conformant use of WSDL to describe a web service. To go deep into
this standard read reverences bellow.
References:
No comments:
Post a Comment