匯出 (0) 列印
全部展開

組態連接字串

更新日期: 2015年3月

Microsoft Azure 支援新的一組受管理 API,其會提供一致的方式,跨 Microsoft 雲端服務建立 Azure 服務用戶端 (例如 服務匯流排) 的新執行個體。API 可讓您具現化這些用戶端 (例如,CloudBlobClientSubscriptionClientTopicClient),而不管是在何處管埋理應用程式:現場、Microsoft 雲端服務、網站或永久的 VM 角色。您也可以使用這些 API,擷取具現化這些用戶端所需的組態資訊,以及變更組態,而不必重新部署呼叫中應用程式。API 位於 CloudConfigurationManager 類別中。

若要具現化用戶端 (例如 QueueClient),您可以將組態資訊表示為連線字串。Azure 支援從組態檔擷取連線字串的 CloudConfigurationManager 類別。在用戶端上,對於每一個不同類型的用戶端,皆有一個使用該連接字串具現化該用戶端類型的方法。若是 服務匯流排,這些方法如下:

例如,假設下列組態區段:

<appSettings>     <add key="Microsoft.ServiceBus.ConnectionString"          value="Endpoint=sb://[namespace].servicebus.windows.net; SharedSecretIssuer=owner;SharedSecretValue=someSecret"/> </appSettings>

下列程式碼會擷取連接字串、建立佇列,以及初始化佇列的連接:

QueueClient Client;   string connectionString = AzureConfigurationManager.AppSettings.GetSetting("Microsoft.ServiceBus.ConnectionString"); var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);   if (!namespaceManager.QueueExists(QueueName))     {         namespaceManager.CreateQueue(QueueName);     }  // Initialize the connection to Service Bus Queue Client = QueueClient.CreateFromConnectionString(connectionString, QueueName); 

除了在設定檔中使用連接字串來連接到 服務匯流排,有時您也必須明確指定連接到 服務匯流排 所需的輸入參數。服務匯流排 定義協助程式類別 ServiceBusConnectionStringBuilder,可讓您在程式碼中建置連接字串。

在程式碼中,識別連接參數 (使用組態檔或其他方法)。然後,使用 ServiceBusConnectionStringBuilder 協助程式類別來連接到 服務匯流排。例如,下列程式碼可建立連接字串供 Microsoft Azure 服務匯流排 使用:

var serviceConnection = new ServiceBusConnectionStringBuilder(); serviceConnection.Endpoints.Add(new Uri("sb://test-namespace.servicebus.windows.net")); serviceConnection.SharedSecretIssuerName = "owner"; serviceConnection.SharedSecretIssuerSecret = "0dteeO3/apkXXXXXXXXXXXXXXXXXXXXXXXXXXXT2G0="; 

下列程式碼會建立具有 SAS 金鑰的連線字串:

QueueClient client = QueueClient.CreateFromConnectionString("Endpoint=sb://[namespace].servicebus.windows.net/ServiceBusDefaultNamespace; RuntimePort=9354;ManagementPort=9355; SharedAccessKeyName=sasrulename;SharedAccessKey=saskey"); 

ServiceBusConnectionStringBuilder 協助程式類別可讓您為數個不同的情況建立連接字串。下列幾節包含範例。

下列範例建置連接字串,格式為

“Endpoint=sb://fjprod-tat-001-monitor1.servicebus.wa.fj-cloud.net/;StsEndpoint=https://fjprod-tat-001-monitor1-sb.accesscontrol.wa.fj-cloud.net/;SharedSecretIssuer=owner;SharedSecretValue=0dteeO3/apkXXXXXXXXXXXXXXXXXXXXXXXXXXXT2G0=”
var serviceCustomConnection = new ServiceBusConnectionStringBuilder(); serviceCustomConnection.Endpoints.Add(new Uri("sb://fjprod-tat-001-monitor1.servicebus.wa.fj-cloud.net/")); serviceCustomConnection.StsEndpoints.Add(new Uri("https://fjprod-tat-001-monitor1-sb.accesscontrol.wa.fj-cloud.net/")); serviceCustomConnection.SharedSecretIssuerName = "owner"; serviceCustomConnection.SharedSecretIssuerSecret = "0dteeO3/apkXXXXXXXXXXXXXXXXXXXXXXXXXXXT2G0=";

