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.
精彩评论