チュートリアル:Azure WCF Relay REST のチュートリアル

このチュートリアルでは、REST ベースのインターフェイスを表示する Azure Relay ホスト アプリケーションを構築する方法について説明します。 REST を使用すると、Web ブラウザーなどの Web クライアントから HTTP 要求を介して Service Bus API にアクセスできるようになります。

本チュートリアルでは、Windows Communication Foundation (WCF) REST プログラミング モデルを使用して、Azure Relay に REST サービスを構築します。 詳細については、WCF Web HTTP プログラミング モデルに関する記事と「サービスの設計と実装」を参照してください。

このチュートリアルでは、次のタスクを行います。

  • このチュートリアルの前提条件をインストールする。
  • Relay 名前空間を作成する。
  • REST ベースの WCF サービス コントラクトを定義する。
  • REST ベースの WCF コントラクトを実装する。
  • REST ベースの WCF サービスをホストし、実行する。
  • サービスを実行してテストする。

前提条件

このチュートリアルを完了するには、次の前提条件を用意しておく必要があります。

Relay 名前空間を作成する

Azure で Relay 機能を使用するには、最初にサービス名前空間を作成する必要があります。 名前空間は、アプリケーション内で Azure リソースをアドレス指定するためのスコープ コンテナーを提供します。 こちらの手順に従って、Relay 名前空間を作成します。

Azure Relay で使用する REST ベースの WCF サービス コントラクトを定義する

WCF REST スタイルのサービスを作成するには、コントラクトを定義する必要があります。 コントラクトには、ホストがサポートする操作を指定します。 サービス操作は、Web サービスのメソッドに似ています。 C++、C#、または Visual Basic インターフェイスを使用してコントラクトを定義します。 インターフェイスの各メソッドは、特定のサービス操作に対応しています。 各インターフェイスに ServiceContractAttribute 属性を適用し、各操作に OperationContractAttribute 属性を適用します。

ヒント

ServiceContractAttribute があるインターフェイスのメソッドに OperationContractAttribute がない場合、そのメソッドは公開されません。 これらのタスクに使用されるコードの例を手順に従って説明します。

WCF コントラクトと REST スタイルのコントラクトの主な違いは、REST スタイルの OperationContractAttribute にはプロパティWebGetAttribute がある点です。 このプロパティを使用すると、インターフェイス内のメソッドを相手側のインターフェイスのメソッドにマップすることができます。 この例では、WebGetAttribute 属性を使用してメソッドを HTTP GET にリンクします。 このアプローチにより、Service Bus は、インターフェイスに送信されたコマンドをより正確に取得および解釈できるようになります。

インターフェイスを使用してコントラクトを作成するには

  1. Microsoft Visual Studio を管理者として起動します。 これを行うには、Visual Studio のプログラム アイコンを右クリックして [管理者として実行] を選択します。

  2. Visual Studio で、 [新しいプロジェクトの作成] を選択します。

  3. [新しいプロジェクトの作成] で、C# の [コンソール アプリ (.NET Framework)] を選択してから、 [次へ] を選択します。

  4. プロジェクトに ImageListener という名前を付けます。 既定の [場所] を使用して、 [作成] を選択します。

    C# プロジェクトの場合、Visual Studio によって Program.cs ファイルが作成されます。 このクラスには、空の Main() メソッドが含まれています。このメソッドは、コンソール アプリケーション プロジェクトを正常にビルドするために必要です。

  5. ソリューション エクスプローラーImageListener プロジェクトを右クリックし、 [NuGet パッケージの管理] を選択します。

  6. [参照] を選択し、WindowsAzure.ServiceBus を探して選択します。 [インストール] を選択して、使用条件に同意します。

    この手順では、Service Bus と System.ServiceModel.dll への参照を追加します。 Service Bus ライブラリと WCF の System.ServiceModel への参照が、このパッケージによって自動的に追加されます。

  7. System.ServiceModel.Web.dll への参照をプロジェクトに明示的に追加します。 ソリューション エクスプローラーで、プロジェクト フォルダーの [参照] を右クリックし、 [参照の追加] を選択します。

  8. [参照の追加][フレームワーク] を選択し、 [検索] に「System.ServiceModel.Web」と入力します。 [System.ServiceModel.Web] チェック ボックスをオンにし、[OK] を選択します。

