Export (0) Print
Expand All

WSStreamedHttpBinding

Download sample

The sample demonstrates how to create a binding that is designed to support streaming scenarios when the HTTP transport is used.

NoteNote:

The setup procedure and build instructions for this sample are located at the end of this topic.

The steps to create and configure a new standard binding are as follows.

  1. Create a new standard binding

    The standard bindings in Windows Communication Foundation (WCF) such as basicHttpBinding, and netTcpBinding pre-configure the underlying transports and channel stack for specific requirements. In this sample, WSStreamedHttpBinding configures the channel stack to support streaming. By default, WS-Security and reliable messaging are not added to the channel stack because both features are not supported by streaming. The new binding is implemented in the class WSStreamedHttpBinding that derives from Binding. The WSStreamedHttpBinding contains the following binding elements: HttpTransportBindingElement, HttpsTransportBindingElement, TransactionFlowBindingElement, and TextMessageEncodingBindingElement. The class provides a CreateBindingElements() method to configure the resulting binding stack, as shown in the following sample code.

    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. Add configuration support

    To expose the transport through configuration the sample implements two more classes—WSStreamedHttpBindingConfigurationElement and WSStreamedHttpBindingSection. The class WSStreamedHttpBindingSection is a StandardBindingCollectionElement that exposes WSStreamedHttpBinding to the WCF configuration system. The bulk of the implementation is delegated to WSStreamedHttpBindingConfigurationElement, which derives from StandardBindingElement. The class WSStreamedHttpBindingConfigurationElement has properties that correspond to the properties of WSStreamedHttpBinding, and functions to map each configuration element to a binding.

    Register the handler with the configuration system, by adding the following section to the service's configuration file:

    <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>
    
    

    The handler can then be referenced from the serviceModel configuration section:

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

To set up, build, and run the sample

  1. Ensure that you have performed the steps listed in One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. Ensure that you have performed the Internet Information Service (IIS) Server Certificate Installation Instructions.

  3. To build the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  4. To run the sample in a cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

  5. When the client window displays, type "Sample.txt". You should find a "Copy of Sample.txt" in your directory.

The WSStreamedHttpBinding Sample Service

The sample service that uses WSStreamedHttpBinding is located in the service subdirectory. The implementation of OperationContract uses a MemoryStream to first retrieve all data from the incoming stream before returning the MemoryStream. The sample service is hosted by 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;
    }
}

The WSStreamedHttpBinding Sample Client

The client that is used to interact with the service using WSStreamedHttpBinding is located in the client subdirectory. Because the certificate used in this sample is a test certificate created with Makecert.exe, a security alert displays when you attempt to access an HTTPS address in your browser such as https://localhost/servicemodelsamples/service.svc. To allow the WCF client to work with a test certificate in place, some additional code has been added to the client to suppress the security alert. The code and the accompanying class are not required when using production certificates.

// 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

Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.

Community Additions

ADD
Show:
© 2014 Microsoft