我遇到了問題,使用我的c#應用程序設置Microsoft Translator SOAP Service,而不依賴於它生成的app.config文件。沒有配置文件的WCF /服務參考
app.config文件包含以下內容:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_LanguageService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://api.microsofttranslator.com/V2/soap.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_LanguageService"
contract="Soap.LanguageService" name="BasicHttpBinding_LanguageService" />
</client>
</system.serviceModel>
</configuration>
在參考這個Stack answer,我用下面的方法:
internal static Soap.LanguageServiceClient CreateWebServiceInstance()
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "BasicHttpBinding_LanguageService";
binding.TextEncoding = System.Text.Encoding.UTF8;
return new Soap.LanguageServiceClient(binding, new EndpointAddress("http://api.microsofttranslator.com/V2/soap.svc"));
}
然而,即使我打電話CreateWebServiceInstance()
之前,我執行翻譯服務,我得到這個未處理的異常:
Could not find default endpoint element that references contract 'Soap.LanguageService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
我不熟悉BasicHttpBinding
,所以我不知道從哪裏開始...
感謝您的回覆。原因是因爲我不想用這個單獨的配置文件運送我的應用程序;在發佈之後,我預計終端地址不會改變。 我到底該如何定義合同?我嘗試添加'binding.Contract = ...',但它給'Contract'引發了一個錯誤'定義'並且沒有擴展方法'Contract' – Spencer