开发者

WCF Best Practice for "Overloaded" methods

开发者 https://www.devze.com 2022-12-29 16:45 出处:网络
What is the best practice for emulating overloaded methods over WCF? Typically I might write an interface like this

What is the best practice for emulating overloaded methods over WCF?

Typically I might write an interface like this

interface IInterface 
{
    MyType ReadMyType(int id);
    IEnumerable<MyType> ReadMyType(String name);
    IEnumerable<MyType> ReadMyType(String name, int maxResults);    
}

What would this interfac开发者_JAVA技巧e look like after you converted it to WCF?


You can leave it like that if you like. Just use the name property of the OperationContract attribute.

[ServiceContract]
interface IInterface 
{
    MyType ReadMyType(int id);
    [OperationContract(Name= "Foo")]
    IEnumerable<MyType> ReadMyType(String name);
    [OperationContract(Name= "Bar")]
    IEnumerable<MyType> ReadMyType(String name, int maxResults);    
}


As mwilson already said - WCF doesn't allow methods to have the same name in the service definition (the WSDL).

If you have two or more (overloaded) methods with the same name in .NET, you need to disambiguate them for the WCF service definition by specifying a Name= on the [OperationContract] attribute for each method.

Remember: WCF is not .NET (or not .NET alone) - it's an interoperable standard, and the WSDL standard does not currently support method overloading - each method must be uniquely identifyable by name.

0

精彩评论

暂无评论...
验证码 换一张
取 消