Service Bus for Windows Server 初用問題小結


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:

image

 

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:                 });


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM