Azure 网站上的 WCF 服务地址

WCF Service Address on Azure Website

我计划 运行 在 Azure 网站上使用 netTcpRelayBinding 的 WCF 服务。

Azure 网站允许的一项有趣功能是更新应用程序设置并在实时和暂存环境之间切换。

我遇到的问题是当我从暂存切换到生产时如何更改服务总线地址,反之亦然。我认为实现的一种方法是在 appsettings 中设置服务地址。

注意:我可以通过 web.config 转换和重新部署来实现。但是,我们正在努力避免重新部署。

示例问题:

<services>
  <service name="Hwo.LocationService.Wcf.HotelLocationService">
    <endpoint                   address="sb://staging.servicebus.windows.net/IHotelLocationService"
              binding="netTcpRelayBinding" 
              contract="Hwo.ProductInterface.Common.Azure.Contracts.IHotelLocationService"
              name="HotelLocationServiceEndPoint" />
  </service>

...

如上所示,地址指向暂存环境。但是当我们在生产中交换那个应用程序时,地址不会改变。我们希望在交换时更改该端点地址。

这可能吗?

谢谢。

由于可以调换App设置,所以我建议你可以将你的Address值写入App设置中的web.config文件中。同时,您需要读取地址值并将其设置为代码中的服务端点。如果交换插槽,只需修改 Azure 门户上的应用程序设置。

读取配置:

WebConfigurationManager.AppSettings["configFile"]  

修改设置:

[更新] 请参考此文档在不同的环境中动态设置您的端点(https://blogs.msdn.microsoft.com/carlosfigueira/2011/06/13/wcf-extensibility-servicehostfactory/)

在 Will 的建议下,我成功地编写了服务主机工厂,完美地解决了这个问题。

代码如下:

protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var serviceHost = new ServiceHost(serviceType, baseAddresses);
        var serviceEndpoint = serviceHost.AddServiceEndpoint(serviceType.GetInterfaces().First(),
            new NetTcpRelayBinding("netTcpRelayBinding"),
            ConfigurationManager.AppSettings["ServiceBus.Address"] + serviceType.Name);
        serviceEndpoint.Behaviors.Add(
            new TransportClientEndpointBehavior(
                TokenProvider.CreateSharedAccessSignatureTokenProvider(
                    ConfigurationManager.AppSettings["ServiceBus.Key"],
                    ConfigurationManager.AppSettings["ServiceBus.Value"])));
        return serviceHost;
    }

谢谢。