Silverlight is not liking my WCF MessageContract. Why?

前端 未结 1 1521
猫巷女王i
猫巷女王i 2021-01-14 12:41

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

[MessageContract]
public class CategoryClient         


        
相关标签:
1条回答
  • 2021-01-14 13:29

    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.

    0 讨论(0)
提交回复
热议问题