Silverlight is not liking my WCF MessageContract. Why?

吃可爱长大的小学妹 提交于 2019-12-01 08:42:38

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!