1. 程式人生 > 實用技巧 >WCF分散式開發常見錯誤解決(7):Cannot have two operations in the same contract

WCF分散式開發常見錯誤解決(7):Cannot have two operations in the same contract

我們啟動服務宿主程式的時候,有可能出現如下的無效操作異常,資訊如下:
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/")]
publicinterfaceIWCFService
{
//操作契約
[OperationContract(Name="SayHello1")]
stringSayHello();
//操作契約
[OperationContract(Name="SayHello2
")]
stringSayHello(stringname);
//操作契約
[OperationContract(Name="SayHello3")]
stringSayHello(stringfirstName,stringlastName);

}
重新編譯執行程式碼即可。