.net - 将 Azure 网站连接到本地 WCF 服务

标签 .net wcf azure

我希望构建一个混合应用程序,通过 Azure 网站在防火墙后面公开托管在本地的特定 WCF 服务。 Azure 网站将简单地调用我的 WCF 服务来获取和放置数据。

我曾以为有一种方法可以通过 Azure 连接到 Azure 服务来连接本地特定的 WCF 服务,但现在我已将其安装在网络上的沙箱计算机上,我不确定情况是否如此。我希望能够将 Azure 和网络之间的连接锁定为仅限我指定的 WCF 服务,以提高安全性。

有哪些选项可以让我的 WCF 服务向我的 Azure 网站公开?

最佳答案

Azure 服务总线是您的 friend 。您真正想要做的是启用服务总线中继,具体如下所述:

http://www.windowsazure.com/en-us/develop/net/how-to-guides/service-bus-relay/

此外,您环境中的服务必须通过防火墙将自身暴露给服务总线。您可以在这里找到一些方便的提示(http://msdn.microsoft.com/en-us/library/windowsazure/ee706729.aspx),尽管通常情况下,注册服务总线的简单行为往往会向 NAT 提供足够的信息,并且遍历您的基础设施。

服务总线支持多种中继绑定(bind),如下所示:http://msdn.microsoft.com/en-us/library/windowsazure/hh410102.aspx

我的博客上有一个关于服务总线的演示,示例代码有两个专门演示中继的项目。看看http://www.stratospher.es/blog/post/slides-and-code-from-dallas-july-10-2012-windows-azure-cloud-summit并在那里下载示例代码。您想要的两个项目是“RelayDemoServer”和“RelayDemoClient”。示例代码在代码中配置中继,但如果您更喜欢这种风格,您也可以通过配置来执行此操作。

简而言之,您本质上必须在服务器端(在您的基础结构中)为所需的服务创建 WCF 端点,然后在服务总线中创建等效的中继绑定(bind)。一旦发生这种情况,您最终将获得内部端点的服务总线托管版本。它看起来像这样:

// create the host itself...
System.ServiceModel.ServiceHost host =
    new ServiceHost(typeof(ProblemSolver));

// programmatically add the endpoints

// 1. the WCF endpoint "internally"
host.AddServiceEndpoint(
    typeof(IProblemSolver),
    new NetTcpBinding(),
    "net.tcp://localhost:9358/solver"
    );

// 2. the endpoint that is projected back through the service bus (note: NetTcpRelayBinding)
// This one will end up with a DNS name of "sb://[serviceNamespace].servicebus.windows.net/solver"
host.AddServiceEndpoint(
    typeof(IProblemSolver), 
    new NetTcpRelayBinding(),
    ServiceBusEnvironment.CreateServiceUri("sb", Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("ServiceBusNamespace"), "solver"))
        .Behaviors.Add(new TransportClientEndpointBehavior
            {
                TokenProvider = TokenProvider.CreateSharedSecretTokenProvider("owner", Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("ServiceBusSecret"))
            });

host.Open();

然后,在客户端,您只需通过服务总线使用该服务即可。看起来像这样:

var cf = new ChannelFactory<RelayDemoServer.IProblemSolverChannel>(
    new NetTcpRelayBinding(),
    new EndpointAddress(
        ServiceBusEnvironment.CreateServiceUri(
            "sb", 
            Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("ServiceBusNamespace"), 
            "solver"
            )
        )
    );

cf.Endpoint.Behaviors.Add(
    new TransportClientEndpointBehavior 
        { 
            TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(
                "owner", 
                Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("ServiceBusSecret")
                ) 
        }
    );

using (var ch = cf.CreateChannel())
{
    Console.WriteLine(ch.AddNumbers(4, 5));
}

secret 武器是对 CreateServiceUri 的调用,它可以计算出 SB 托管的 URI。

希望这对您有所帮助,但如果您需要更多信息,请告诉我,

亚当·霍夫曼

Windows Azure 博客 - http://stratospher.es

推特 - http://twitter.com/stratospher_es

关于.net - 将 Azure 网站连接到本地 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12479456/

相关文章:

.net - WIF(使用 Thinktecture Identity Server)和双工 WCF channel

asp.net-mvc-4 - 同一台计算机上的两个 Chrome session - 一个将连接到我们的 Azure 网站,另一个 "unable to connect to SQL Server database"

c# - LINQ 过滤对象列表

c# - TopicClient 应该存活多久?

c# - DataTable 未加载 SqlDataReader

c# - wcf "An existing connection was forcibly closed by the remote host"关闭客户端后

wcf - 更改 WCF 服务以要求 SSL

azure - PowerShell 获取 Azure API 报告?

azure - Azure 函数中针对 Cosmos DB 的 SQL 注入(inject)

c# - 你如何在 C# 中找到用户名/身份