WCF分布式开发常见错误解决(7):Cannot have two operations in the same contract

本文介绍了解决WCF服务中因契约方法名重复导致的异常问题的方法。通过更改方法名或使用OperationContractAttribute的Name属性来区分同名操作契约。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   我们启动服务宿主程序的时候,有可能出现如下的无效操作异常,信息如下:
Cannot have two operations in the same contract with the same name, methods SayHello and SayHello in type WCFService.IWCFService violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.异常信息截图:
    原因:这个是由于服务契约里定义了定义了两个相同名称的操作契约。
解决办法:
1.重新定义操作契约的名称,使两者不同;
2.或者使用操作契约的名称属性,实例代码如下:
    //1.服务契约,操作契约重载
    [ServiceContract(Namespace = "http://www.cnblogs.com/frank_xl/")]
    
public interface IWCFService
    {
        
//操作契约
        [OperationContract(Name = "SayHello1")]
        
string SayHello();
        
//操作契约
        [OperationContract(Name = "SayHello2")]
        
string SayHello(string name);
        
//操作契约
        [OperationContract(Name = "SayHello3")]
        
string SayHello(string firstName, string lastName);

    }
 
 重新编译运行代码即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值