Silverlight 不喜欢我的 WCF MessageContract.为什么? [英] Silverlight is not liking my WCF MessageContract. Why?

查看:20
本文介绍了Silverlight 不喜欢我的 WCF MessageContract.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下 MessageContract 通过 Silverlight 客户端上传文件:

I am attempting to upload a file through a Silverlight client using the following MessageContract:

[MessageContract]
public class CategoryClientFileTransferMC : IDisposable
{
    /// <summary>
    /// CategoryID - Category identity.
    /// </summary>
    [MessageHeader(MustUnderstand = true)]
    public int CategoryID;

    /// <summary>
    /// ID - File identifier.
    /// </summary>
    [MessageHeader(MustUnderstand = true)]
    public string ID;

    /// <summary>
    /// Length - File length in bytes.
    /// </summary>
    [MessageHeader(MustUnderstand = true)]
    public long Length;

    /// <summary>
    /// FileByteStream - File stream.
    /// </summary>
    [MessageBodyMember(Order = 1)]
    public Stream FileByteStream;

    /// <summary>
    /// Dispose the contract.
    /// </summary>
    public void Dispose()
    {
        if (FileByteStream != null)
        {
            FileByteStream.Close();
            FileByteStream = null;
        }
    }
}

我的问题是客户端生成的操作方法只有一个参数;一个名为 FileByteStream 的字节数组.在我创建的其他(非 Silverlight)客户端中,它也要求 MemberHeader 字段.如果不指定这些标头,服务器将不知道如何处理该文件.调用操作时如何设置这些标头?

My problem is that the generated operation method on the client only takes a single argument; a byte array called FileByteStream. In other (non-Silverlight) clients I've created it asks for the MemberHeader fields as well. Without specifying these headers, the server has no idea what to do with the file. How can I set these headers when I call the operation?

另外,有没有更好的方法从 Silverlight 客户端上传文件?这让我很头疼.

Also, is there a better way to upload a file from a Silverlight client? This has been a huge headache.

谢谢.

推荐答案

WCF 客户端的 Silverlight 子集不支持 [MessageHeader] 属性.您仍然可以设置消息标题,但它不像其他平台那样简单.基本上,您需要在调用之前使用操作上下文设置标头,如下例所示:

The Silverlight subset of the WCF client does not support the [MessageHeader] attribute. You can still set message headers, but it's not as straightforward as in other platforms. Basically, you'll need to set the headers using the operation context, prior to making the call, like in the example below:

var client = new SilverlightReference1.MyClient();
using (new OperationContextScope(client.InnerChannel))
{
    string contractNamespace = "http://tempuri.org/";
    OperationContext.Current.OutgoingMessageHeaders.Add(
        MessageHeader.CreateHeader("CategoryId", contractNamespace, 1));
    OperationContext.Current.OutgoingMessageHeaders.Add(
        MessageHeader.CreateHeader("ID", contractNamespace, "abc123"));
    OperationContext.Current.OutgoingMessageHeaders.Add(
        MessageHeader.CreateHeader("Length", contractNamespace, 123456L));
    client.UploadFile(myFileContents);
}

其中 contractNamespace 是消息头字段的 XML 命名空间(IIRC,它们默认与服务合同相同).您可以使用 Fiddler 和类似 WCF 测试客户端的东西来查看那里使用了哪个命名空间.

Where contractNamespace is the XML namespace for the message header fields (IIRC they default to the same as the service contract). You can use Fiddler and something like the WCF Test Client to see which namespace is used there.

这篇关于Silverlight 不喜欢我的 WCF MessageContract.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