要放入上下文,我有一個客戶端應用程序,它將嘗試調用將部署在多個Web服務器上的Web服務。 URI列表將從客戶端的Settings.settings
文件獲得,並且foreach循環將遍歷URI直到可用服務響應。如何使用多個端點URI調用服務
比方說,我有以下的合同服務:
[ServiceContract]
public interface ICMMSManagerService
{
[OperationContract]
ServerInfo GetServerInfo(string systemNumber);
}
在服務的項目的web.config中,我已經定義CMMSManager
服務與端點名稱:BasicHttpBinding_IWorkloadMngrService
<system.serviceModel>
<services>
<service name="WorkloadMngr">
<endpoint binding="basicHttpBinding" contract="IMetadataExchange" />
</service>
<service name="CMMSManager">
<endpoint binding="basicHttpBinding" contract="IMetadataExchange" name="BasicHttpBinding_IWorkloadMngrService" />
</service>
</services>
<client>
<remove contract="IMetadataExchange" name="sb" />
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding>
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
在客戶端,我在應用程序啓動時執行以下代碼:
private void QueryWebServiceUrls()
{
var webServiceUrls = Properties.Settings.Default.WebServiceUrls;
foreach (var webServiceUrl in webServiceUrls)
{
try
{
var client = new CMMSManagerServiceClient("BasicHttpBinding_IWorkloadManagerService");
client.Endpoint.Address = new EndpointAddress(new Uri(webServiceUrl),
client.Endpoint.Address.Identity, client.Endpoint.Address.Headers);
client.Open();
var result = client.GetServerInfo("test");
}
catch (EndpointNotFoundException e)
{
continue;
}
catch (InvalidOperationException e)
{
break;
}
}
}
但是當CMMSManagerServiceClient
類被實例化時,應用程序崩潰了InvalidOperationException
。
未能找到在ServiceModel客戶 配置部分名稱 「BasicHttpBinding_IWorkloadMngrService」和合同 「ComClientService.ICMMSManagerService」終結點元素。這可能是因爲沒有爲您的應用程序找到配置文件 ,或者因爲在客戶端元素中找不到此名稱匹配的端點元素 。
我有以下的配置在App.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICMMSManagerService">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/WorkloadMngr/CMMSManagerService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICMMSManagerService"
contract="ComClientService.ICMMSManagerService" name="BasicHttpBinding_ICMMSManagerService" />
</client>
</system.serviceModel>
我以爲一切都是由BasicHttpBinding_ICMMSManagerService
參數傳遞給CMMSManagerServiceClient
類有效。我不知道我現在缺少什麼......有什麼想法?