title: WCF Service TcpNetBinding StreamRequest not receiving stream
body: <p>My first question so be gentle =)</p>
  
  <p>The following is all .Net 4, VB.Net, in VS2010.  The end goal is to receive a (complete) stream from the client over Tcp binding, to an IIS Hosted WCF Service.  The problem I am facing is that the service is not able to read any bytes from the provided stream.  Now with the nitty gritty... I've removed a fair amount for brevity but, let me know if I've omitted something important.</p>
  
  <p>The service contract is as follows:</p>
  
  <pre><code>&lt;ServiceContract(Namespace:="ImageSystem")&gt; _
  Public Interface IUploadService
      &lt;OperationContract()&gt; _
      Function UploadFile(ByVal file As ImageUpload) As ImageUpload
  End Interface
  </code></pre>
  
  <p>The data contract <em>ImageUpload</em> is as follows:</p>
  
  <pre><code>&lt;MessageContract()&gt; _
  Public Class ImageUpload
  
  #Region " Message Header "
  
      Private _ImageID As Nullable(Of Long)
      &lt;MessageHeader()&gt; _
      Public Property ImageID() As Nullable(Of Long)
          Get
              Return _ImageID
          End Get
          Set(ByVal value As Nullable(Of Long))
              _ImageID = value
          End Set
      End Property
  
      '... a few other value type properties
  
  #End Region
  
  #Region " Message Body"
      ' Do not add any more members to the message body or streaming support will be disabled!
  
      &lt;MessageBodyMember()&gt; _
      Public Data As System.IO.Stream
  
  #End Region
  
  End Class
  </code></pre>
  
  <p>The relevant server config/bindings are as follows (these are obviously dev environment settings only):</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding name="netTcpStreamBinding" transferMode="Streamed" maxBufferSize="20971520" maxReceivedMessageSize="20971520"/&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="UploadServiceBehaviour"
          name="ImageSystem.SVC.UploadService"&gt;
          &lt;endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpStreamBinding"
            contract="ImageSystem.SVC.IUploadService"&gt;
            &lt;identity&gt;
              &lt;dns value="localhost" /&gt;
            &lt;/identity&gt;
          &lt;/endpoint&gt;
          &lt;endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="net.tcp://localhost:809/UploadService" /&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="UploadServiceBehaviour"&gt;
            &lt;serviceMetadata httpGetEnabled="false"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>The WCF Service is a service Library, which is hosted by a Web Application.  The Web Application project runs in my local IIS 7.5.  IIS has been configured to enable TCP connections, and the application pool identity is configured with relevant permissions to the contract implementation.  VS2010 is run as admin to enable debugging in IIS.</p>
  
  <p>To test the contract implementation I have a Windows Console Application set up as a (test) client.  The client proxy classes were generated by adding a service reference to the service within the IIS host (http://localhost/ImageSystem/UploadService.svc).  The service reference is configured to generate async methods.</p>
  
  <p>The relevant auto-generated client config is as follows (note, I've tried increasing maxBufferPoolSize, maxBufferSize, and maxReceivedMessageSize to match the servers config of "20971520", but to no avail):</p>
  
  <p><em>[EDIT: reliableSessions section commented out in light of Sixto Saez's suggestion but to no avail]</em></p>
  
  <pre><code>&lt;system.serviceModel&gt;
  
      &lt;bindings&gt;
          &lt;binding name="NetTcpBinding_IUploadService" 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" maxBufferPoolSize="20971520"
            maxBufferSize="20971520" maxConnections="10" maxReceivedMessageSize="20971520"&gt;
            &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
            &lt;!--&lt;reliableSession ordered="true" inactivityTimeout="00:10:00"
              enabled="false" /&gt;--&gt;
            &lt;security mode="None"&gt;
              &lt;transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /&gt;
              &lt;message clientCredentialType="Windows" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
  
      &lt;client&gt;
        &lt;endpoint address="net.tcp://mycomputername.mydomain/ImageSystem/UploadService.svc"
          binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IUploadService"
          contract="UploadService.Local.IUploadService" name="NetTcpBinding_IUploadService"&gt;
          &lt;identity&gt;
            &lt;dns value="localhost" /&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
      &lt;/client&gt;
  
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>The client usage is as follows:</p>
  
  <pre><code>Public Sub Test()
      Dim serviceClient As UploadService.Local.UploadServiceClient = New UploadService.Local.UploadServiceClient
      AddHandler serviceClient.UploadFileCompleted, AddressOf LocalTestCallback
      Dim ms As MemoryStream = New MemoryStream
      My.Resources.Penguins.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
      serviceClient.ClientCredentials.Windows.ClientCredential.Domain = "MYDOMAIN"
      serviceClient.ClientCredentials.Windows.ClientCredential.UserName = "User"
      serviceClient.ClientCredentials.Windows.ClientCredential.Domain = "Password123"
      serviceClient.UploadFileAsync(Nothing, ..., ms, ms) '"..." is obviously not actually here, other values omitted. "ms" is passed as UserState object in addition to fulfilling the 'Data' parameter
  End Sub
  </code></pre>
  
  <p>In case you wonder (or it matters), the penguins image is the one provided with Windows 7 in the sample pictures directory. The image is 777,835 bytes (should be within the relevant request/buffer max sizes).</p>
  
  <p>I have tried two approaches to read the image on the server side.</p>
  
  <p>Approach 1:</p>
  
  <pre><code>Public Function UploadFile(ByVal file As ImageUpload) As ImageUpload Implements IUploadService.UploadFile
  
      Dim uploadBuffer(Helper.Settings.AppSettings(Of Integer)("UploadBufferSize", True) - 1) As Byte
      Dim ms As MemoryStream = New MemoryStream()
      Dim bytesRead As Integer
      Do
          bytesRead = file.Data.Read(uploadBuffer, 0, uploadBuffer.Length)
          ms.Write(uploadBuffer, 0, bytesRead)
      Loop Until bytesRead = 0
  
  End Function
  </code></pre>
  
  <p>Approach 2:</p>
  
  <pre><code>Public Function UploadFile(ByVal file As ImageUpload) As ImageUpload Implements IUploadService.UploadFile
  
      Dim reader As StreamReader = New StreamReader(file.Data)
      Dim imageB64 As String = reader.ReadToEnd
      ms = New MemoryStream(Convert.FromBase64String(imageB64))
  
  End Function
  </code></pre>
  
  <p>In both cases, ms.Length = 0.  More clearly, in the second approach, imageB64 = "" (empty string).</p>
  
  <p>Why aren't I receiving anything from the stream?  Also, as a sneaky <em>sub-question</em>, why does the generated proxy class not provide an overload that accepts an object of type <em>ImageUpload</em>?</p>
  
  <p>Thank you in advance!!</p>