Configure DIME with WSE 2.0

This article focuses on demonstrating the usage of DIME (Direct Internet Message Encapsulation) with Web Services. Here, you will have two applications, one is a Web Serivice that uses DIME technology to send an attachment (of different types) to a client side, other is a Windows Forms application.

Introduction

SOAP messages are not the best way to send large binary files. As the Web Services emerge, people look for Web services to have more and more functionality. Result was the introduction of Web Services Enhancement (WSE) 2.0. The Web Services Enhancements for Microsoft .NET (WSE) supports attaching files to SOAP messages outside the SOAP envelope; these files are not serialized as XML. This can be beneficial when sending large text or binary files because XML serialization is a very costly process and can result in files much larger than the originals. Direct Internet Message Encapsulation (DIME) is a lightweight, binary message format that WSE uses to encapsulate SOAP messages and their attachments in a DIME message.

Background

First, we create a Web service that can read an image file, and return a DIME message that contains a SOAP message and the image file attached.

Second, we create a Windows Forms client that can consume the DIME message created by the Web service and display the image.

Requirements

The users who read this article need a basic understanding of Web services and how to create and use them with .NET Framework. Additionally, they need to have the Microsoft Visual Studio .NET 2003 and Web Service Enhancement 2.0 installed.

Web service that generates a DIME message containing image attachments

Note: Because DIME attachments are contained outside of the SOAP envelope, they cannot be signed or encrypted using WSE. It is strongly recommended that all SOAP messages that contain DIME attachments be sent over a secure transport protocol such as HTTPS.

Note: If you send a file as a DIME attachment, you cannot close or delete the file from within the Web method that sent the file. This is because WSE holds a reference to the file until the message is serialized and delivered to the client. To overcome this, override a file stream and delete the file when all references to the file stream are released.

Let's create the Visual Studio ASP/.NET Web service project:

Go to File menu and create a New Project.

In the Project Types pane, select Visual C# Projects.

In the Templates pane, select ASP.NET Web Service.

Fill the Location box with the project name: http://localhost/MyDimeTest.

Click OK button to add the MyDimeTest project to the solution.

Go to Solution Explorer and change the name of Service1.asmx to DimeService.asmx by selecting the Properties of the file.

To be smarter, view the DimeService.asmx and change the class name as well as the constructor name to have the same name, i.e., "DimeService".

Now the project is done, and now you got to configure the project for WSE 2.0. It is very easy.

In the Solution Explorer, right click on the project name, you will see "WSE 2.0 Settings ..." just below the "Property" menu item.

Select "WSE 2.0 Settings ...".

To configure a Web service project to use WSE 2.0, you got to check both check boxes in the "General" tab.

The first check box enables the use of the current project with WSE. This means that the Microsoft.Web.Services2.dll will be added to the project references, and that changes will be made to the Web.config file to add support for the WSE configuration handler. In addition, any Web references that are created from this point on will include WSE 2.0 support in the proxy classes generated.

The second check box is only enabled if this is an ASP.NET Web Service project. By selecting it, the WSE Soap Extension is added to the project, which will enable the additional protocol support to work within the ASP.NET Web service HTTP handler (for .ASMX files). This is accomplished by modifying the Web.config file and adding the WSE SOAP Extension to the list of .asmx SOAP Extensions for the virtual directory.

Add using statement to the DimeService.asmx.cs file:

using Microsoft.Web.Services2.Dime;
using Microsoft.Web.Services2;
using System.Net;

Place an image file to be read by the DimeService. In my case, I placed a "test.gif" file in my "c:" drive. So the path of the image file is described as "c:\test.gif".

Write a Web Method that can read the image file and attach it with the SOAP message using DIME technology and transfer it to the client end. Please follow the code below:

Now the client project is done, and now you got to configure the project for WSE 2.0. It is very easy.

In the Solution Explorer, right click on the project name, you will see "WSE 2.0 Settings ..." just below the "Property" menu item.

Select "WSE 2.0 Settings ...".

To configure a Web service project to use WSE 2.0, you got to check the first check box in the "General" tab. (I.e., enable this project for Web Service enhancements.) This will add all necessary references as well as it will edit the Web.Config for you.

DIME Defaults

By default, DIME/ WSE 2.0 doesn't allow adding/sending attachments larger than 4096 KB of size. If you intend to send larger files, which are over the 4 MB limit (including the SOAP message), you need to add some tags to the web.config file of the service as well as, remember, of the receiver (client) end. You need to add values to override the values of the maxRequestLengths.

<httpRuntime> Settings

This configures the ASP.NET HTTP runtime settings. This section can be declared at the machine, site, application, and subdirectory levels:

<httpRuntimeuseFullyQualifiedRedirectUrl="true|false"maxRequestLength="size in kbytes"executionTimeout="seconds"/>

Example

This example demonstrates a instant of sending an attachment of size 8 MB and the timeout is set to 45 seconds:

Share

About the Author

In-depth coverage of Microsoft .Net, Cloud and many other cutting-edge Technologies.

- The Mandelbrot set – someone has called it the thumb-print of God – is one of the most beautiful and remarkable discoveries in the entire history of mathematics. My profile picture is generated with that equation.

