【问题标题】:Configure WCF client endpoint to use a relative address (for Azure)将 WCF 客户端终结点配置为使用相对地址(适用于 Azure)
【发布时间】:2013-02-08 05:09:35
【问题描述】:

我有一个包含多个 Web 角色的 Windows Azure 云服务项目来托管一些服务。我想在 ServiceY 中使用 ServiceX(每个都在 不同的角色上运行),使用 relative URL。

这就是我托管 ServiceX 的方式:

<service name="ServiceX">
    <endpoint address="" binding="basicHttpBinding" contract="ServiceX" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

现在我想在 ServiceY 中使用该服务。使用绝对 URL 可以正常工作:

<system.serviceModel>
    <client>
      <endpoint name="ServiceXProxy"
          address="http://mycloudservice.cloudapp.net:8080/ServiceX.svc"
          binding="basicHttpBinding"
          contract="ServiceX"/>
...

但是如何在 ServiceY 中使用带有相对地址的 ServiceX?这不可能,因为它们运行在同一个云服务上?

【问题讨论】:

    标签: c# wcf azure wcf-binding basichttpbinding


    【解决方案1】:

    您可以通过编程方式使用相对地址,但您仍然需要知道基地址(或仅使用 localhost:8080 作为基地址) - 无法通过 web.config 使用相对地址除非您构建自定义配置或利用 AppSettings

    // create bindings & endpoints
    var baseAddress = System.ConfigurationManager.AppSettings["baseAddress"];
    var binding = new System.ServiceModel.BasicHttpBinding();
    var endpoint = new EndpointAddress(baseAddress + "/ServiceX.svc");
    

    您还可以从web.config 加载客户端端点地址,并使用UriBuilder 覆盖基地址以实现类似方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 2013-03-18
      • 1970-01-01
      • 2012-08-25
      • 2012-02-07
      • 2011-07-03
      • 2019-10-05
      相关资源
      最近更新 更多