Service Bus 1.0 安装说明 中演示了Service Bus for Windows Server的安装过程,但是在使用过程中,Service Bus 也有一些注意事项,下文中会讲解几点
1.示例下载
在 http://servicebus.codeplex.com 中有Service Bus的使用示例,不过仅是Azure版本的。
2.程序集引用
引用Microsoft.ServiceBus.dll可以在"X:\Program Files\Service Bus\1.0\Microsoft.ServiceBus.dll"找到,当然,也可以使用NuGet来获取Windows Azure Service Bus:
3.关于连接
Service Bus for Windows Server 在代码编写上的主要不同主要集中在ConnectionString的编写上.在Service Bus for Windows Azure中通常使用以下代码来进行NamespaceManager的构造:
1: TokenProvider credentials = TokenProvider.CreateSharedSecretTokenProvider(Sender.IssuerName, Sender.IssuerKey);
2: Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", Sender.ServiceNamespace, string.Empty);
3: NamespaceManager namespaceClient = new NamespaceManager(serviceUri, credentials);
不过在Service Bus for Windows Server 中,可以使用ServiceBusConnectionStringBuilder类来构造ConnectionString:
1: string ServerFQDN = "hostname";
2: int HttpPort = 9355;
3: int TcpPort = 9354;
4: string ServiceNamespace = "NameSpace";
5: ServiceBusConnectionStringBuilder connBuilder = new ServiceBusConnectionStringBuilder();
6: connBuilder.ManagementPort = HttpPort;
7: connBuilder.RuntimePort = TcpPort;
8: connBuilder.Endpoints.Add(new UriBuilder() {Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace}.Uri);
9: connBuilder.StsEndpoints.Add(new UriBuilder() {Scheme = "https", Host = ServerFQDN, Port = HttpPort, Path = ServiceNamespace}.Uri);
10: NamespaceManager namespaceClient = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());
这样就可以连接到本地的ServiceBus服务并使用Queue及Topic了
3.远程访问
防火墙注意打开 9002-9004 9354-9356
并且NamespaceManager、MessagingFactory初始化时要指定TokenProvider
官司方文档中说可以通过证书或IssueUser来验证,但是我这里都没有尝试成功。只通过OAuth验证通过了。
1: var tokenProvider = TokenProvider.CreateOAuthTokenProvider(
2: new[] { new UriBuilder() { Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace, Port = HttpPort }.Uri },
3: new NetworkCredential("windows user", "windows user password"));
4: namespaceClient.Settings.TokenProvider = tokenProvider;
至于 MessagingFactory可以通过以下方法获得带有TokenProvider的实例
1: var runtimeAddress = string.Format("sb://{0}:{2}/{1}/", ServerFQDN, ServiceNamespace, TcpPort);
2: var factory = MessagingFactory.Create(runtimeAddress,
3: new MessagingFactorySettings()
4: {
5: TokenProvider = tokenProvider,
6: OperationTimeout = TimeSpan.FromMinutes(30)
7: });
时间: 2024-10-15 09:12:17