WSStreamedHttpBinding

WSStreamedHttpBinding 示例演示如何创建一个绑定,该绑定旨在支持使用 HTTP 传输时的流式处理方案。

注释

本示例的设置过程和生成说明位于本主题末尾。

创建和配置新标准绑定的步骤如下所示。

  1. 创建新的标准绑定

    Windows Communication Foundation(WCF)中的标准绑定(如 basicHttpBinding)和 netTcpBinding 针对特定要求配置基础传输和通道堆栈。 在此示例中, WSStreamedHttpBinding 将通道堆栈配置为支持流式处理。 默认情况下,不将 WS-Security 和可靠消息传递添加到通道堆栈中,因为流不支持这两个功能。 新绑定在派生自WSStreamedHttpBinding的类Binding中实现。 包含 WSStreamedHttpBinding 以下绑定元素: HttpTransportBindingElementHttpsTransportBindingElementTransactionFlowBindingElementTextMessageEncodingBindingElement。 该类提供一种方法 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. 添加配置支持

    为了通过配置来暴露传输过程,该示例还实现了两个类,WSStreamedHttpBindingConfigurationElementWSStreamedHttpBindingSectionWSStreamedHttpBindingSection 类是一个向 WCF 配置系统公开 StandardBindingCollectionElement<TStandardBinding,TBindingConfiguration>WSStreamedHttpBinding。 批量实现委托给从 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. 使用以下命令安装 ASP.NET 4.0。

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable
    
  2. 确保您已经执行了One-Time Windows Communication Foundation 示例设置程序中列出的步骤。

  3. 确保已执行 Internet Information Services (IIS) 服务器证书安装说明

  4. 要生成解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。

  5. 若要在跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行。

  6. 当客户端窗口显示时,键入“Sample.txt”。 应当能够在目录中找到“Copy of Sample.txt”。

WSStreamedHttpBinding 示例服务

使用 WSStreamedHttpBinding 的示例服务位于服务子目录中。 OperationContract 的实现使用 MemoryStream 先从传入流中检索所有数据,然后返回 MemoryStream。 示例服务由 Internet Information Services (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 地址时会显示安全警报,例如 https://localhost/servicemodelsamples/service.svc。 为了让 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");