tags:

views:

138

answers:

2

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 interface look like after you converted it to WCF?

+4  A: 

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);    
}
mwilson
What would that look like I'm not familiar with that?
Nate Bross
This works, although this can be really painful to keep up with, trace, debug, etc... Depending on the connecting products (E.g. Basic Profile, etc) you can still run in to problems. Although it feels strange to class developers it may be better to just use more expressive names and not have to maintain two seperate taxonomies
JoeGeeky
A: 

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.

marc_s