0

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

4

3 に答える 3

4

実際にストリーミングを実行したいですか、それともシリアライズしたいだけですか (そして、バッファリングしても問題ありません)?

バッファリングされても問題ない場合:

DataContractSerializer には Streams のサポートが組み込まれていませんが、バイト配列にはサポートされていることに注意してください。したがって、通常の DataContract 型変換のトリックを実行します。ストリームを DataMember でマークしないで、ストリームをラップする byte[] 型のプライベート [DataMember] プロパティを作成します。何かのようなもの:

public Stream myStream;

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

実際にストリーミングしたい場合:

Stream が本文全体である場合、WCF ServiceModel はストリーミングされたメッセージ本文のみをサポートできます。したがって、操作は、Stream 以外のすべてのものをヘッダーとして返す MessageContract を返す必要があります。そのようです:

[MessageContract]
public class MyMessage {
   [MessageHeader]
   public MyDataContract someInfo;
   [MessageBody]
   public Stream myStream;
}
于 2009-10-09T07:45:25.613 に答える
2

つまり、バッファリングされた転送 (DataContracts としてマークされた int、string、またはカスタムの複雑な型を送り返す) とストリーミングを混在させることはできません。

ここで十分に文書化されています: MSDN on WCF Streaming

それは言います:

ストリーミング転送の制限

ストリーミング転送モードを使用すると、ランタイムによって追加の制限が適用されます。

ストリーミング トランスポート全体で発生する操作は、最大 1 つの入力または出力パラメーターとのコントラクトを持つことができます。そのパラメーターは、メッセージの本文全体に対応し、Message、Stream の派生型、または IXmlSerializable 実装である必要があります。操作の戻り値を持つことは、出力パラメーターを持つことと同じです。

したがって、ソリューションを再構築して、基本情報を複合型で返すメソッドと、ストリーミングを処理する 2 番目の操作の 2 つのメソッドを持たせる必要があると思います。

マルク

于 2009-10-09T08:21:43.457 に答える
0

これは重複した質問のようです -ソリューションのストリームを含むカスタム オブジェクトのコレクションを含むカスタム オブジェクトを返す WCFに対する私の回答を参照してください。

ところで

[OperationContract] で Stream を直接使用する場合は、特殊なケースです。DataContractSerializer は呼び出されません。WCF ServiceModel は、ストリームを使用してメッセージ本文を書き出す特別な方法を使用します (基になるバインディングがサポートしている場合は、実際にストリーミングされるようにします)。

ただし、Stream を [DataContract] 内の別の [DataMember] として使用すると、それは DataContractSerializer の別の型であり、サポートされていません。したがって、型変換のトリックを使用する必要があります (以前のリンクを参照してください)。

直感的ではありませんが、私は知っています:)しかし、ある程度ここここに文書化されています。

于 2009-10-09T07:53:21.930 に答える