【问题标题】:How to call a service using multiple Endpoint URIs如何使用多个端点 URI 调用服务
【发布时间】:2014-09-10 13:50:47
【问题描述】:

具体来说,我有一个客户端应用程序,它将尝试调用将部署在多个 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

找不到具有名称的端点元素 'BasicHttpBinding_IWorkloadMngrService' 和合同 ServiceModel 客户端中的“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 类,一切都是有效的。我不知道我现在错过了什么......有什么想法吗?

【问题讨论】:

    标签: c# web-services wcf endpoint


    【解决方案1】:

    错误告诉您到底出了什么问题:没有名称为 BasicHttpBinding_IWorkloadMngrService 的端点。 app.config 说端点被称为BasicHttpBinding_ICMMSManagerService 所以你的代码应该是:

    var client = new CMMSManagerServiceClient("BasicHttpBinding_ICMMSManagerService");
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 2020-02-03
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      相关资源
      最近更新 更多