How to: Send and Receive Large Amounts of Data to and from a Web Service

WSE 3.0 supports the Message Transmission Optimization Mechanism (MTOM) specification for transmitting large amounts of data to and from Web services.

To send large amounts of data to or from a Web service

  1. Open the Web service client project in Visual Studio 2005.

  2. Specify that the client sends SOAP messages encoded using MTOM.

    1. In Solution Explorer, right-click the project name, and then click WSE Settings 3.0….

    2. Select the Messaging tab.

    3. Choose On for the Client Mode.
      The On Client Mode specifies that proxy classes generated in the future set use the MTOM encoding by setting the RequireMtom property of the proxy class to true.

      Note

      When the TCP protocol is used, the RequireMtom property has no affect, as SOAP messages are always sent MTOM encoded.

    4. Click OK to dismiss the dialog.
      This adds an <mtom> Element to the client's Web.config file.

  3. When the amount of data send or received by the Web service client exceeds 4 MB, configure the client to handle the larger amount of data.

    1. Increase the ASP.NET limits on the maximum size of SOAP messages and the maximum number of seconds that a request is allowed to execute by adding the <httpRuntime> configuration element to the application's app.config file.
      The following code example sets the ASP.NET limit on the maximum size of an incoming request to 400MB and the maximum amount of time a request is allowed to execute to 5 minutes (300 seconds).

      <configuration>
        <system.web>
        <httpRuntime maxMessageLength="409600"
          executionTimeoutInSeconds="300"/>
        </system.web>
      </configuration>
      
    2. Increase the WSE limit on the maximum size of SOAP messages using the <maxMessageLength> Element.
      To send and receive the largest possible SOAP messages, set the value of the <maxMessageLength> element to -1.
      The following code example disables the limit on the maximum size of SOAP messages by WSE.

      <configuration>
        <microsoft.web.services3>
          <messaging>
            <maxMessageLength value="-1" />
          </messaging>
        </microsoft.web.services3>
      </configuration>
      
  4. Communicate with the Web service.

    Parameters or return values that contain byte arrays are MTOM encoded.

    The following code example communicates with a Web service to get a file that is MTOM encoded.

    Dim response As Byte()
    ' Communicate with the Web service to get the requested file.
    response = serviceproxy.GetFile(fileName)
    
    //  Communicate with the Web service to get the requested file.
    byte[] response = serviceproxy.GetFile(fileName);
    

Example

The following code example communicates with a Web service using SOAP messages that are MTOM encoded to retrieve a graphic image and then displays it in a picture box.

Dim fileName As String = "Winter.jpg"
' Create an instance of the Web service proxy class.
Dim serviceproxy As New BinaryDataMTOMServiceWse()

' Specify that MTOM encoding must be used.
serviceproxy.RequireMtom = True

serviceproxy.SetPolicy("ClientPolicy")
Dim response As Byte()
' Communicate with the Web service to get the requested file.
response = serviceproxy.GetFile(fileName)

Dim memory As MemoryStream = New MemoryStream(response)

' Display the file in a picture box.
PictureBox1.Image = System.Drawing.Image.FromStream(memory)
String fileName = "Winter.jpg";
// Create an instance of the Web service proxy class.
BinaryDataMTOMServiceWse serviceproxy = new BinaryDataMTOMServiceWse();

// Specify that MTOM encoding must be used.
serviceproxy.RequireMtom = true;

serviceproxy.SetPolicy("ClientPolicy");
//  Communicate with the Web service to get the requested file.
byte[] response = serviceproxy.GetFile(fileName);

MemoryStream memory = new MemoryStream(response);

//  Display the file in a picture box.
pictureBox1.Image = System.Drawing.Image.FromStream(memory);

See Also

Tasks

How to: Enable a Web Service to Send and Receive Large Amounts of Data

Reference

RequireMtom