次の方法で共有


WSStreamedHttpBinding

Download sample

このサンプルでは、HTTP トランスポート使用時にストリーミングをサポートする目的でデザインされたバインディングを作成する方法を示します。

Noteメモ :

このサンプルのセットアップ手順とビルド手順については、このトピックの最後を参照してください。

新しい標準バインディングを作成して構成する手順は、次のとおりです。

  1. 新しい標準バインディングを作成する

    basicHttpBinding や netTcpBinding などの Windows Communication Foundation (WCF) の標準バインディングは、特定の要件に合わせて、基になるトランスポートとチャネル スタックを構成します。このサンプルでは、WSStreamedHttpBinding は、ストリーミングをサポートするようチャネル スタックを構成します。既定では、WS-Security と信頼できるメッセージ機能はチャネル スタックに追加されません。どちらの機能もストリーミングではサポートされないためです。新しいバインディングは、Binding から派生するクラス WSStreamedHttpBinding に実装されます。WSStreamedHttpBinding には、HttpTransportBindingElementHttpsTransportBindingElementTransactionFlowBindingElement、および TextMessageEncodingBindingElement のバインディング要素が含まれます。このクラスは、作成されるバインディング スタックを構成する CreateBindingElements() メソッドを提供します。次のサンプル コードを参照してください。

    public override BindingElementCollection CreateBindingElements()
    {
         // return collection of BindingElements
         BindingElementCollection bindingElements = new BindingElementCollection();
    
        // the order of binding elements within the collection is important: layered channels are applied in the order included, followed by
        // the message encoder, and finally the transport at the end
        if (flowTransactions)
        {
            bindingElements.Add(transactionFlow);
        }
        bindingElements.Add(textEncoding);
    
        // add transport (http or https)
        bindingElements.Add(transport);
        return bindingElements.Clone();
    }
    
  2. 構成サポートを追加する

    構成を使用してトランスポートを公開するため、サンプルではさらに、WSStreamedHttpBindingConfigurationElement クラスと WSStreamedHttpBindingSection クラスという 2 つのクラスを実装します。クラス WSStreamedHttpBindingSectionStandardBindingCollectionElement で、WSStreamedHttpBinding を WCF の構成システムに公開します。実装の大部分は WSStreamedHttpBindingConfigurationElement で代行されます。これは StandardBindingElement の派生です。クラス WSStreamedHttpBindingConfigurationElement には WSStreamedHttpBinding のプロパティに対応するプロパティがあり、各構成要素をバインディングにマップする関数があります。

    次のセクションをサービスの構成ファイルに追加することにより、ハンドラを構成システムに登録します。

    <configuration>
      <system.serviceModel>
        <extensions>
          <bindingExtensions>
            <add name="wsStreamedHttpBinding" type="Microsoft.ServiceModel.Samples.WSStreamedHttpBindingCollectionElement, WSStreamedHttpBinding, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" />
          </bindingExtensions>
        </extensions>
      </system.serviceModel>
    </configuration>
    

    ハンドラは serviceModel 構成セクションから参照できます。

    <configuration>
      <system.serviceModel>
        <client>
          <endpoint
                    address="https://localhost/servicemodelsamples/service.svc"
                    bindingConfiguration="Binding"
                    binding="wsStreamedHttpBinding"
                    contract="Microsoft.ServiceModel.Samples.IStreamedEchoService"/>
        </client>
      </system.serviceModel>
    </configuration>
    

サンプルを設定、ビルド、および実行するには

  1. Windows Communication Foundation サンプルの 1 回限りのセットアップの手順」の手順が実行済みであることを確認します。

  2. インターネット インフォメーション サービス (IIS) サーバー証明書インストール手順」が実行済みであることを確認します。

  3. ソリューションをビルドするには、「Windows Communication Foundation サンプルのビルド」の手順に従います。

  4. 複数コンピュータ構成でサンプルを実行するには、「Windows Communication Foundation サンプルの実行」の手順に従います。

  5. クライアント ウィンドウが表示されたら、「Sample.txt」と入力します。ディレクトリで "Sample.txt のコピー" を検索する必要があります。

WSStreamedHttpBinding サービスのサンプル

WSStreamedHttpBinding を使用するサービスのサンプルは、サービス サブディレクトリにあります。OperationContract の実装は MemoryStream を使用して、最初に受信ストリームからすべてのデータを取得し、その後 MemoryStream を返します。このサンプル サービスは、インターネット インフォメーション サービス (IIS) によってホストされます。

[ServiceContract]
public interface IStreamedEchoService
{
    [OperationContract]
    Stream Echo(Stream data);
}

public class StreamedEchoService : IStreamedEchoService
{
    public Stream Echo(Stream data)
    {
        MemoryStream dataStorage = new MemoryStream();
        byte[] byteArray = new byte[8192];
        int bytesRead = data.Read(byteArray, 0, 8192);
        while (bytesRead > 0)
        {
            dataStorage.Write(byteArray, 0, bytesRead);
            bytesRead = data.Read(byteArray, 0, 8192);
        }
        data.Close();
        dataStorage.Seek(0, SeekOrigin.Begin);

        return dataStorage;
    }
}

WSStreamedHttpBinding クライアントのサンプル

WSStreamedHttpBinding を使用してサービスとやり取りするクライアントは、クライアント サブディレクトリにあります。このサンプルで使用する証明書は Makecert.exe で作成されたテスト証明書なので、ブラウザで https://localhost/servicemodelsamples/service.svc のような HTTPS アドレスにアクセスしようとするとセキュリティ警告が表示されます。WCF クライアントがテスト証明書に対して問題なく動作するようにするには、クライアントにコードを追加して、セキュリティ警告を非表示にする必要があります。そのためのコードとそれに必要なクラスは、本運用の証明書を使用するときには不要です。

// WARNING: This code is only required for test certificates such as those created by makecert. It is 
// not recommended for production code.
PermissiveCertificatePolicy.Enact("CN=ServiceModelSamples-HTTPS-Server");

Footer image

Copyright © 2007 by Microsoft Corporation.All rights reserved.