Method Overloading is common in OOP, but it may be troublesome in WCF. Let's look at the example below.
[Servicecontract]
Public interface icontract
{
Void test (int I );
Void test (string S );
}
Public class myservice: icontract
{
Public void test (int I)
{
Console. writeline (I );
}
Public void test (string S)
{
Console. writeline (s );
}
}
Public class wcftest
{
Public static void test ()
{
Appdomain. createdomain ("server1"). docallback (delegate
{
Servicehost host = new servicehost (typeof (myservice), new uri ("http: // localhost: 8080/myservice "));
Host. addserviceendpoint (typeof (icontract), new basichttpbinding (),"");
Host. open ();
});
Icontract
Channel = channelfactory <icontract>. createchannel (New
Basichttpbinding (), new
Endpointaddress ("http: // localhost: 8080/myservice "));
Using (channel as idisposable)
{
Channel. Test (1 );
}
}
}
An exception occurred when loading the host. The information is as follows:
System. invalidoperationexception not processed
Message = "contractdescription 'icontract 'has zero operations; a contract must have at least one operation ."
Source = "system. servicemodel"
Stacktrace:
In system. servicemodel. description. contractdescription. ensureinvariants ()
Although this exception information is a bit confusing, the problem is actually caused by method overloading. The SOAP message action cannot distinguish between the two methods, so the call target cannot be determined. The solution is simple. Use operationcontract. Name.
[Servicecontract]
Public interface icontract
{
[Operationcontract (name = "test1")]
Void test (int I );
[Operationcontract (name = "Test2")]
Void test (string S );
}
in this way, there will naturally be no two tests in the generated client proxy and message.