Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4571822
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:33:33+00:00 2026-05-21T19:33:33+00:00

My first question so be gentle =) The following is all .Net 4, VB.Net,

  • 0

My first question so be gentle =)

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.

The service contract is as follows:

<ServiceContract(Namespace:="ImageSystem")> _
Public Interface IUploadService
    <OperationContract()> _
    Function UploadFile(ByVal file As ImageUpload) As ImageUpload
End Interface

The data contract ImageUpload is as follows:

<MessageContract()> _
Public Class ImageUpload

#Region " Message Header "

    Private _ImageID As Nullable(Of Long)
    <MessageHeader()> _
    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!

    <MessageBodyMember()> _
    Public Data As System.IO.Stream

#End Region

End Class

The relevant server config/bindings are as follows (these are obviously dev environment settings only):

<system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpStreamBinding" transferMode="Streamed" maxBufferSize="20971520" maxReceivedMessageSize="20971520"/>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="UploadServiceBehaviour"
        name="ImageSystem.SVC.UploadService">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpStreamBinding"
          contract="ImageSystem.SVC.IUploadService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:809/UploadService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="UploadServiceBehaviour">
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

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.

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.

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

[EDIT: reliableSessions section commented out in light of Sixto Saez’s suggestion but to no avail]

<system.serviceModel>

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

    <client>
      <endpoint address="net.tcp://mycomputername.mydomain/ImageSystem/UploadService.svc"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IUploadService"
        contract="UploadService.Local.IUploadService" name="NetTcpBinding_IUploadService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>

  </system.serviceModel>

The client usage is as follows:

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

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

I have tried two approaches to read the image on the server side.

Approach 1:

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

Approach 2:

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

In both cases, ms.Length = 0. More clearly, in the second approach, imageB64 = “” (empty string).

Why aren’t I receiving anything from the stream? Also, as a sneaky sub-question, why does the generated proxy class not provide an overload that accepts an object of type ImageUpload?

Thank you in advance!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:33:34+00:00Added an answer on May 21, 2026 at 7:33 pm

    it seemed strange to me that you would have the problems you mentioned, so I was curious and put together an implementation using your service contract. That one actually worked straight away. I don’t actually know what went wrong in your case (it’s not obvious), but let me just post a working solution here hoping that this will help you to solve your problem.

    Unfortunately, as I abandoned VB many years ago, I can only provide C# code. Hope that’s alright.

    Server Web.config (tested in IIS, with net.tcp binding):

      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding transferMode="Streamed" maxReceivedMessageSize="1000000">
              <security mode="None"/>
            </binding>
          </netTcpBinding>
        </bindings>
        <services>
          <service name="ImageSystem.SVC.UploadService">
            <endpoint address="" binding="netTcpBinding" contract="ImageSystem.SVC.IUploadService">
            </endpoint>
            <endpoint address="mex" kind="mexEndpoint" binding="mexTcpBinding"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="false"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    

    Client app.config (console test app):

    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding transferMode="Streamed" maxReceivedMessageSize="1000000">
                    <security mode="None"/>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint 
                address="net.tcp://localhost/WcfService1/UploadService.svc" 
                binding="netTcpBinding" 
                contract="ImageServices.IUploadService" 
                name="NetTcpBinding_IUploadService">
            </endpoint>
        </client>
    </system.serviceModel>
    

    Service contract & implementation:

    [ServiceContract(Namespace="urn:ImageSystem")]
    public interface IUploadService
    {
        [OperationContract]
        ImageUpload UploadFile(ImageUpload file);
    }
    
    [MessageContract]
    public class ImageUpload
    {
        [MessageHeader]
        public long? ImageID { get; set; }
        [MessageBodyMember]
        public Stream Data;
    }
    
    public class UploadService : IUploadService
    {
    
        public ImageUpload UploadFile(ImageUpload file)
        {
            long length;
    
            using (var ms = new MemoryStream())
            {
                file.Data.CopyTo(ms);
                length = ms.Length;
            }
    
            return new ImageUpload { ImageID = length, Data = new MemoryStream() };
        }
    }
    

    Test app:

    private static readonly string imgPath = @"C:\Pictures\somepicture.jpg";
    private static readonly EventWaitHandle waitHandle = new AutoResetEvent(false);
    
    static void Main()
    {
        long? result;
    
        using (var service = new ImageServices.UploadServiceClient("NetTcpBinding_IUploadService"))
        {
            var image = new ImageServices.ImageUpload();
            using (var imgStream = File.OpenRead(imgPath))
            {
                image.Data = imgStream;
                service.UploadFileCompleted += (sender, e) => 
                { 
                    result = e.Result;
                    if (e.Data != null) image.Data.Dispose();
                    waitHandle.Set();
                };
    
                service.UploadFileAsync(null, imgStream);
    
                waitHandle.WaitOne();
            }
        }
    }
    

    First of all, as you can see, the config files can be a lot simpler. Especially the large BufferSize value is not necessary. Then, with regard to the service contract, it’s not clear to me why the Upload operation would receive AND return an ImageUpload message. In my implementation, I’m returning the uploaded file size in the ImageID parameter just for demo purposes, of course. I don’t know what your reasoning behind that contract was, and what you actually would want to return.


    Actually, was just about to click “Send” when I had an idea why your code could have failed. In your test client, before you call serviceClient.UploadFileAsync(), add this line to your code: ms.Seek(0, SeekOrigin.Begin).

    This resets the position of the MemoryStream back to its beginning. If you don’t do that, the MemoryStream will be consumed only from its current position, which is its end – and which explains the Length = 0 of the stream received on the service side!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First question on Stackoverflow (.Net 2.0): So I am trying to return an XML
First question from me; I'm currently fixing a graphing service that uses XSLFO to
First question. Be gentle. I'm working on software that tracks technicians' time spent working
first question to StackOverflow, please be gentle. I am trying to find the equation
this is my first question on stack overflow, so be gentle. Let me first
This is my first question her so please be gentle: I have followig animation
First question on SOF, please be gentle if this may be a stupid question.
this is my first question.. be gentle :) I am part of the development
This is my first time asking a question, please be gentle! I have a
first question answered very quickly, thanks to all. here is the issue, for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.