[英]WCF: The contract 'X' in client configuration does not match the name in service contract
當我嘗試在VS中運行WCF服務時,此錯誤消息顯示,並且我試圖弄清楚“客戶端配置”和“服務合同”實際上指的是什么:
客戶端配置中的合同“ IMyService”與服務合同中的名稱不匹配
我認為服務合同部分是指:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]
public interface IMyService
{
// CODEGEN: Generating message contract since the operation MyService is neither RPC nor document wrapped.
[System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")]
[System.ServiceModel.XmlSerializerFormatAttribute()]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(Task))]
SendResponse Request(SendRequest request);
}
關於客戶端配置所指的任何想法?
編輯:在我的web.config中,我有system.serviceModel的這一部分:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MyServiceBinding">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="XXX.YYY.MyService">
<endpoint binding="basicHttpBinding" bindingConfiguration="MyServiceBinding" name="MyServiceSendHttps"
contract="IMyService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost" />
</baseAddresses>
</host>
</service>
</services>
我遇到了同樣的問題,我花了很多時間尋找解決方案。 然后,我發現了有關WCF工具svcutil.exe生成的代碼的本文 。
不能保證生成的C#代碼也適用於服務端合同。 就我而言,問題出在ReplyAction =“ *”(在問題中我也看到了)。 根據MSDN文檔 :
在服務中指定一個星號會指示WCF不要在消息中添加回復動作,如果您直接針對消息進行編程,這將非常有用。
變更后
[System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")]
至
[System.ServiceModel.OperationContractAttribute(Action = "")]
問題解決了。
請參閱項目中的app.config文件。 如果您未通過編程方式配置客戶端,則app.config文件必須包含客戶端配置節點。
更新:您的第一個代碼段包括以下行:
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]`.
在“ ConfigurationName”屬性的文檔中: https : //msdn.microsoft.com/zh-cn/library/system.servicemodel.servicecontractattribute.configurationname%28v=vs.110%29.aspx,我們可以閱讀:
用於在應用程序配置文件中定位服務元素的名稱。 缺省值為服務實現類的名稱。
因此,我們有:服務實現類的名稱為“ XXX.YYY.MyService”,並且(在第二個代碼段中)我們看到“ <service name =“ XXX.YYY.MyService”>”,但屬性的ConfigurationName值為“ IMyService”。 ”。
如果只是從行中刪除“ ConfigurationName =“ IMyService”
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]
像這樣:
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy")]
應該可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.