title: WCF Service Generated Proxy Class Exposing Function as ByRef Sub
body: <p>I have a WCF Service exposing a single contract and operation:</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 function both receives and returns an "ImageUpload" which is defined as such:</p>
  
  <pre><code>&lt;MessageContract()&gt; _
  Public Class ImageUpload
  
      &lt;MessageHeader()&gt; _
      Public Property ImageID() As Nullable(Of Long)
  
      &lt;MessageHeader()&gt; _
      Public Property ImageTypeID() As Long
  
      &lt;MessageHeader()&gt; _
      Public Property IncludeInGallery() As Boolean
  
      &lt;MessageHeader()&gt; _
      Public Property OriginalFileName() As String
  
      &lt;MessageHeader()&gt; _
      Public Property ErrorDescription() As String
  
      &lt;MessageBodyMember()&gt; _
      Public Data As System.IO.Stream
  
  End Class
  </code></pre>
  
  <p>The endpoints are defined as follows (not sure this matters too much but just in case):</p>
  
  <p>Client:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.serviceModel&gt;
  
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding name="netTcpStreamBinding" 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="524288"
            maxBufferSize="20971520" maxConnections="10" maxReceivedMessageSize="20971520"&gt;
            &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
            &lt;security mode="None" /&gt;
          &lt;/binding&gt;
      &lt;/bindings&gt;
  
      &lt;client&gt;
        &lt;endpoint address="net.tcp://localhost:809/UploadService" binding="netTcpBinding"
          bindingConfiguration="netTcpStreamBinding" 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;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding name="netTcpStreamBinding" transferMode="StreamedRequest" maxBufferSize="20971520"
                   maxReceivedMessageSize="20971520" &gt;
            &lt;security mode="None"/&gt;
          &lt;/binding&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;!-- To avoid disclosing metadata information, set the value below to false and remove the 
            metadata endpoint above before deployment --&gt;
            &lt;serviceMetadata httpGetEnabled="false"/&gt;
            &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true.  
            Set to false before deployment to avoid disclosing exception information --&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>My problem is that the proxy class generated by adding a service reference to the client, is generating a sub (void function) instead of the function I was expecting.</p>
  
  <p>What's more, the generated sub doesn't accept my message contract as in/out parameters, instead it lists the message contracts' members.</p>
  
  <p>I.e, I would expect the auto-generated proxy class to have the following signature:</p>
  
  <pre><code>Public Function UploadFile(ByVal file As ImageUpload) As ImageUpload
  </code></pre>
  
  <p>Instead, it's generating:</p>
  
  <pre><code>    Public Sub UploadFile(ByRef ErrorDescription As String, ByRef ImageID As System.Nullable(Of Long), ByRef ImageTypeID As Long, ByRef IncludeInGallery As Boolean, ByRef OriginalFileName As String, ByRef Data As System.IO.Stream)
          Dim inValue As UploadService.Local.ImageUpload = New UploadService.Local.ImageUpload()
          inValue.ErrorDescription = ErrorDescription
          inValue.ImageID = ImageID
          inValue.ImageTypeID = ImageTypeID
          inValue.IncludeInGallery = IncludeInGallery
          inValue.OriginalFileName = OriginalFileName
          inValue.Data = Data
          Dim retVal As UploadService.Local.ImageUpload = CType(Me,UploadService.Local.IUploadService).UploadFile(inValue)
          ErrorDescription = retVal.ErrorDescription
          ImageID = retVal.ImageID
          ImageTypeID = retVal.ImageTypeID
          IncludeInGallery = retVal.IncludeInGallery
          OriginalFileName = retVal.OriginalFileName
          Data = retVal.Data
      End Sub
  </code></pre>
  
  <p>This subsequently leads to stream casting issues, because the generated function allows me to pass a memory stream as an input (which works correctly when passed through to the service), but instead of passing me back a new stream for the response, it attempts to cast the MessageBodyStream received from the service into my memory stream.</p>
  
  <p>This is, in some ways similar to <a href="https://stackoverflow.com/questions/2899806/why-does-the-proxy-generated-code-create-a-new-class-when-a-messagecontract-is-in">other posts</a> but as you can see, there are no enums involved in my contracts - presence of Enums caused strange proxy class generation is marked as the answer in the similar post.</p>
  
  <p>Is there anywhere I configure the proxy behaviour to use the contracts I've specified?  Clearly I'm within a dev/test environment currently, but when this eventually goes to production it will be memory and file streams passed to the service, and the returned stream can be in any format to be honest, I intend to treat it as the abstract stream class.  The only way round this I can see right now is to change my <em>in</em> stream to be the same as the anticipated <em>out</em> stream, but surely there is a better way?</p>