WCF: using streaming with Message Contracts

Stefano Ricciardi picture Stefano Ricciardi · Aug 27, 2009 · Viewed 23.6k times · Source

I am trying to use the WCF streaming with Message Contracts, because I need additional parameters beside the stream itself.

Basically I am creating a file upload and download service, with some additional logic on top.

Unfortunately, when I try to hit the service from the browser to check that everything is all right, I get the following error:

Server Error in '/' Application. Operation 'UploadFile' in contract 'IFileTransferService' uses a MessageContract that has SOAP headers. SOAP headers are not supported by the None MessageVersion.

Unfortunately googling for it did not yield any significant result that helped me. Can you guys have help me out? Here the details of the service (I have removed the download part for reason of space).

[ServiceContract(Namespace = "http://www.acme.org/2009/04")]
public interface IFileTransferService
{
    [OperationContract(Action = "UploadFile")]
    void UploadFile(FileUploadMessage request);
}

[MessageContract]
public class FileUploadMessage
{
    [MessageHeader(MustUnderstand = true)]
    public FileMetaData Metadata { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream FileByteStream { get; set; }
}

[DataContract(Namespace = "http://schemas.acme.org/2009/04")]
public class FileMetaData
{
    [DataMember(Name="FileType", Order=0, IsRequired=true)]
    public FileTypeEnum fileType;

    [DataMember(Name="localFilename", Order=1, IsRequired=false)]
    public string localFileName;

    [DataMember(Name = "remoteFilename", Order = 2, IsRequired = false)]
    public string remoteFileName;
}

I have tried to use both basichttpbinding and a customhttp binding with not positive effect:

<customBinding>
    <binding name="customHttpBindingStream">
        <textMessageEncoding messageVersion="Soap12" />
        <httpTransport transferMode="Streamed" maxReceivedMessageSize="2147483647"/>
    </binding>
</customBinding>

UPDATE: reading the documentation online it seems that streaming with MessageContracts should indeed be possible. Refer for example to MSDN (Large Data and Streaming):

Programming Model for Streamed Transfers

The programming model for streaming is straightforward. For receiving streamed data, specify an operation contract that has a single Stream typed input parameter. For returning streamed data, return a Stream reference. [...] This rule similarly applies to message contracts. As shown in the following message contract, you can have only a single body member in your message contract that is a stream. If you want to communicate additional information with the stream, this information must be a carried in message headers. The message body is exclusively reserved for the stream content.

[MessageContract]
public class UploadStreamMessage
{
   [MessageHeader]
   public string appRef;
   [MessageBodyMember]
   public Stream data;
} 

I have also seen blog posts from people accomplishing services of file upload and download very similar to what I am trying to put together (for example here).

UPDATE 2 I have tried creating a small console and self hosting the service with a basicHttpBinding and there it works like a charm. I am starting to believe that the problem might be the hosting on IIS. Any idea?

UPDATE 3 See my own answer.

Answer

Stefano Ricciardi picture Stefano Ricciardi · Aug 28, 2009

I finally found out what was the error: it had nothing to do with Soap versions, streams, etc... I just mispelled the name of my own service (!), using FileTransfer instead of FileTransferService.

In the end basicHttpBinding was perfectly fine, I didn't need to resort to a custom binding.

Original (bad) version:

<service 
    behaviorConfiguration="serviceBehavior"
    name="Acme.Service.FileTransfer">
    <endpoint address="" 
        name="basicHttpStream" 
        binding="basicHttpBinding"
        bindingConfiguration="httpLargeMessageStream" 
        contract="Acme.Service.IFileTransferService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

New (fixed) version:

<service 
    behaviorConfiguration="serviceBehavior"
    name="Acme.Service.FileTransferService">
    <endpoint address="" 
        name="basicHttpStream" 
        binding="basicHttpBinding"
        bindingConfiguration="httpLargeMessageStream" 
        contract="Acme.Service.IFileTransferService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

Still I can't say that the error message was helpful in any way to understand what was going on here....

If you are interested in the whole service, you can find more details on my blog at the following link: File Transfer with WCF