Web services 服务中具有相同名称的两个操作

Web services 服务中具有相同名称的两个操作,web-services,wcf,Web Services,Wcf,我可以在同一个服务中有两个名称相同(但签名不同)的操作吗 例如: [ServiceContract] public interface IMyService { [OperationContract] bool MyOperation(string target); [OperationContract] bool MyOperation(List<string> targets); } [服务合同] 公共接口IMyService{ [经营合同]

我可以在同一个服务中有两个名称相同(但签名不同)的操作吗

例如:

[ServiceContract] 
public interface IMyService {
    [OperationContract]
    bool MyOperation(string target);

    [OperationContract]
    bool MyOperation(List<string> targets); }
[服务合同]
公共接口IMyService{
[经营合同]
布尔MyOperation(字符串目标);
[经营合同]
bool MyOperation(列出目标);}
我真的需要支持不同的签名,因为我有几个团队使用我的服务,只有一个团队需要第二个签名(我不希望团队的其他成员不得不更改他们的代码)


有什么想法吗?

没有,您确实不能对合同进行相同的命名操作,但您可以将它们在两个合同中分开:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    bool MyOperation(string target);
}

[ServiceContract]
public interface IMyServiceV2
{
    [OperationContract]
    bool MyOperation(List<string> targets);
}


[ServiceContract]
public class MyService : IMyService, IMyServiceV2
{
    public bool MyOperation(string target)
    {
        /* your code*/
        return true;
    }

    public bool MyOperation(List<string> targets)
    {
        /* your code*/
        return true;
    }
}
[服务合同]
公共接口IMyService
{
[经营合同]
布尔MyOperation(字符串目标);
}
[服务合同]
公共接口IMyServiceV2
{
[经营合同]
bool MyOperation(列出目标);
}
[服务合同]
公共类MyService:IMyService,IMyServiceV2
{
公共布尔MyOperation(字符串目标)
{
/*你的代码*/
返回true;
}
公共bool MyOperation(列出目标)
{
/*你的代码*/
返回true;
}
}
并公开两个端点:

<services>
  <service name="YourNamespace.MyService ">
    <endpoint
        address="http://localhost:8000/v1"
        binding="webHttpBinding" 
        contract="YourNamespace.IMyService" />

    <endpoint
        address="http://localhost:8000/v2"
        binding="webHttpBinding" 
        contract="YourNamespace.IMyServiceV2" />
  </service>
</services>


或者,您可以为OperationContract设置名称参数,但对于SOA服务,其结果将与重命名函数名相同。

不要使用值类型参数。您可以为您的
MyOperationRequest
datacontract提供两个属性(
string Target
List Targets
)或类似的内容,并检查设置了哪个属性。或者尝试仅使用
列表
,请求至少设置了一项。我知道数据契约,但我正在进行代码维护。代码非常古老,是由其他团队开发的。我无法更改当前接口以使用数据契约(它会影响其他团队),但需要支持新的接口(对于请求它的团队)。我想唯一的办法就是换个名字。谢谢你的时间:)谢谢。在我的情况下,这不是一个选项,但在任何其他情况下它都是有效的。@jpaires您预期的结果是什么?一个endpooint(一个地址),但client1可以访问Method1,client2可以访问Method2?预期结果是一个端点,客户端根据给定的参数集使用方法(参数1的列表)或方法(参数2的列表)。这就像操作的方法重载一样。@jpaires在您的案例中,只能对操作契约属性使用Name属性(如CodeCaster提供的链接中所述)。因此,SOAP消息中的操作名称将替换为您在操作的名称属性中设置的名称。