WCF transferMode=流化,输出流是否将序列化?
由此 我准备了一个测试项目 WCF服务端: web.config,如下所示:WCF transferMode=流化,输出流是否将序列化?,wcf,stream,nettcpbinding,Wcf,Stream,Nettcpbinding,由此 我准备了一个测试项目 WCF服务端: web.config,如下所示: <bindings> <basicHttpBinding> <binding name="HttpStreaming" maxReceivedMessageSize="67108864" transferMode="Streamed"/> </basicHttpBinding> <!-- an example customBinding u
<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>
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
{
}
}
<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>
客户端代码如下所示:
<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>
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
{
}
}
<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>
客户端app.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>
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
{
}
}
<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捕获网络流量,我发现当我在clinet上使用http绑定时,流将在xml响应中转换为base64字符串。它表示流已序列化为xml。但当我在客户机上使用nettcp绑定时,我不确定数据是否被序列化。因为从WireShark我无法判断数据是否序列化。看看这篇博文:数据是序列化的-必须通过电线发送。在Wireshark中无法分辨的原因(可能)是因为NetTcpBinding将使用二进制编码。我不是100%确定,但是我100%确定如果数据成功发送,并且成功接收,那么它在发送之前是序列化的,在接收时是反序列化的。谢谢Jocke,但是那篇文章没有给出我的答案。比你Tim,它是有用的!发送一个流,实际上就是发送很多字节。是这样吗?如果正确,通过tcp发送字节和通过tcp接收字节是否需要序列化?它只发送原始字节,接收器存储字节,它们需要序列化吗?