Skip to main content

Streaming files over WCF

With all the REST buzz nowadays, using SOAP/WCF services seems so ‘old school’. But the right tool for the right job, so let’s move on and focus on the problem I want to handle: sending large files over WCF(I blogged about this before with some tips, but people keep asking questions, so I describe the process step by step).

Table Of Contents

  • Service Contract
  • Service Implementation
  • Configuring The Service
  • Hosting The Service in IIS

WCF Service Contract

Let’s start by laying the groundwork for the WCF Service. As stated the service must enable users to upload a file to the web server which hosts it. Thus the resulting service contract only contains one method, named Upload(…).

Open up Visual Studio 2010 and create a new blank solution. Next add a new Empty Web application called Sample.Services. Afterwards go to Add new item, choose  the WCF service template and type FileUploadService.cs as the name. This will automatically generate some files (IFileUploadService.cs, FileUploadService.cs) and update the web.config with some default configuration settings.

Open the IFileUploadService file and replace the generated code with the code in Listing 1:

Listing 1 – Service Contract

   1:  [ServiceContract(Namespace = "http://sample/wcf/services")]
   2:  public interface IFileUploadService
   3:  {
   4:      [OperationContract]
   5:      UploadResponse Upload(UploadRequest uploadRequest);
   6:  }

As you can see in Listing 1 above they mention two other classes, namely:

  1. UploadRequest
  2. UploadResponse

These classes are both decorated with the MessageContract attribute.

Note: WCF requires that the parameter that holds the data to be streamed must be the only parameter in the method.

Note: If your service operation contains a parameter of a type which has been decorated with the MessageContract attribute then all of the parameters and the return type used in this operation must be of a type to which this attribute has been applied.

You cannot mix parameters of a primitive type with message contracts. For instance if you want the Upload(…) method to return a boolean indicating if the upload succeeded or failed then you have to wrap this in another message contract.

The FileInfo class specifies the structure of a SOAP envelope for a particular message.

Listing 2 – FileInfo class

   1:  [MessageContract]
   2:  public class UploadRequest
   3:  {
   4:      [MessageHeader(MustUnderstand = true)]
   5:      public string FileName { get; set; }
   6:   
   7:      [MessageBodyMember(Order = 1)]
   8:      public Stream Stream { get; set; }
   9:  }

Note: By applying the MessageHeader attribute to the FileName and Length propery you place this information in the header of the SOAP message. When streaming a file the body of the SOAP message must only contain the actual file itself. By applying the MessageBodyMember attribute to the Stream property you place it in the body of the SOAP message.

Listing 3 displays how to setup the return value as a message contract. The response only contains a boolean value to indicate if the upload was successful.

Listing 3 – FileReceivedInfo class

   1:  [MessageContract]
   2:  public class UploadResponse
   3:  {
   4:      [MessageBodyMember(Order = 1)]
   5:      public bool UploadSucceeded { get; set; }
   6:  }

Service Implementation

Now it’s time to provide an actual implementation for the service contract.

Open the FileUploadService.cs file and replace the generated code with the code in Listing 4. The code is pretty straightforward. It reads the incoming stream and saves it to a file using familiar .NET code.

The Upload(…) method’s return type is of the UploadResponse type. If the upload succeeds the UploadSucceeded property is set to true, if it fails this property is set to false.

Listing 4 – Service Implementation

   1:  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
   2:      ConcurrencyMode = ConcurrencyMode.Single)]
   3:  public class FileUploadService : IFileUploadService
   4:  {
   5:      #region IFileUploadService Members
   6:   
   7:      public UploadResponse Upload(UploadRequest request)
   8:      {
   9:          try
  10:          {
  11:   
  12:              string uploadDirectory =
  13:                  ConfigurationManager.AppSettings["uploadDirectory"];
  14:   
  15:              // Try to create the upload directory if it does not yet exist
  16:              if (!Directory.Exists(uploadDirectory))
  17:              {
  18:                  Directory.CreateDirectory(uploadDirectory);
  19:              }
  20:   
  21:              // Check if a file with the same filename is already 
  22:              // present in the upload directory. If this is the case 
  23:              // then delete this file
  24:              string path = Path.Combine(uploadDirectory, fileInfo.FileName);
  25:              if (File.Exists(path))
  26:              {
  27:                  File.Delete(path);
  28:              }
  29:   
  30:              // Read the incoming stream and save it to file
  31:              const int bufferSize = 2048;
  32:              byte[] buffer = new byte[bufferSize];
  33:              using (FileStream outputStream = new FileStream(path,
  34:                  FileMode.Create, FileAccess.Write))
  35:              {
  36:                  int bytesRead = request.Stream.Read(buffer, 0, bufferSize);
  37:                  while (bytesRead > 0)
  38:                  {
  39:                      outputStream.Write(buffer, 0, bytesRead);
  40:                      bytesRead = request.Stream.Read(buffer, 0, bufferSize);
  41:                  }
  42:                  outputStream.Close();
  43:              }
  44:              return new UploadResponse
  45:                         {
  46:                             UploadSucceeded = true
  47:                         };
  48:          }
  49:          catch (Exception ex)
  50:          {
  51:              return new UploadResponse
  52:                         {
  53:                             UploadSucceeded = false
  54:                         };
  55:          }
  56:      }
  57:   
  58:      #endregion
  59:  }