下列範例建置連接字串,格式為

"Endpoint=sb://machine1/test-namespace;StsEndpoint=https://machine1:9355/test-namespace;RuntimePort=9354;ManagementPort=9355"
var serverLogonConnection = new ServiceBusConnectionStringBuilder(); serverLogonConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace")); serverLogonConnection.StsEndpoints.Add(new Uri("https://machine1:9355/test-namespace")); serverLogonConnection.RuntimePort = 9354; serverLogonConnection.ManagementPort = 9355;

下列範例建置連接字串,格式為

"Endpoint=sb://machine1/test-namespace,sb://machine2/test-namespace;StsEndpoint=https://machine1:9355/test-namespace,https://machine2:9355/test-namespace;RuntimePort=9354;ManagementPort=9355"
var serverMultiEndpointsConnection = new ServiceBusConnectionStringBuilder(); serverMultiEndpointsConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace")); serverMultiEndpointsConnection.Endpoints.Add(new Uri("sb://machine2/test-namespace")); serverMultiEndpointsConnection.StsEndpoints.Add(new Uri("https://machine1:9355/test-namespace")); serverMultiEndpointsConnection.StsEndpoints.Add(new Uri("https://machine2:9355/test-namespace")); serverMultiEndpointsConnection.RuntimePort = 9354; serverMultiEndpointsConnection.ManagementPort = 9355;

下列範例建置連接字串,格式為

"Endpoint=sb://machine1/test-namespace;StsEndpoint=https://machine1:9355/test-namespace;RuntimePort=9354;ManagementPort=9355;WindowsUsername=testUser;WindowsDomain=<Your machine name>;WindowsPassword=password"
var serverLocalUserConnection = new ServiceBusConnectionStringBuilder(); serverLocalUserConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace")); serverLocalUserConnection.StsEndpoints.Add(new Uri("https://machine1:9355/test-namespace")); serverLocalUserConnection.RuntimePort = 9354; serverLocalUserConnection.ManagementPort = 9355; serverLocalUserConnection.WindowsCredentialDomain = Environment.MachineName; serverLocalUserConnection.WindowsCredentialUsername = "testUser"; serverLocalUserConnection.WindowsCredentialPassword = securePassword;

下列範例建置連接字串,格式為

"Endpoint=sb://machine1/test-namespace;StsEndpoint=https://machine1:9355/test-namespace;RuntimePort=9354;ManagementPort=9355;WindowsUsername=testUser;WindowsDomain=redmond;WindowsPassword=password"
var serverDomainConnection = new ServiceBusConnectionStringBuilder(); serverDomainConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace")); serverDomainConnection.StsEndpoints.Add(new Uri("https://machine1:9355/test-namespace")); serverDomainConnection.RuntimePort = 9354; serverDomainConnection.ManagementPort = 9355; serverDomainConnection.WindowsCredentialDomain = "redmond"; serverDomainConnection.WindowsCredentialUsername = "testUser"; serverDomainConnection.WindowsCredentialPassword = securePassword;

下列範例建置連接字串,格式為

"Endpoint=sb://machine1/test-namespace;RuntimePort=9354;ManagementPort=9355"
var serverAnonymousConnection = new ServiceBusConnectionStringBuilder(); serverAnonymousConnection.Endpoints.Add(new Uri("sb://machine1/test-namespace")); serverAnonymousConnection.RuntimePort = 9354; serverAnonymousConnection.ManagementPort = 9355;

另請參閱

顯示:
© 2015 Microsoft