简体   繁体   English

WCF中MessageContract的KnownType

[英]KnownType for MessageContract in WCF

I am using Stream object inside my wcf Contracts so forced to use MessageContract instead of DataContract . 我在我的wcf合同中使用Stream对象,因此强制使用MessageContract而不是DataContract

 [MessageContract]
    public class Document 
    {
        [MessageBodyMember(Order = 1)]
        public System.IO.Stream FileData;

}

 [MessageContract]
    public class A : Document 
    {
        [MessageBodyMember]
        public string input;

}

 [MessageContract]
    public class B : Document 
    {
        [MessageBodyMember]
        public string someProp;

}

[ServiceContract]
    public interface ISomeService
    {

        [OperationContract]
        Document SomeMethod(Document file);
}

I want the consumer of this service to create object of either A or B and call the service with it. 我希望此服务的使用者创建A或B的对象并使用它来调用服务。 On the service side, I can type cast it to proper object then perform some action. 在服务方面,我可以将其转换为适当的对象,然后执行一些操作。

Problem is I cannot specified KnownType with MessageContract and inherited contracts cannot be exposed to client until they are used in service or declared with KnownType . 问题是我无法使用MessageContract指定KnownType ,并且继承的合同在服务中使用或使用KnownType声明之前不能向客户端公开。

I tried google it but couldn't find anything related to KnownType with MessageContract . 我试过google但是找不到任何与MessageContract相关的KnownType

As suggested in comment... i updated my message contract with KnownType but they are still not exposed to client through service reference... 正如评论中所建议的......我使用KnownType更新了我的消息合同,但它们仍未通过服务引用向客户端公开...

[MessageContract]
    [KnownType(typeof(FileSystemStoredDocument))]
    [KnownType(typeof(FileBoundStoredDocument))]
    [KnownType(typeof(SharepointStoredDocument))]

    public class Document : DocumentInfo, IDisposable
    {
}

Can any one help me what's wrong here? 任何人都可以帮我解决这里的错误吗?

Note : ALL KnownType are inherited from Document 注意 :所有KnownType都是从Document继承的

Message contracts describe exactly how the message should look like . 消息合同准确描述了消息的外观 They do support inheritance, but you must specify the exact message contract you're using in a specific operation. 它们确实支持继承,但您必须指定在特定操作中使用的确切消息协定。

If you check the body parts of the message: 如果您检查邮件的正文部分:

ContractDescription.GetContract(typeof(ISomeService)).Operations[0].Messages[0].Body.Parts

You'll see exactly one part - a Stream object. 你会看到一个部分 - 一个Stream对象。 That's in contrast to data contracts, where the body contains a part of the type Object . 这与数据契约形成对比,数据契约的主体包含Object类型的一部分。 So you see why KnownType wouldn't work here . 所以你明白为什么KnownType在这里不起作用

(The ContractDescription class is used, among other things, to generate WSDL. See the WsdlExporter class.) (除其他外,使用ContractDescription类生成WSDL。请参阅WsdlExporter类。)

What you can do is create a hierarchy of data contracts that would be contained in the message contract, eg 可以做的是创建一个包含在消息合同中的数据合同层次结构,例如

[MessageContract]
public class Document 
{
    [MessageHeader]
    public DocumentProperties Properties;

    [MessageBodyMember(Order = 1)]
    public System.IO.Stream FileData;
}

[DataContract]
[KnownType(typeof(A))]
[KnownType(typeof(B))]
public abstract class DocumentProperties { }

[DataContract]
public class A : DocumentProperties 
{
    [DataMember]
    public string input;
}

[DataContract]
public class B : DocumentProperties 
{
    [DataMember]
    public string someProp;
}

Note that you you cannot have more than one body member if you want to pass a Stream , so the rest of the properties must be in headers. 请注意,如果要传递Stream ,则不能拥有多个正文成员,因此其余属性必须位于标题中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM