文章目錄
- 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: });