将 MessageContract 作为 MessageBodyMember 放入 WCF 中的其他合约中
我可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用继承来定义两个消息契约之间的关系:
您不能按照
FileDataContract
中所述定义消息契约,因为您不能拥有消息头数组,而且 Stream 在大多数情况下可能是唯一可用的正文元素,并且必须只有一个。因此,如果您需要传递多个文件,您还应该实施一些压缩(zip)并发送单个流。You can use inheritance to define relation between two message contracts:
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.快速回答:不,你不能。消息契约是 SOAP 消息的顶层定义,而不是可以组合的东西。在您的示例中,您将
Files
成员定义在正文中,但其某些属性(FileName
、FileSize
)在标头不一致。如果您尝试这样做,它甚至可能“工作”,您不会看到任何错误,但这只是因为 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 theFileUploadInputParameter
type as a POCO serializable type.