0

From this article I prepare a test project.

WCF service side:

web.config like this:

<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 code is like this:

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;
            }
        }

Client code is like this:

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
            {

            }
        }

the client app.config is like this:

<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>

I use the basic http binding and net tcp binding to do testing. And use WireShark to capture the network traffic, I found that when I use the http binding on clinet, the stream will be convert to a base64 string in the xml respose. It indicate that the stream was serialized to xml. But when I use net tcp binding on client, I am not sure the Data was serialized or not. because from the WireShark I can't figure out the data is serialized or not.

gfan
  • 1,027
  • 1
  • 14
  • 28
  • 1
    have a look at this blog post: http://www.codeguru.com/csharp/.net/net_wcf/article.php/c18583/Passing-Large-Files-in-Windows-Communication-Foundation-WCF-using-Streaming-and-TCP.htm – Jocke Jan 27 '14 at 08:13
  • 2
    The data was serialized - it *has* to be to be sent over the wire. The reason (probably) you can't tell in Wireshark is because NetTcpBinding will use binary encoding. I'm not 100% sure on that, but I am 100% sure that if the data was sent successfully, and received successfully, that it was serialized before sending and deserialized when received. – Tim Jan 27 '14 at 08:33
  • Thank you Jocke, but that article doesn't give my answer. – gfan Jan 29 '14 at 02:03
  • Than you Tim, It's useful! Send a stream, is in fact send many bytes. is it right? If it's right, does sending bytes via tcp and receiving bytes via tcp need serialization? It just sends the raw bytes, the receiver store the bytes, Do they need serialization? – gfan Jan 29 '14 at 03:22

0 Answers0