Giardino
Giardino

Reputation: 1387

WCF message contract missing headers when added as service reference

I have the following MessageContract in my wcf service:

  [MessageContract]
    public class RemoteFileInfo : IDisposable
    {

        private string fileName;
        private long length;
        private System.IO.Stream fileByteStream;

        [MessageHeader(MustUnderstand = true)]
        public string FileName
        {
            set { this.fileName = value; }
            get { return this.fileName; }
        }

        [MessageHeader(MustUnderstand = true)]
        public long Length
        {
            set { this.length = value; }
            get { return this.length; }
        }

        [MessageBodyMember(Order = 1)]
        public System.IO.Stream FileByteStream
        {
            set { this.fileByteStream = value; }
            get { return this.fileByteStream; }
        }

        public void Dispose()
        {
            if (fileByteStream != null)
            {
                fileByteStream.Close();
                fileByteStream = null;
            }
        }
    }

Then I have the actual implementation:

public void uploadFile(RemoteFileInfo request)
    {
        FileStream targetStream = null;
        Stream sourceStream = request.FileByteStream;

        //Tempdir
        string uploadFolder = @"C:\upload\";

        string filePath = Path.Combine(uploadFolder, request.FileName);
        using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            // Read from the input stream in 65000 byte chunks
            const int bufferLen = 65000;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
            {
                //save to output stream
                targetStream.Write(buffer, 0, count);
            }
            targetStream.Close();
            sourceStream.Close();
        }
    }

I know that metro apps are limited and not able to access the MessageHeader type of a MessageContract, so how would I set these values? Whenever I add a service reference to my metro app the only property of RemoteFileInfo that I am able to access is FileByteStream while the two other properties (headers) are totally hidden.

I also need to make sure that when calling the uploadFile method that it doesn't throw any exceptions complaining about missing properties from the passed RemoteFileInfo

Any ideas?

Upvotes: 2

Views: 470

Answers (1)

Hozikimaru
Hozikimaru

Reputation: 1156

I think the way to get around this would be creating another MessageBodyMember.

[MessageContract]
public class RemoteFileInfo : IDisposable
{

    private string fileName;
    private long length;
    private System.IO.Stream fileByteStream;

    public RemoteFileInfo()
    { 

    }

    [MessageHeader(MustUnderstand = true)]
    public string FileName
    {
        set { this.fileName = value; }
        get { return this.fileName; }
    }

    [MessageHeader(MustUnderstand = true)]
    public long Length
    {
        set { this.length = value; }
        get { return this.length; }
    }

    [MessageBodyMember(Order = 1)]
    public System.IO.Stream FileByteStream
    {
        set { this.fileByteStream = value; }
        get { return this.fileByteStream; }
    }

    [MessageBodyMember(Order = 2)]
    public Header CustomHeader
    {
        set { this.fileName = FileName; this.length = Length; }
        get { return this.CustomHeader; }
    }

    public void Dispose()
    {
        if (fileByteStream != null)
        {
            fileByteStream.Close();
            fileByteStream = null;
        }
    }
}

public class Header
{
    public string FileName { get; set; }
    public long Length { get; set; }
}

This is basically a custom header setter and when you are sending the request you should see the Header object and should be able to set it now.

Upvotes: 1

Related Questions