アプリケーションの開発

チュートリアルを開始する前に、「Service Bus for Windows Server のインストールと構成」で説明しているように正しく Service Bus for Windows Server をインストールして構成したことを確認します。

サービスの名前空間があることを確認するには

  1. PowerShell コマンド プロンプトで次のコマンドを実行し、少なくとも 1 つのサービスの名前空間が定義されていることを確認します。

    get-SBNamespace
    

    このコマンドからのサンプル出力は次のようになります。

    Name                  : myServiceNamespaceName
    AddressingScheme      : Path
    CreatedTime           : 2/29/2012 11:35:09 PM
    IssuerName            : myServiceNamespaceName
    IssuerUri             : myServiceNamespaceName
    ManageUsers           : {list of user accounts}
    Uri                   : 
    ServiceIdentifier     : 
    PrimarySymmetricKey   : myKey
    SecondarySymmetricKey :
    

    注意

    サービスの名前空間 の詳細情報:、「サービス名前空間」を参照してください。

  2. PowerShell コマンド プロンプトで次のコマンドを実行し、Service Bus for Windows Server クライアント構成ファイルをエクスポートします。

    get-sbClientConfiguration > sbclient.config
    

Visual Studio で新しいプロジェクトを作成する

  1. Visual Studio 2012 で、新しいコンソール アプリケーションを作成します。

  2. ソリューション エクスプローラーで、[参照] を右クリックし、[参照の追加] をクリックします。クリックして System.Runtime.Serialization 項目を追加します。

Service Bus for Windows Server SDK への参照を追加する

  1. ソリューション エクスプローラーで、[参照] を右クリックし、[NuGet パッケージの管理] をクリックします。このオプションが表示されるのは、NuGet 拡張機能がインストールされている場合のみです。

  2. [検索] ボックスに、「Service Bus 1.0」と入力します。[Service Bus 1.0 (Windows Server 用)] 項目をクリックします。インストールを完了し、このダイアログを閉じます。

接続文字列と構成ファイルを使用して Service Bus for Windows Server に接続する

  1. Service Bus for Windows Server を使用すると、追加のパラメーターを指定せずに、構成ファイルに接続文字列を指定してコードで使用することができます。このオプションは Service Bus for Windows Server でサポートされ、柔軟な展開を実現します。接続文字列を使用すると、構成ファイルの設定を変更することで、Service Bus for Windows Server (オンプレミス) から Windows Azure Service Bus (クラウド) に切り替えることができます。

    PowerShell コマンド プロンプトで次のコマンドを実行し、接続文字列を サービスの名前空間 に取得します。

    get-SBClientConfiguration
    

    このコマンドからのサンプル出力は次のようになります。

    Endpoint=sb://yourHost/ServiceBusDefaultNamespace;StsEndpoint=https://yourHost:9355/ServiceBusDefaultNamespace;RuntimePort=9354;ManagementPort=9355
    
  2. アプリケーション構成ファイルを開くか作成し、接続文字列を貼り付けます。次に例を示します。

    <configuration>
      <appSettings>
        <add key="Microsoft.ServiceBus.ConnectionString"         
            value="Endpoint=sb://yourHost/ServiceBusDefaultNamespace;StsEndpoint=https://yourHost:9355/ServiceBusDefaultNamespace;RuntimePort=9354;ManagementPort=9355" />
      </appSettings>
    </configuration>
    

    複数の サービスの名前空間 がある場合、PowerShell コマンドレットによって生成されるエンドポイントのうち 1 つのみを選択してコピーする必要があることに注意してください。

  3. Windows Azure Service Bus に対する場合と同様に、NamespaceManager および MessagingFactory オブジェクトを作成します。

    MessagingFactory messagingFactory = MessagingFactory.Create();
    NamespaceManager namespaceManager = NamespaceManager.Create();
    

    Service Bus に接続していると、名前空間マネージャーのインスタンスを使用してメッセージング エンティティ (キュー、トピック、およびサブスクリプション) を作成するか、QueueClientTopicClient、または SubscriptionClient オブジェクトを作成して、メッセージを送受信できます。

接続パラメーターを明示的に指定して Service Bus for Windows Server に接続する

  1. 構成ファイルで接続文字列を使用して Service Bus に接続する操作に加え、必要な入力パラメーターを明示的に指定して Service Bus に接続する必要が生じる場合があります。Service Bus では、コードに接続文字列を組み込めるヘルパー クラスを導入します。

    接続ファイルまたはその他の手段を使用して、コード内に接続パラメーターを指定してください。

    string ServerFQDN = "localhost";
    int HttpPort = 9355;
    int TcpPort = 9354;
    string ServiceNamespace = "ServiceBusDefaultNamespace";
    
  2. コード内で、ServiceBusConnectionStringBuilder ヘルパー クラスを使用して Service Bus に接続します。次に例を示します。

    ServiceBusConnectionStringBuilder connBuilder = new ServiceBusConnectionStringBuilder();
    connBuilder.ManagementPort = HttpPort;
    connBuilder.RuntimePort = TcpPort;
    connBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace }.Uri);
    connBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = ServerFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri);
    
    MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());
    NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());
    

    Service Bus for Windows Server の接続文字列の詳細情報:、「Configuration Connection Strings」を参照してください。

参照

概念

開発方法

その他のリソース

Configuration Connection Strings
Queues, Topics, and Subscriptions

ビルド日:

2013-07-25