[英]How to call a service using multiple Endpoint URIs
放到上下文中,我有一个客户端应用程序,它将尝试调用将部署在多个Web服务器上的Web服务。 URI列表将从客户端的Settings.settings
文件中获取,并且foreach循环将循环遍历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
类,一切都是有效的。 我现在不知道我在想什么...有什么想法吗?
该错误告诉您确切的问题所在:没有名称为BasicHttpBinding_IWorkloadMngrService
终结点。 app.config表示端点称为BasicHttpBinding_ICMMSManagerService
因此您的代码应为:
var client = new CMMSManagerServiceClient("BasicHttpBinding_ICMMSManagerService");
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.