次に、次のコードの変更をプロジェクトに加えます。

  1. Program.cs ファイルの先頭に次の using ステートメントを追加します。

    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Web;
    using System.IO;
    
    • System.ServiceModel は、WCF の基本機能にプログラムでアクセスできる名前空間です。 WCF Relay は、サービス コントラクトの定義に WCF の多くのオブジェクトと属性を使用します。 ほとんどの Relay アプリケーションで、この名前空間を使用します。
    • System.ServiceModel.Channels はチャネルの定義に役立ちます。チャネルは、Azure Relay およびクライアント Web ブラウザーとの通信に使用されるオブジェクトです。
    • System.ServiceModel.Web には、Web ベースのアプリケーションを作成できる型が含まれています。
  2. ImageListener 名前空間の名前を Microsoft.ServiceBus.Samples に変更します。

    namespace Microsoft.ServiceBus.Samples
    {
        ...
    
  3. 名前空間の宣言の左中かっこの直後に、IImageContract という名前の新しいインターフェイスを定義し、https://samples.microsoft.com/ServiceModel/Relay/RESTTutorial1 の値を持つインターフェイスに ServiceContractAttribute 属性を適用します。

    [ServiceContract(Name = "ImageContract", Namespace = "https://samples.microsoft.com/ServiceModel/Relay/RESTTutorial1")]
    public interface IImageContract
    {
    }
    

    この名前空間の値は、コードのスコープ全体で使用する名前空間とは異なります。 この名前空間の値は、このコントラクトの一意の識別子であり、値にはバージョン情報が含まれています。 詳細については、「サービスのバージョン管理」を参照してください。 名前空間を明示的に指定すると、既定の名前空間値がコントラクト名に追加されなくなります。

  4. IImageContract インターフェイス内で、IImageContract コントラクトによってインターフェイスで公開される単一操作のメソッドを宣言し、パブリック Service Bus コントラクトの一部として公開するメソッドに OperationContract 属性を適用します。

    public interface IImageContract
    {
        [OperationContract]
        Stream GetImage();
    }
    
  5. OperationContract 属性に、WebGet の値を追加します。

    public interface IImageContract
    {
        [OperationContract, WebGet]
        Stream GetImage();
    }
    

    WebGet の値の追加により、リレー サービスは HTTP GET 要求を GetImage にルーティングし、GetImage の戻り値を HTTP GETRESPONSE 応答に変換できるようになります。 このチュートリアルの後半で、Web ブラウザーを使用してこのメソッドにアクセスし、ブラウザーに画像を表示します。

  6. IImageContract 定義の直後に、IImageContract インターフェイスと IClientChannel インターフェイスの両方から継承するチャネルを宣言します。

    public interface IImageChannel : IImageContract, IClientChannel { }
    

    チャネルは、サービスとクライアントが相互に情報を渡すときに経由する WCF オブジェクトです。 後で、ホスト アプリケーションでチャネルを作成します。 Azure Relay は、ブラウザーからの HTTP GET 要求を GetImage 実装に渡すために、このチャネルを使用します。 また、リレーでは、チャネルを使用して GetImage の戻り値が取得され、クライアント ブラウザーの HTTP GETRESPONSE に変換されます。

  7. ここまでの作業の精度を確認するには、 [ビルド]>[ソリューションのビルド] を選択します。

WCF Relay コントラクトを定義する例

次のコードに、WCF Relay コントラクトを定義する基本的なインターフェイスを示します。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.IO;

namespace Microsoft.ServiceBus.Samples
{

    [ServiceContract(Name = "IImageContract", Namespace = "https://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IImageContract
    {
        [OperationContract, WebGet]
        Stream GetImage();
    }

    public interface IImageChannel : IImageContract, IClientChannel { }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

REST ベースの WCF サービス コントラクトを実装する

REST スタイルの WCF Relay サービスを作成するには、最初にインターフェイスを使用してコントラクトを作成します。 次の手順はインターフェイスの実装です。 この手順には、ユーザー定義の ImageService インターフェイスを実装する IImageContract というクラスの作成が含まれます。 コントラクトを実装したら、App.config ファイルを使用してインターフェイスを構成します。 この構成ファイルには、アプリケーションに必要な情報が含まれています。 この情報には、サービス名、コントラクト名、リレー サービスとの通信に使用されるプロトコルの種類が含まれます。 これらのタスクに使用されるコードの例を手順に従って説明します。

これまでの手順と同様に、REST スタイルのコントラクトと WCF Relay コントラクトの実装にはほとんど違いがありません。

REST スタイルの Service Bus コントラクトを実装するには

  1. IImageContract インターフェイスの定義の直後に、ImageService という新しいクラスを作成します。 ImageService クラスによって、IImageContract インターフェイスが実装されます。

    class ImageService : IImageContract
    {
    }
    

    他のインターフェイス実装と同様に、別のファイルに指定した定義を実装することができます。 ただし、このチュートリアルでは、インターフェイス定義や Main() メソッドと同じファイルで実装します。

  2. ServiceBehaviorAttribute 属性を IImageService クラスに適用して、クラスが WCF コントラクトの実装であることを示します。

    [ServiceBehavior(Name = "ImageService", Namespace = "https://samples.microsoft.com/ServiceModel/Relay/")]
    class ImageService : IImageContract
    {
    }
    

    既に説明したように、この名前空間は従来の名前空間ではありません。 これは、コントラクトを特定する WCF アーキテクチャの一部です。 詳細については、「データ コントラクト名」を参照してください。

  3. .jpg 画像をプロジェクトに追加します。 このファイルは、サービスが受信側ブラウザーに表示する画像です。

    1. プロジェクトを右クリックし、 [追加] を選択します。
    2. 次に、 [既存の項目] を選択します。
    3. [既存項目の追加] を使用して適切な .jpg を参照し、 [追加] を選択します。 ファイルを追加するときに、 [ファイル名] の横にあるドロップダウン リストから [すべてのファイル] を選択します。

    以降、このチュートリアルでは、画像名が image.jpg という前提で説明します。 別のファイルを使用する場合は、画像のファイル名を変更するか、ファイル名に合わせてコードを変更する必要があります。

  4. 実行中のサービスから画像ファイルを検出できるようにするには、ソリューション エクスプローラーで画像ファイルを右クリックし、 [プロパティ] を選択します。 [プロパティ][出力ディレクトリにコピー][新しい場合はコピーする] に設定します。

  5. インターフェイスを使用してコントラクトを作成するには」の手順を使用して、System.Drawing.dll アセンブリへの参照をプロジェクトに追加します。

  6. 次の関連する using ステートメントを追加します。

    using System.Drawing;
    using System.Drawing.Imaging;
    using Microsoft.ServiceBus;
    using Microsoft.ServiceBus.Web;
    
  7. ビットマップを読み込む次のコンストラクターを ImageService クラスに追加し、クライアント ブラウザーに送信する準備をします。

    class ImageService : IImageContract
    {
        const string imageFileName = "image.jpg";
    
        Image bitmap;
    
        public ImageService()
        {
            this.bitmap = Image.FromFile(imageFileName);
        }
    }
    
  8. 前のコードの直後に、次の GetImage メソッドを ImageService クラスに追加して、画像を含む HTTP メッセージを返します。

    public Stream GetImage()
    {
        MemoryStream stream = new MemoryStream();
        this.bitmap.Save(stream, ImageFormat.Jpeg);
    
        stream.Position = 0;
        WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
    
        return stream;
    }
    

    この実装では、MemoryStream を使用して画像を取得し、ブラウザーにストリーミングする準備をします。 ゼロ位置からストリーミングを開始し、ストリーム コンテンツを .jpg 形式と宣言し、情報をストリーミングします。

  9. [ビルド]>[ビルドするソリューション] を選択します。

Service Bus で Web サービスを実行するための構成を定義するには

  1. ソリューション エクスプローラーで、App.config をダブルクリックして、Visual Studio エディターでそのファイルを開きます。

    App.config ファイルには、サービス名、エンドポイント、およびバインドが含まれています。 エンドポイントは、クライアントとホストの相互通信用に Azure Relay により公開されている場所です。 バインドは、通信に使用されるプロトコルの種類です。 主な違いは、構成されているサービス エンドポイントが WebHttpRelayBinding バインドを参照している点です。

  2. <system.serviceModel> XML 要素は 1 つ以上のサービスを定義する WCF 要素です。 これはサービス名とエンドポイントの定義に使用されます。 <system.serviceModel> 要素の下 (ただし、<system.serviceModel> 内) に、次の内容の <bindings> 要素を追加します。

    <bindings>
        <!-- Application Binding -->
        <webHttpRelayBinding>
            <binding name="default">
                <security relayClientAuthenticationType="None" />
            </binding>
        </webHttpRelayBinding>
    </bindings>
    

    この内容では、アプリケーションで使用されるバインドを定義します。 複数のバインドを定義できますが、このチュートリアルで定義するバインドは 1 つのみです。

    前のコードでは、relayClientAuthenticationTypeNone に設定された WCF Relay WebHttpRelayBinding バインドを定義しています。 この設定は、このバインドを使用するエンドポイントは、クライアントの資格情報を必要としないことを示します。

  3. <bindings> 要素の後に、<services> 要素を追加します。 バインドと同様に、1 つの構成ファイルで複数のサービスを定義できます。 ただし、このチュートリアルで定義するサービスは 1 つのみです。

    <services>
        <!-- Application Service -->
        <service name="Microsoft.ServiceBus.Samples.ImageService"
             behaviorConfiguration="default">
            <endpoint name="RelayEndpoint"
                    contract="Microsoft.ServiceBus.Samples.IImageContract"
                    binding="webHttpRelayBinding"
                    bindingConfiguration="default"
                    behaviorConfiguration="sbTokenProvider"
                    address="" />
        </service>
    </services>
    

    この内容では、以前に定義した既定値 webHttpRelayBinding を使用するサービスを構成します。 また、次の手順で定義されている既定の sbTokenProvider も使用します。

  4. <services> 要素の後に、次の内容の <behaviors> 要素を作成します。SAS_KEY は、Shared Access Signature (SAS) キーに置き換えます。 Azure portal から SAS キーを取得するには、「管理資格情報を取得する」を参照してください。

    <behaviors>
        <endpointBehaviors>
            <behavior name="sbTokenProvider">
                <transportClientEndpointBehavior>
                    <tokenProvider>
                        <sharedAccessSignature keyName="RootManageSharedAccessKey" key="YOUR_SAS_KEY" />
                    </tokenProvider>
                </transportClientEndpointBehavior>
            </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="default">
                    <serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false" />
                </behavior>
            </serviceBehaviors>
    </behaviors>
    
  5. 引き続き App.config 内で、<appSettings> 要素内の接続文字列の値全体を、前の手順でポータルから取得した接続文字列に置き換えます。

    <appSettings>
       <!-- Service Bus specific app settings for messaging connections -->
       <add key="Microsoft.ServiceBus.ConnectionString"
           value="Endpoint=sb://yourNamespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=YOUR_SAS_KEY"/>
    </appSettings>
    
  6. ソリューション全体をビルドするには、 [ビルド]>[ソリューションのビルド] を選択します。

REST ベースの WCF サービス コントラクトを実装する例

次のコードは、WebHttpRelayBinding バインドを使用して Service Bus で実行する REST ベース サービスのコントラクトとサービスの実装を示しています。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Web;

namespace Microsoft.ServiceBus.Samples
{


    [ServiceContract(Name = "ImageContract", Namespace = "https://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IImageContract
    {
        [OperationContract, WebGet]
        Stream GetImage();
    }

    public interface IImageChannel : IImageContract, IClientChannel { }

    [ServiceBehavior(Name = "ImageService", Namespace = "https://samples.microsoft.com/ServiceModel/Relay/")]
    class ImageService : IImageContract
    {
        const string imageFileName = "image.jpg";

        Image bitmap;

        public ImageService()
        {
            this.bitmap = Image.FromFile(imageFileName);
        }

        public Stream GetImage()
        {
            MemoryStream stream = new MemoryStream();
            this.bitmap.Save(stream, ImageFormat.Jpeg);

            stream.Position = 0;
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";

            return stream;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

次の例は、サービスに関連付けられている App.config ファイルを示しています。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>
    <system.serviceModel>
        <extensions>
            <!-- In this extension section we are introducing all known service bus extensions. User can remove the ones they don't need. -->
            <behaviorExtensions>
                <add name="connectionStatusBehavior"
                    type="Microsoft.ServiceBus.Configuration.ConnectionStatusElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="transportClientEndpointBehavior"
                    type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="serviceRegistrySettings"
                    type="Microsoft.ServiceBus.Configuration.ServiceRegistrySettingsElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            </behaviorExtensions>
            <bindingElementExtensions>
                <add name="netMessagingTransport"
                    type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingTransportExtensionElement, Microsoft.ServiceBus,  Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="tcpRelayTransport"
                    type="Microsoft.ServiceBus.Configuration.TcpRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="httpRelayTransport"
                    type="Microsoft.ServiceBus.Configuration.HttpRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="httpsRelayTransport"
                    type="Microsoft.ServiceBus.Configuration.HttpsRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="onewayRelayTransport"
                    type="Microsoft.ServiceBus.Configuration.RelayedOnewayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            </bindingElementExtensions>
            <bindingExtensions>
                <add name="basicHttpRelayBinding"
                    type="Microsoft.ServiceBus.Configuration.BasicHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="webHttpRelayBinding"
                    type="Microsoft.ServiceBus.Configuration.WebHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="ws2007HttpRelayBinding"
                    type="Microsoft.ServiceBus.Configuration.WS2007HttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="netTcpRelayBinding"
                    type="Microsoft.ServiceBus.Configuration.NetTcpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="netOnewayRelayBinding"
                    type="Microsoft.ServiceBus.Configuration.NetOnewayRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="netEventRelayBinding"
                    type="Microsoft.ServiceBus.Configuration.NetEventRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add name="netMessagingBinding"
                    type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            </bindingExtensions>
        </extensions>
      <bindings>
        <!-- Application Binding -->
        <webHttpRelayBinding>
          <binding name="default">
            <security relayClientAuthenticationType="None" />
          </binding>
        </webHttpRelayBinding>
      </bindings>
      <services>
        <!-- Application Service -->
        <service name="Microsoft.ServiceBus.Samples.ImageService"
             behaviorConfiguration="default">
          <endpoint name="RelayEndpoint"
                  contract="Microsoft.ServiceBus.Samples.IImageContract"
                  binding="webHttpRelayBinding"
                  bindingConfiguration="default"
                  behaviorConfiguration="sbTokenProvider"
                  address="" />
        </service>
      </services>
      <behaviors>
        <endpointBehaviors>
          <behavior name="sbTokenProvider">
            <transportClientEndpointBehavior>
              <tokenProvider>
                <sharedAccessSignature keyName="RootManageSharedAccessKey" key="YOUR_SAS_KEY" />
              </tokenProvider>
            </transportClientEndpointBehavior>
          </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
          <behavior name="default">
            <serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
    <appSettings>
        <!-- Service Bus specific app settings for messaging connections -->
        <add key="Microsoft.ServiceBus.ConnectionString"
            value="Endpoint=sb://yourNamespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=YOUR_SAS_KEY>"/>
    </appSettings>
</configuration>

Azure Relay を使用する REST ベースの WCF サービスをホストする

このセクションでは、WCF Relay でコンソール アプリケーションを使用して、Web サービスを実行する方法について説明します。 このセクションで作成するコードの詳細については、手順に従って例を使用して説明します。

サービスのベース アドレスを作成するには

  1. Main() 関数の宣言で、プロジェクトの名前空間を格納する変数を作成します。 yourNamespace は、前に作成した Relay 名前空間の名前に置き換えてください。

    string serviceNamespace = "yourNamespace";
    

    Service Bus は、名前空間の名前を使用して一意の URI を作成します。

  2. 名前空間に基づくサービスのベース アドレスの Uri インスタンスを作成します。

    Uri address = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "Image");
    

Web サービス ホストを作成および構成するには

引き続き Main() で、このセクションで作成した URI アドレスを使用して、Web サービス ホストを作成します。

WebServiceHost host = new WebServiceHost(typeof(ImageService), address);

サービス ホストは、ホスト アプリケーションをインスタンス化する WCF オブジェクトです。 この例では、作成するホストの種類 (ImageService) と、ホスト アプリケーションを公開するアドレスを渡します。

Web サービス ホストを実行するには

  1. 引き続き Main() で、次の行を追加してサービスを開きます。

    host.Open();
    

    サービスが実行されます。

  2. サービスが実行中であることと、サービスの停止方法を示すメッセージが表示されます。

    Console.WriteLine("Copy the following address into a browser to see the image: ");
    Console.WriteLine(address + "GetImage");
    Console.WriteLine();
    Console.WriteLine("Press [Enter] to exit");
    Console.ReadLine();
    
  3. 完了したら、サービス ホストを閉じます。

    host.Close();
    

サービス コントラクトと実装の例

次の例では、チュートリアルの前の手順で説明したサービス コントラクトと実装が含まれています。ここでは、コンソール アプリケーションでサービスをホストします。 次のコードをコンパイルして、ImageListener.exe という実行可能ファイルを作成します。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Web;

namespace Microsoft.ServiceBus.Samples
{

    [ServiceContract(Name = "ImageContract", Namespace = "https://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IImageContract
    {
        [OperationContract, WebGet]
        Stream GetImage();
    }

    public interface IImageChannel : IImageContract, IClientChannel { }

    [ServiceBehavior(Name = "ImageService", Namespace = "https://samples.microsoft.com/ServiceModel/Relay/")]
    class ImageService : IImageContract
    {
        const string imageFileName = "image.jpg";

        Image bitmap;

        public ImageService()
        {
            this.bitmap = Image.FromFile(imageFileName);
        }

        public Stream GetImage()
        {
            MemoryStream stream = new MemoryStream();
            this.bitmap.Save(stream, ImageFormat.Jpeg);

            stream.Position = 0;
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";

            return stream;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string serviceNamespace = "InsertServiceNamespaceHere";
            Uri address = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "Image");

            WebServiceHost host = new WebServiceHost(typeof(ImageService), address);
            host.Open();

            Console.WriteLine("Copy the following address into a browser to see the image: ");
            Console.WriteLine(address + "GetImage");
            Console.WriteLine();
            Console.WriteLine("Press [Enter] to exit");
            Console.ReadLine();

            host.Close();
        }
    }
}

サービスを実行してテストする

ソリューションをビルドしたら、次の手順でアプリケーションを実行します。

  1. F5 キーを押すか、実行可能ファイルの場所 (ImageListener\bin\Debug\ImageListener.exe) を参照し、サービスを実行します。 次の手順で必要になるため、アプリを実行したままにしておきます。
  2. コマンド プロンプトのアドレスをコピーし、ブラウザーに貼り付けて画像を確認します。
  3. 操作が完了したら、コマンド プロンプト ウィンドウで Enter キーを押してアプリを終了します。

次のステップ

ここでは、Azure Relay サービスを使用するアプリケーションを構築しました。詳細については、次の記事を参照してください。