从此article 我准备了一个测试项目。
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捕获网络流量,我发现当我在clinet上使用http绑定时,流将转换为xml respose中的base64字符串。它表明该流被序列化为xml。但是当我在客户端上使用net tcp绑定时,我不确定数据是否被序列化。因为从WireShark我无法弄清楚数据是否被序列化。