WCF TransferMode=Streamed, выходной поток будет сериализован?
Из этой статьи я готовлю тестовый проект.
Сторона обслуживания WCF:
web.config вот так:
<bindings>
<basicHttpBinding>
<binding name="HttpStreaming" maxReceivedMessageSize="67108864"
transferMode="Streamed"/>
</basicHttpBinding>
<!-- an example customBinding using Http and streaming-->
<netTcpBinding>
<binding name="netTcpBindConfig" closeTimeout="00:30:00" portSharingEnabled="true"
openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" maxConnections="50"
hostNameComparisonMode="StrongWildcard" listenBacklog="100">
<readerQuotas maxDepth="2147483647"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:01:00" enabled="false" />
<security mode="None">
<transport clientCredentialType="None" protectionLevel="None" />
<message clientCredentialType="None" />
</security>
</binding>
</netTcpBinding>
</bindings>
код service.cs выглядит так:
public Stream GetStream(string data)
{
//this file path assumes the image is in
// the Service folder and the service is executing
// in service/bin
string filePath = Path.Combine(
System.Environment.CurrentDirectory,
"D:\\Practice\\WcfStream\\WcfStream\\image.jpg");
//open the file, this could throw an exception
//(e.g. if the file is not found)
//having includeExceptionDetailInFaults="True" in config
// would cause this exception to be returned to the client
try
{
FileStream imageFile = File.OpenRead(filePath);
return imageFile;
}
catch (IOException ex)
{
Console.WriteLine(
String.Format("An exception was thrown while trying to open file {0}", filePath));
Console.WriteLine("Exception is: ");
Console.WriteLine(ex.ToString());
throw ex;
}
}
Код клиента выглядит так:
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
ServiceReference1.StreamingSampleClient c = new ServiceReference1.StreamingSampleClient("NetTcpBinding_IStreamingSample");// I use the basic http binding and net tcp binding to do testing.
Stream s = c.GetStream("aa");
Img.Source = BitmapFrame.Create(s,
BitmapCreateOptions.None,
BitmapCacheOption.OnLoad);
c.Close();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message, "Error");
}
finally
{
}
}
клиентский app.config выглядит так:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IStreamingSample" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_IStreamingSample" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Streamed" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxReceivedMessageSize="2147483647"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
>
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
Я использую базовую http-привязку и net tcp-привязку для тестирования. И, используя WireShark для захвата сетевого трафика, я обнаружил, что при использовании http-привязки на clinet поток будет преобразован в строку base64 в xml respose. Это указывает на то, что поток был сериализован в XML. Но когда я использую привязку net tcp на клиенте, я не уверен, что данные были сериализованы или нет. потому что из WireShark я не могу понять, сериализованы ли данные или нет.