In .NET WCF I want to use TransferMode.Streamed. Thus I need to have a Message object to transfer more than one parameter. To avoid a lot of message classes for any combination of parameters to transfer I tried it with a template class. Example for two parameters:
[MessageContract]
public class StreamMessage<TA,TB>
{
[MessageHeader(MustUnderstand = true)]
public TA Value1;
[MessageBodyMember(Order = 1)]
public TB Value2;
}
If I use the template class to transfer an FileStream object, the client receives the stream always closed. Server:
public StreamMessage<String,FileStream> DownloadFromServer()
{
Console.WriteLine("DownloadFromServer()");
const string filename = @"c:\The\File\Name";
var result = new StreamMessage<String,FileStream>();
result.Value1 = filename;
result.Value2 = File.OpenRead(filename);
return result;
}
This does not happen without usage of message template. Any suggestions?
FileSteam can not be serialized. Try using the Steam class:
public StreamMessage<String, Stream> DownloadFromServer()
{
Console.WriteLine("DownloadFromServer()");
const string filename = @"c:\The\File\Name";
var result = new StreamMessage<String, Stream>();
result.Value1 = filename;
result.Value2 = File.OpenRead(filename);
return result;
}
Based on Florians answer the following is possible, eg for two values and one stream value:
[MessageContract]
public class StreamMessage<T1,T2>
{
[MessageHeader(MustUnderstand = true)]
public T1 Val1;
[MessageHeader(MustUnderstand = true)]
public T2 Val2;
[MessageBodyMember(Order = 1)]
public Stream Stream;
}
In case of type Stream, WCF restrict this to only one object in body. Therefore I decided two have to templates: Message and StreamMessage.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.