Share via


How to: Migrate Code that Uses DIME Attachments to Use MTOM

Web Services Enhancements for Microsoft .NET (WSE) 2.0 code that uses DIME attachments must be migrated to use the Message Transmission Optimization Mechanism (MTOM).

To migrate a Web service that returns a DIME attachment

  1. For Web services that return a DIME attachment, change the return type of the Web service method to a byte array.

    When a Web service method returns more than one file, the Web service method must return a type that contains multiple byte arrays. For more details, see How to: Enable a Web Service to Send and Receive Large Amounts of Data.

    The following code examples show how to change the return type of a Web service method that returns a file to return a byte array instead.

    WSE 2.0

    <WebMethod()>  _
    Public Sub GetFile(ByVal filename As String)
    
    [WebMethod]
    public void GetFile(string fileName)
    

    WSE 3.0

    <WebMethod()> _
    Public Function GetFile(ByVal filename As String) As Byte()
    
    [WebMethod]
    public byte[] GetFile(string fileName)
    
  2. Change the code that reads the file into a DimeAttachment to instead read the file into a byte array.

    The following code examples show how to migrate a WSE 2.0 Web service method that reads a file into a DimeAttachment to read the file into a byte array and then return the byte array to the client.

    WSE 2.0

    <WebMethod()>  _
    Public Sub GetFile(ByVal filename As String)
       Dim respContext As SoapContext = ResponseSoapContext.Current
       Dim dimeAttach As New DimeAttachment("image/jpeg", TypeFormat.MediaType, filename)
       respContext.Attachments.Add(dimeAttach);
    End Sub 'GetFile
    
    [WebMethod]
    public void GetFile(string fileName)
    {
       SoapContext respContext = ResponseSoapContext.Current;
       DimeAttachment dimeAttach = new DimeAttachment("image/jpeg", TypeFormat.MediaType,
    fileName);
       respContext.Attachments.Add(dimeAttach);
    }
    

    WSE 3.0

    <WebMethod()> _
    Public Function GetFile(ByVal filename As String) As Byte()
        Dim response As Byte()
        Dim filePath As String = AppDomain.CurrentDomain.BaseDirectory + "App_Data\" + filename
        response = File.ReadAllBytes(filePath)
        Return response
    End Function
    
    [WebMethod]
    public byte[] GetFile(string fileName)
    {
    
        byte[] response;
        String filePath = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\" + fileName;
        response = File.ReadAllBytes(filePath);
        return response;
    }
    
  3. Specify that the Web service can accept 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 optional or always for the MTOM Mode.
      The optional MTOM Mode specifies that WSE processes incoming SOAP messages whether or not they are MTOM encoded and that all SOAP responses and SOAP faults match the MTOM encoding of the SOAP request.
      The always MTOM Mode specifies that all incoming and outgoing SOAP messages must be MTOM encoded. When a SOAP request is received that is not encoded using MTOM, a SOAP fault is returned to the sender.
    4. Click OK to dismiss the dialog.
      This adds an <mtom> Element to the Web service's Web.config file.

To migrate a client that receives DIME attachments from a Web service

  1. 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 SOAP messages are MTOM encoded unless client code explicitly sets the RequireMtom property of the proxy class to false.
    4. Click OK to dismiss the dialog.
      This adds an <mtom> Element to the client's app.config file.
  2. Change the code to receive a byte array instead of extracting a DimeAttachment from the SOAP response.

    The following code examples show how to migrate a WSE 2.0 client that extracts a DimeAttachment from the SOAP response.

    WSE 2.0

    Dim fileName As String = "Winter.jpg"
    Dim serviceproxy As MyDimeServiceWse = New MyDimeServiceWse
    serviceproxy.GetFile(fileName)
    If (serviceproxy.ResponseSoapContext.Attachments.Count = 1) Then
       pictureBox1.Image = New Bitmap( _
       serviceproxy.ResponseSoapContext.Attachments(0).Stream)
    End If
    
    String fileName = "Winter.jpg";
    MyDimeServiceWse serviceproxy = new MyDimeServiceWse();
    serviceproxy.GetFile(fileName);
    if (serviceproxy.ResponseSoapContext.Attachments.Count == 1)
    {
    pictureBox1.Image = new Bitmap(
    serviceproxy.ResponseSoapContext.Attachments[0].Stream);
    }
    

    WSE 3.0

    Dim fileName As String = "Winter.jpg"
    ' Create an instance of the Web service proxy class.
    Dim serviceproxy As New BinaryDataMTOMServiceWse()
    
    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();
    
    //  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
How to: Send and Receive Large Amounts of Data to and from a Web Service