Azure 服务总线是你的朋友。您真正想要做的是启用服务总线中继,具体如下所述:
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 和遍历您的基础设施。
服务总线支持多种中继绑定,如下所述:http: //msdn.microsoft.com/en-us/library/windowsazure/hh410102.aspx。
我的博客上有一个关于 Service Bus 的演示文稿,示例代码有两个项目专门演示了中继。查看http://www.stratospher.es/blog/post/slides-and-code-from-dallas-july-10-2012-windows-azure-cloud-summit并在那里下载示例代码。您想要的两个项目是“RelayDemoServer”和“RelayDemoClient”。示例代码在代码中配置继电器,但如果这更符合您的风格,您也可以通过配置来执行此操作。
简而言之,您基本上必须在服务器端(在您的基础结构中)为您想要的服务创建 WCF 端点,然后在服务总线中创建等效的中继绑定。一旦发生这种情况,您最终将获得内部端点的服务总线托管版本。它看起来像这样:
// 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));
}
秘诀是对 CreateServiceUri 的调用,它计算出 SB 托管的 URI。
希望这对您有所帮助,但如果您需要更多信息,请告诉我,
亚当霍夫曼
Windows Azure 博客 - http://stratospher.es
推特 - http://twitter.com/stratospher_es