如何确定Azure Service Bus中客户端可用的通道

时间:2012-07-22 13:07:54

标签: azure servicebus

您好,

我正在使用Azure AppFabric Service Bus构建程序。我的目的是通过服务总线使用相同的命名空间暴露多个设备。每个设备使用“ServiceBusEnvironment.CreateServiceUri”方法通过服务总线创建一个具有不同“服务路径”的连接。示例:

第一个主机:

Uri relayAddress1 = ServiceBusEnvironment.CreateServiceUri(“https”,“MyNamespace”,“MyServicePath1” “);

第二个主持人:

Uri relayAddress2 = ServiceBusEnvironment.CreateServiceUri(”https“,”MyNamespace“,”MyServicePath2“);

我想知道是否可以从服务总线检索哪些主机连接可用。我可以想象“MyServicePath1”,“MyServicePath2”和“MyServicePath4”可用,但“MyServicePath3”不可用。在客户端,我可以尝试向每个主机打开4个通道并捕获CommunicationException以确定主机是否可用,但这并不是非常有效。有没有办法实现这一目标而不会出现异常?

提前致谢,

Stan Verdiesen

2 个答案:

答案 0 :(得分:2)

嗨Sandrino Di Mattia,

感谢您的快速回复。我已成功尝试了您的解决方案!再次感谢。

为了完整性,我的实施:

服务主机上的DiscoveryMode属性:

        Uri relayAddress = ServiceBusEnvironment.CreateServiceUri(Uri.UriSchemeHttps, "MyNamespace", "MyServicePath1");

        BasicHttpRelayBinding relayBinding = new BasicHttpRelayBinding();

        ServiceHost serviceHost = new ServiceHost(typeof(ExternalDataExchangerTestServer));
        ServiceEndpoint endPoint = serviceHost.AddServiceEndpoint(typeof(IExternalDataExchanger), relayBinding, relayAddress);

        endPoint.Behaviors.Add(new TransportClientEndpointBehavior { TokenProvider = TokenProvider.CreateSharedSecretTokenProvider("YourName", "YourKey") });
        endPoint.Behaviors.Add(new ServiceRegistrySettings { DiscoveryMode = DiscoveryType.Public });

        serviceHost.Open();

通过代码读取原子Feed的解决方案:

  public static List<string> GetAvailableServiceBusHosts(string serviceNamespace)
  {
     if (serviceNamespace == null)
        throw new ArgumentNullException("serviceNamespace");

     var relayAddress = ServiceBusEnvironment.CreateServiceUri(Uri.UriSchemeHttps, serviceNamespace, string.Empty);

     var availableHosts = new List<string>();

     var httpWebRequest = (HttpWebRequest)WebRequest.Create(relayAddress);

     httpWebRequest.Proxy = null;
     httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
     httpWebRequest.Method = WebRequestMethods.Http.Get;

     using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
     {
        using (var responseStream = httpWebResponse.GetResponseStream())
        {
           if (responseStream == null)
              throw new NullReferenceException("responseStream");

           var atomFormatter = new Atom10FeedFormatter();

           var xmlReader = XmlReader.Create(responseStream);
           atomFormatter.ReadFrom(xmlReader);

           var syndicationFeed = atomFormatter.Feed;

           foreach (var syndicationItem in syndicationFeed.Items)
           {
              availableHosts.Add(syndicationItem.Title.Text);
           }

           return availableHosts;
        }
     }
  }

我希望这有助于其他人!

答案 1 :(得分:1)

如果您将 ServiceRegistrySettings DiscoveryMode设置为公开,则您的服务将在原子Feed中提供。完成此操作后,您可以通过访问命名空间URL获取服务列表:http:// service-namespace .servicebus.windows.net /

参考:How to: Discover and Expose a Service Bus Application