添加为服务引用时,WCF消息合同缺少标头

时间:2014-12-18 14:47:38

标签: c# wcf

我的wcf服务中有以下MessageContract

  [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;
            }
        }
    }

然后我有实际的实现:

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();
        }
    }

我知道metro应用程序有限且无法访问MessageHeader MessageContract类型,因此如何设置这些值?每当我向我的metro应用程序添加服务引用时,我能够访问RemoteFileInfo的唯一属性是FileByteStream,而其他两个属性(标题)完全隐藏。

我还需要确保在调用uploadFile方法时,它不会抛出任何异常,抱怨传递的RemoteFileInfo

缺少属性

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为解决这个问题的方法是创建另一个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; }
}

这基本上是一个自定义标题设置器,当您发送请求时,您应该看到Header对象,并且应该能够立即设置它。