WCF返回一个包含流的自定义对象集合的自定义对象 [英] WCF returning a custom object with a collection of custom objects containing streams

查看:362
本文介绍了WCF返回一个包含流的自定义对象集合的自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可以做,但我有一个WCF服务,应该返回一个自定义对象,该对象有另一个自定义对象的集合包含流。



当我尝试返回这个对象我得到



System.Runtime.Serialization.InvalidDataContractException:Type'System.ServiceModel.Dispatcher.StreamFormatter + MessageBodyStream'无法序列化。考虑使用DataContractAttribute属性来标记它,并标记要使用DataMemberAttribute属性序列化的所有成员。有关其他支持的类型,请参阅Microsoft .NET Framework文档。



如果我更改为方法,只返回一个流与Stream作为返回类型,它工作正常。这将是太多的代码,我发布,所以我只是想知道一般如果它是可能的,如果有某些特殊的我必须做的获取自定义对象与流从WCF服务返回没有错误?



我在测试的时候使用wsHttpBindig。



我在类中标记了流和IList作为DataMembers,



感谢任何帮助,如果它不能理解我可以尝试创建一个smal示例代码

你真的想要流式传输发生,或者你只是想让它序列化p> 如果您确定要缓冲它:



请记住,DataContractSerializer没有对Streams的内置支持,但它适用于字节数组。所以,做通常的DataContract类型转换技巧:不要使用DataMember标记流,而是创建一个包含Stream的类型为byte []的私有[DataMember]属性。例如:

  public Stream myStream; 

[DataMember(Name =myStream)]
private byte [] myStreamWrapper {
get {/ * convert myStream to byte [] here * /}
set {/ * convert byte [] to myStream here * /}
}

如果您确实想要它流式传输:



如果流是整个主体,WCF ServiceModel只能支持流式消息体。所以,你的操作应该返回一个MessageContract,返回所有非Stream的东西作为头。像这样:

  [MessageContract] 
public class MyMessage {
[MessageHeader]
public MyDataContract someInfo;
[MessageBody]
public Stream myStream;
}


I don't know if this could be done, but I have a WCF service that should return a custom object, the object has a collection of another custom object that contains a stream.

when I try to return this object I get

System.Runtime.Serialization.InvalidDataContractException: Type 'System.ServiceModel.Dispatcher.StreamFormatter+MessageBodyStream' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types.

If I change to method to just return one of the stream with Stream as return type it works fine. It would be too much code for me to post, so I was just wondering in general if it's possible, and if there is somethings special I have to do to get custom object with streams to return without errors from WCF service?

I Use wsHttpBindig now while testing.

I have marked the streams and the IList as DataMembers in the classes, should I mark them something else?

Thanks for any help, if it's not understandable I can try to create a smal example code

解决方案

Do you actually want streaming to happen, or do you just want it serialized (and are ok with it being buffered)?

If you're ok with it being buffered:

Keep in mind that the DataContractSerializer does not have built-in support for Streams, but it does for byte arrays. So, do the usual DataContract type conversion trick: Don't mark the stream with DataMember, but create a private [DataMember] property of type byte[] that wraps the Stream. Something like:

public Stream myStream;

[DataMember(Name="myStream")]
private byte[] myStreamWrapper {
   get { /* convert myStream to byte[] here */ }
   set { /* convert byte[] to myStream here */ }
}

If you actually want it streamed:

The WCF ServiceModel can only support a streamed message body if the Stream is the entire body. So, your operation should return a MessageContract that returns all the non-Stream things as headers. Like so:

[MessageContract]
public class MyMessage {
   [MessageHeader]
   public MyDataContract someInfo;
   [MessageBody]
   public Stream myStream;
}

这篇关于WCF返回一个包含流的自定义对象集合的自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