WCF-servicecontract operation overload

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.