Comments and Discussions

I was trying to implement sending image attachment through DIME in .NET and I came across your example, I did exactly the samething that you had mentioned in the example.

First I installed the WSE 2.0 and WSE 3.0 on my machine, and after creating the web service when i right clicked on the project for configuring the WSE 2.0 settings I found only the WSE 3.0 settings in the menu and I did the changes that you had mentioned to that in the web service project and as well as in the client project, then I compiled the web service project it was successfull and then I deployed the web service in the IIS, and in the client project I added this web service as web reference and tried to compile the client project then it threw the error which states as below.

Error 1 The type or namespace name 'DimeServiceWse' does not exist in the namespace 'DimeClient.pxy' (are you missing an assembly reference?) E:\Dot Net 2005 Projs (MOM)\DimeClient\DimeClient\Form1.cs 20 17 DimeClient

My question is why it is not showing the WSE 2.0 settings in the menu eventhough the WSE 2.0 is installed on the system ?

Can I send the attachments by using WSE 3.0 ?, if so what changes do i need to make in the code ?

After carrying out a little bit of research work I came to know the reason why the WSE 2.0 settings is not getting displayed ? the thing is that the WSE 2.0 is made for the .NET 1.1 framework i.e. for the visual studio .NET 2003.

In the previous post I forgot to mention that I am using visual studio .NET 2005, so I need to use only WSE 3.0

Is there any classes are there in WSE 3.0 like DIME as in WSE 2.0 for sending the attachments through SOAP.

WSE 3.0 is using a new method called MTOM to send attachment, or rather binary data.

Pros

Once you are configured with MTOM you may just return the byte arrary of the file just like any other return type..
e.g.
[WebMethod]
byte[] GetMyFile()
{
return the byte array of the file
}

Once you have installed WSE 3.0 and your application is configured with MTOM the system will recognize the data type and treat it in a special way and allow you to transfer the file afficiently.

Cons

If your client if only capable of reading a DIME attachement (Third parties who are having dime parsers) will not be able to read the message return type since it is compressed and optimized for binary trafsfering

Carefully.. Step by step.. read this document and the one you referred to.. then you will see the difference.. if you prove that there is not, then just post a reply here and I will delete this article..

Hi All,
Just in case, my DIME running server in Korea and I used it in Australia. I got error message something like "Creation time in the timestamp can not be in the further" because of the time difference. So the solution is <timeToleranceInSeconds>any seconds</timeToleranceInSeconds> in both web.config and app.config.

I have a devlopment server where I have installed the WSE 2.0 SP3
but i get the error DmiServiceWse does not exist.I can not install any other because the VS.NET is sheared from a single server(where it is installed)
On the server I have installed the WSE 2.0 SP3 Admin for runtime.

Now i am testing the webservice using the URI
http://localhost/MyDimeTest/DimeService.asmx/CreateDimedImage
and i get error Object reference not set "respContext is null"
I have added the appropriate configuration params in the web.config

I have a devlopment server where I have installed the WSE 2.0 SP3
but i get the error DmiServiceWse does not exist.I can not install any other because the VS.NET is sheared from a single server(where it is installed)
On the server I have installed the WSE 2.0 SP3 Admin for runtime.

Now i am testing the webservice using the URI
http://localhost/MyDimeTest/DimeService.asmx/CreateDimedImage
and i get error Object reference not set "respContext is null"
I have added the appropriate configuration params in the web.config

I think I know the problem I had the same problem when I first installed SP3, SP3 itself by default install a personal firewall. You need to talk to your friendly network admin and ask him to disable the firewall or ask him to give needed permission for your web service to work with DIME.

I can download over 4Mb of any files using DIME but can't upload over 4Mb of files. I've got HTTP 400 Bad Request error after I changed the web.config file as you stated ( maxRequestLength over 4000 ), if I don't change the maxRequest size, saying over limitation of size.

sorry for the late reply.. when u say upload you mean it is from web service end to a client application right?

Any way, if download is OK then the other way should automatically be OK. According to your error I think it has something to do with the request you made, I mean the error itself discribe that, so are u sure that the one which produce the error is giving a valid request?

while saying this, I must accept that the error you see is indeed bit confusing.. can some one else help us here..

I've solved it, I mean I put the recommended code only in web.config file and it did not work, but when it put the code in App.config as well it was working. It was my misunderstanding. Anyway, thanks again.

That is a common mistake and that is why I have said "remeber" specifically when I explain that part in my article.

But according to your given explaination, you said it is working one way, but if that is the mistake, then you cannot get it work one way as well, so that lead me not to guess that you have done that common mistake..

I think you want to show the user a progress bar while a huge attachement is receiving.. If the attachement is a zip file and if you want to do this easily, the best thing I can think of is by breaking your attached file in to small parts and get them through sequence of calls and let the progress bar show the progress as each part receives..

but these is some thing I have never thought about.. but I cannot see a direct way to see the progress through the web service proxy .

The answer is a bit of a guess work.. I think as long as both compliant with WSDL 1.1 standard either party should be able to communicate both ways.

But when u ask DIME, DIME is a standard so it also should support cross platform communication. But let me do a bit of searching and get back to you, if you find it before me pls let me know the result.