WCF“重载”方法的最佳实践

时间:2021-03-19 10:56:03

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

在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?

将此接口转换为WCF后,该界面会是什么样子?

2 个解决方案

#1


10  

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

如果你愿意,你可以这样离开。只需使用OperationContract属性的name属性即可。

[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);    
}

#2


5  

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

正如mwilson所说 - WCF不允许方法在服务定义(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.

如果在.NET中有两个或多个(重载)方法具有相同的名称,则需要通过在每个方法的[OperationContract]属性上指定Name =来为WCF服务定义消除歧义。

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.

记住:WCF不是.NET(或者不是.NET) - 它是一个可互操作的标准,WSDL标准目前不支持方法重载 - 每个方法必须按名称唯一标识。

#1


10  

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

如果你愿意,你可以这样离开。只需使用OperationContract属性的name属性即可。

[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);    
}

#2


5  

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

正如mwilson所说 - WCF不允许方法在服务定义(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.

如果在.NET中有两个或多个(重载)方法具有相同的名称,则需要通过在每个方法的[OperationContract]属性上指定Name =来为WCF服务定义消除歧义。

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.

记住:WCF不是.NET(或者不是.NET) - 它是一个可互操作的标准,WSDL标准目前不支持方法重载 - 每个方法必须按名称唯一标识。