你好,
我正在使用 Azure AppFabric 服务总线构建一个程序。我的意图是通过具有相同命名空间的服务总线公开多个设备。每个设备都使用“ServiceBusEnvironment.CreateServiceUri”方法通过具有不同“服务路径”的服务总线创建连接。例子:第一主持人:Uri relayAddress1 = ServiceBusEnvironment.CreateServiceUri("https", "MyNamespace", "MyServicePath1");第二主持人:Uri relayAddress2 = ServiceBusEnvironment.CreateServiceUri("https", "MyNamespace", "MyServicePath2");我想知道是否可以从服务总线检索信息,哪些主机连接可用。我可以想象“MyServicePath1”、“MyServicePath2”和“MyServicePath4”可用,但“MyServicePath3”不可用。在客户端,我可以尝试为每个主机打开 4 个通道并捕获 CommunicationException 以确定主机是否可用,但这感觉效率不高。有没有办法在不捕获异常的情况下实现这一目标?提前致谢,斯坦维尔迪森
问问题
1254 次
2 回答
2
嗨桑德里诺迪马蒂亚,
感谢你的快速回复。我已经成功地尝试了您的解决方案!再次感谢。
为了完整起见,我的实现:
服务主机上的 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();
通过代码读取原子提要的解决方案:
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;
}
}
}
我希望这对其他人有帮助!
于 2012-07-23T09:25:55.207 回答
1
如果您将 ServiceRegistrySettings 的DiscoveryMode设置为Public ,您的服务将在 atom 提要中可用。完成此操作后,您可以通过访问命名空间 url 获取服务列表:http:// service-namespace .servicebus.windows.net/
于 2012-07-22T13:57:10.283 回答