将 MessageContract 作为 MessageBodyMember 放入 WCF 中的其他合约中

发布于 2024-12-11 16:39:45 字数 998 浏览 0 评论 0原文

我可以在 WCF 中使用递归 MessageContract 吗? 例如:

我需要发布一些参数,其中一个参数是文件流数组。 这是我的操作合同:

DomainResult AddSomethingNew(int externalCustomerId, string domainName, bool isDefault, FileDataContract[] files);

这是我的消息合同:

[MessageContract]
public class FileDataContract
{        
    [MessageHeader(MustUnderstand=true)]
    public int ExternalCustomerId { get; set; }

    [MessageHeader(MustUnderstand=true)]
    public string DomainName{get;set;}

    [MessageHeader(MustUnderstand=true)]
    public bool IsDefault{get;set;}

    [MessageBodyMember(Order=1)]
    public FileUploadInputParameter[] Files { get; set; }
}

[MessageContract]
public class FileUploadInputParameter
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName { get; set; }

    [MessageHeader(MustUnderstand = true)]
    public decimal FileSize { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream FileStream { get; set; }
}

需要你的帮助..

Can I use recursive MessageContract in WCF ?
for example :

I need to post some parameters, one of the parameters is an array of file stream.
this is My OperationContract :

DomainResult AddSomethingNew(int externalCustomerId, string domainName, bool isDefault, FileDataContract[] files);

here is my MessageContract :

[MessageContract]
public class FileDataContract
{        
    [MessageHeader(MustUnderstand=true)]
    public int ExternalCustomerId { get; set; }

    [MessageHeader(MustUnderstand=true)]
    public string DomainName{get;set;}

    [MessageHeader(MustUnderstand=true)]
    public bool IsDefault{get;set;}

    [MessageBodyMember(Order=1)]
    public FileUploadInputParameter[] Files { get; set; }
}

[MessageContract]
public class FileUploadInputParameter
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName { get; set; }

    [MessageHeader(MustUnderstand = true)]
    public decimal FileSize { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream FileStream { get; set; }
}

need your helps..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-12-18 16:39:45

您可以使用继承来定义两个消息契约之间的关系:

[MessageContract]
public class FileUploadInputParameter
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName { get; set; }

    [MessageHeader(MustUnderstand = true)]
    public decimal FileSize { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream FileStream { get; set; }
}

[MessageContract]
public class FileDataContract : FileUploadInputParameter
{        
    [MessageHeader(MustUnderstand=true)]
    public int ExternalCustomerId { get; set; }

    [MessageHeader(MustUnderstand=true)]
    public string DomainName{get;set;}

    [MessageHeader(MustUnderstand=true)]
    public bool IsDefault{get;set;}
}

您不能按照 FileDataContract 中所述定义消息契约,因为您不能拥有消息头数组,而且 Stream 在大多数情况下可能是唯一可用的正文元素,并且必须只有一个。因此,如果您需要传递多个文件,您还应该实施一些压缩(zip)并发送单个流。

You can use inheritance to define relation between two message contracts:

[MessageContract]
public class FileUploadInputParameter
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName { get; set; }

    [MessageHeader(MustUnderstand = true)]
    public decimal FileSize { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream FileStream { get; set; }
}

[MessageContract]
public class FileDataContract : FileUploadInputParameter
{        
    [MessageHeader(MustUnderstand=true)]
    public int ExternalCustomerId { get; set; }

    [MessageHeader(MustUnderstand=true)]
    public string DomainName{get;set;}

    [MessageHeader(MustUnderstand=true)]
    public bool IsDefault{get;set;}
}

You cannot define message contract as you described in FileDataContract because you cannot have array of message headers and more over Stream can be in most cases the only available body element and it must be only one. So if you need to pass multiple files you should also implement some compression (zip) and send single stream.

旧人哭 2024-12-18 16:39:45

快速回答:不,你不能。消息契约是 SOAP 消息的顶层定义,而不是可以组合的东西。在您的示例中,您将 Files 成员定义在正文中,但其某些属性(FileNameFileSize)在标头不一致。如果您尝试这样做,它甚至可能“工作”,您不会看到任何错误,但这只是因为 WCF 会将 FileUploadInputParameter 类型视为 POCO 可序列化类型。

Quick answer: no, you can't. Message contract is a top-level definition of a SOAP message, not something which can be composed. In your example, you define the Files member to be in the body, but some of its properties (FileName, FileSize) to be in the header which is not consistent. If you try that it may even "work", in a way that you won't see any errors, but just because WCF will be treating the FileUploadInputParameter type as a POCO serializable type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文