Configuring The Host

Start by opening the web.config file. Next specify the directory in which the service should save the incoming files.

Listing 5 – Upload directory

   1:  <appSettings>
   2:    <add key="uploadDirectory" value="C:\temp\upload" />
   3:  </appSettings>

Now let’s first add a new behavior to our serviceBehaviors node.

Listing 6 – Service Behavior

   1:  <behaviors>
   2:    <serviceBehaviors>
   3:      <behavior name="FileUploadServiceBehavior">
   4:        <serviceMetadata httpGetEnabled="True" httpsGetEnabled="False" />
   5:        <serviceDebug includeExceptionDetailInFaults="False" />
   6:      </behavior>
   7:    </serviceBehaviors>
   8:  </behaviors>

The behavior specifies that the service should not propagate exception details and that it’s metadata should be shared over HTTP.

Next up is the binding for the service. A couple of its properties need to be tweaked to fit the needs of the service, namely:

  1. transferMode: Set it to Streamed to enable streaming.
  2. messageEncoding: The message encoding is set to MTOM encoding which is a mechanism for transmitting binary attachements with SOAP messages.
  3. maxReceivedMessageSize: Set to 64 megabytes to allow large files to be uploaded.
  4. maxBufferSize: Set to 64 kilobytes.
  5. receiveTimeout: Set to 10 minutes. If the file fails to upload within this time frame an exception will be thrown.

Note: Configure these settings according to the needs of your application.

Listing 7 – The Binding

   1:  <bindings>
   2:    <basicHttpBinding>
   3:      <!-- buffer: 64KB; max size: 64MB -->
   4:      <binding name="FileUploadServiceBinding"
   5:               transferMode="Streamed"
   6:               messageEncoding="Mtom"
   7:               maxReceivedMessageSize="67108864" maxBufferSize="65536"
   8:               closeTimeout="00:01:00" openTimeout="00:01:00"
   9:               receiveTimeout="00:10:00" sendTimeout="00:01:00">
  10:        <security mode="None">
  11:          <transport clientCredentialType="None" />
  12:        </security>
  13:      </binding>
  14:    </basicHttpBinding>
  15:  </bindings>

Last but not least is the configuration for the service itself.

Listing 8: FileUploadService configuration

   1:  <services>
   2:    <service behaviorConfiguration="FileUploadServiceBehavior"
   3:      name="Sample.Services.FileUploadService">
   4:      <endpoint address="" binding="basicHttpBinding" contract="Sample.Services.IFileUploadService"
   5:                bindingConfiguration="FileUploadServiceBinding">
   6:      </endpoint>
   7:    </service>
   8:  </services>

Hosting The Service

Now that the service has been setup it’s time to host it. Don’t try to host it in your ASP.NET Development Server(default option), instead choose IIS or the new IIS Express option (if installed). Therefore right click on your web application and choose Properties. Go to the Web tab and change Servers option from Use Visual Studio Development Server to Use Local IIS Web server.

That’s it!

Popular posts from this blog

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

Kubernetes–Limit your environmental impact

Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...

DevToys–A swiss army knife for developers

As a developer there are a lot of small tasks you need to do as part of your coding, debugging and testing activities.  DevToys is an offline windows app that tries to help you with these tasks. Instead of using different websites you get a fully offline experience offering help for a large list of tasks. Many tools are available. Here is the current list: Converters JSON <> YAML Timestamp Number Base Cron Parser Encoders / Decoders HTML URL Base64 Text & Image GZip JWT Decoder Formatters JSON SQL XML Generators Hash (MD5, SHA1, SHA256, SHA512) UUID 1 and 4 Lorem Ipsum Checksum Text Escape / Unescape Inspector & Case Converter Regex Tester Text Comparer XML Validator Markdown Preview Graphic Col...