Service Bus for Windows Server 初用問題小結

來源:互聯網
上載者:User
文章目錄
  • 1.樣本下載
  • 2.程式集引用
  • 3.關於串連
  • 3.遠端存取

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:                 });
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.