Handling binary data with Web Service Enhancements 3.0
In one of my recent projects, I needed to expose a web service to upload some files to the server. Hence, decided to use array of bytes to transfer contents of these files over the wire. The problem, however, was the fact that these files were as huge (at times even ten’s of MBS). Sending and receiving these huge arrays of bytes over the web was a real pain in the neck. Thanks to Microsoft’s Web Service Enhancement (WSE) 3.0 which proved to be the perfect answer to our problem.
In this article we will examine how easy and efficient it is to send and receive large binary data using WSE 3.0. WSE 3.0 uses MTOM encoding and does not require any form of SOAP attachment. If you have not downloaded WSE 3.0 yet, you can do the same from here. We have used C# as the language and Visual studio 2005 as the IDE in the example that follows.
Create a Web service which Uploads/download large binary data
1. Create a web service called FileUploadService using visual studio 2005.
2. Add two methods called UploadFile and DownloadFile
|
[WebMethod]
public void UploadFile(string fileName, byte[] fileData)
{
string filePath = Server.MapPath("Uploads");
filePath = filePath + @"\";
File.WriteAllBytes(filePath + fileName, fileData);
}
|
|
[WebMethod]
public byte[] DownloadFile(string fileName)
{
string filePath = Server.MapPath("Uploads");
filePath = filePath + @"\";
return File.ReadAllBytes(filePath + fileName);
}
|
Make your service WSE enabled
1. Install WSE 3.0.
2. After the installation is complete, right click on the web service project and select WSE Settings 3.0 from the context menu.
3. From the displayed dialog and on General tab select the Enable this project for Web Services Enhancements check box and Enable Microsoft Web Services Enhancement Soap Protocol Factory check box and click OK.
4. In your web.config, you’ll notice that a new configuration section is added:
|
<configuration>
<configSections>
<sectionname="microsoft.web.services3"
type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration,
Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
</configSections>
...
</configuration>
|
A reference to the Microsoft.Web.Services3 assembly is added
|
<assemblies>
<addassembly="Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" />
</assemblies>
...
|
Under the system.web element, the <webServices> configuration element is defined. Here the Web service is configured to use the Microsoft Web Services Enhancement Soap Protocol factory WseProtocolFactory which is defined by the <soapServerProtocolFactory> configuration element under the <webServices> element. This is a must for a web service to be able to use WSE 3.0. ...
|
<webServices>
<soapExtensionImporterTypes>
<addtype="Microsoft.Web.Services3.Description.WseExtensionImporter,
Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
</soapExtensionImporterTypes>
<soapServerProtocolFactorytype="Microsoft.Web.Services3.WseProtocolFactory,
Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
</webServices>
|
Enable a Web Service to Send and Receive Large Amounts of Data
Select Messaging tab from the dialog. On MTOM Settings ensure that Server Mode is set to optional and click OK.
Following Server Mode option are available:
· In optional mode the WSE processes the incoming SOAP messages whether or not they are MTOM encoded. With optional the client is the one who decide whether to use MTOM or not, if the client application request to use MTOM the web service will use MTOM.
· In always mode all incoming and outgoing SOAP messages must be MTOM encoded. When a SOAP request is received that is not encoded using MTOM, an HTTP error 415: "Media unsupported" is returned to the sender.
· In never mode all incoming SOAP messages must not be MTOM encoded. When a SOAP request is received that is encoded using MTOM, an HTTP error 415: "Media unsupported" is returned to the sender.
Again open the web.config file and review the changes made by the WSE 3.0 configuration tool. You’ll notice the following:
A <messaging> configuration element is defined under the <microsoft.web.services3> configuration. This is used to specify the MTOM options.
|
<microsoft.web.services3>
<messaging>
<mtomserverMode="optional" />
</messaging>
</microsoft.web.services3>
|
Create a client Application
1. Create an asp.net website.
2. Add a fileUpload control and a button control to the default page.
|
<formid="form1"runat="server">
<div>
<table>
<tr>
<td>
Choose a file
</td>
<td>
<asp:FileUploadID="filUpload"runat="server"/>
</td>
<td>
<asp:ButtonID="btnUpload"
runat="server"Text="Upload"
OnClick="btnUpload_Click" />
</td>
</tr>
</table>
</div>
</form>
|
Make the client Application WSE-enabled
1. Right click on the website ans select WSE 3.0 properties. From the displayed dialog and on General tab select the Enable this project for Web Services Enhancements check box and Enable Microsoft Web Services Enhancement Soap Protocol Factory check box and click OK.
2. In the Messaging tab, specify client Mode as “On” and Server Mode as “always”. By spcifying client mode “on”, we indcate that the configuration is for the client and Server Mode “always” means that all incoming and outgoing SOAP messages must be MTOM encoded.
Call the Web service
The client now is ready to call the WSE-enabled FileUpload/FileDownload web-method. Drop in the following code:
|
private void UploadFile()
{
FileServiceWse service = new FileServiceWse();
service.UploadFile(filUpload.FileName, filUpload.FileBytes);
Response.Write("File uploaded.");
}
|
Conclusion
Here was an attempt to share my first exciting encouter with Microsoft's WSE 3.0. The idea was to illustrate how easy and efficient it is to use the WSE 3.0 to handle large volume of binary data over the web. Hope you guys find this effort useful
.