WCF Stream.Read always returns 0 in client












1















I've spent most of my day trying to figure out why this isn't working. I have a WCF service that streams an object to the client. The client is then supposed to write the file to its disk. But when I call stream.Read(buffer, 0, bufferLength) it always returns 0. Here's my code:



namespace StreamServiceNS
{
[ServiceContract]
public interface IStreamService
{
[OperationContract]
Stream downloadStreamFile();
}
}
class StreamService : IStreamService
{
public Stream downloadStreamFile()
{
ISSSteamFile sFile = getStreamFile();
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
bf.Serialize(stream, sFile);
return stream;
}
}


Service config file:



<system.serviceModel>
<services>
<service name="StreamServiceNS.StreamService">
<endpoint address="stream" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IStreamService"
name="BasicHttpEndpoint_IStreamService" contract="SWUpdaterService.ISWUService" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IStreamService" transferMode="StreamedResponse"
maxReceivedMessageSize="209715200"></binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceThrottling maxConcurrentCalls ="100" maxConcurrentSessions="400"/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>


Client:



TestApp.StreamServiceRef.StreamServiceClient client = new StreamServiceRef.StreamServiceClient();
try
{
Stream stream = client.downloadStreamFile();
int bufferLength = 8 * 1024;
byte buffer = new byte[bufferLength];

FileStream fs = new FileStream(@"C:testtestFile.exe", FileMode.Create, FileAccess.Write);
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, bufferLength)) > 0)
{
fs.Write(buffer, 0, bytesRead);
}
stream.Close();
fs.Close();
}
catch (Exception e) { Console.WriteLine("Error: " + e.Message); }


Client app.config:



    <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpEndpoint_IStreamService" maxReceivedMessageSize="209715200" transferMode="StreamedResponse">
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://[server]/StreamServices/streamservice.svc/stream"
binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpoint_IStreamService"
contract="StreamServiceRef.IStreamService" name="BasicHttpEndpoint_IStreamService" />
</client>
</system.serviceModel>


(some code clipped for brevity)



I've read everything I can find on making WCF streaming services, and my code looks no different than theirs. I can replace the streaming with buffering and send an object that way fine, but when I try to stream, the client always sees the stream as "empty". The testFile.exe gets created, but its size is 0KB.



What am I missing?






UPDATE

Hrm. Well, I can't debug it because when I try to it tells me that the WCF Test Client does not support Streams. But, I know something is making it across in the stream because if I don't put the while loop, and just have one fs.Write(...), it will write 8KB once the stream closes. So something is getting across.



I looked at your post and added:



[MessageContract]
public class FileDownloadMessage
{
[MessageBodyMember(Order = 1)]
public MemoryStream FileByteStream;
}


to my IStreamService. And downloadStreamFile() now returns a FileDownloadMessage object.



But after updating the service reference on my client, it thinks downloadStreamFile() returns a TestApp.StreamServiceRef.MemoryStream object.

It also created a downloadStreamFileRequest() class, which I did not make and have no idea where it's coming from.










share|improve this question





























    1















    I've spent most of my day trying to figure out why this isn't working. I have a WCF service that streams an object to the client. The client is then supposed to write the file to its disk. But when I call stream.Read(buffer, 0, bufferLength) it always returns 0. Here's my code:



    namespace StreamServiceNS
    {
    [ServiceContract]
    public interface IStreamService
    {
    [OperationContract]
    Stream downloadStreamFile();
    }
    }
    class StreamService : IStreamService
    {
    public Stream downloadStreamFile()
    {
    ISSSteamFile sFile = getStreamFile();
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream stream = new MemoryStream();
    bf.Serialize(stream, sFile);
    return stream;
    }
    }


    Service config file:



    <system.serviceModel>
    <services>
    <service name="StreamServiceNS.StreamService">
    <endpoint address="stream" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IStreamService"
    name="BasicHttpEndpoint_IStreamService" contract="SWUpdaterService.ISWUService" />
    </service>
    </services>
    <bindings>
    <basicHttpBinding>
    <binding name="BasicHttpBinding_IStreamService" transferMode="StreamedResponse"
    maxReceivedMessageSize="209715200"></binding>
    </basicHttpBinding>
    </bindings>
    <behaviors>
    <serviceBehaviors>
    <behavior>
    <serviceThrottling maxConcurrentCalls ="100" maxConcurrentSessions="400"/>
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
    </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>


    Client:



    TestApp.StreamServiceRef.StreamServiceClient client = new StreamServiceRef.StreamServiceClient();
    try
    {
    Stream stream = client.downloadStreamFile();
    int bufferLength = 8 * 1024;
    byte buffer = new byte[bufferLength];

    FileStream fs = new FileStream(@"C:testtestFile.exe", FileMode.Create, FileAccess.Write);
    int bytesRead;
    while ((bytesRead = stream.Read(buffer, 0, bufferLength)) > 0)
    {
    fs.Write(buffer, 0, bytesRead);
    }
    stream.Close();
    fs.Close();
    }
    catch (Exception e) { Console.WriteLine("Error: " + e.Message); }


    Client app.config:



        <system.serviceModel>
    <bindings>
    <basicHttpBinding>
    <binding name="BasicHttpEndpoint_IStreamService" maxReceivedMessageSize="209715200" transferMode="StreamedResponse">
    </binding>
    </basicHttpBinding>
    </bindings>
    <client>
    <endpoint address="http://[server]/StreamServices/streamservice.svc/stream"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpoint_IStreamService"
    contract="StreamServiceRef.IStreamService" name="BasicHttpEndpoint_IStreamService" />
    </client>
    </system.serviceModel>


    (some code clipped for brevity)



    I've read everything I can find on making WCF streaming services, and my code looks no different than theirs. I can replace the streaming with buffering and send an object that way fine, but when I try to stream, the client always sees the stream as "empty". The testFile.exe gets created, but its size is 0KB.



    What am I missing?






    UPDATE

    Hrm. Well, I can't debug it because when I try to it tells me that the WCF Test Client does not support Streams. But, I know something is making it across in the stream because if I don't put the while loop, and just have one fs.Write(...), it will write 8KB once the stream closes. So something is getting across.



    I looked at your post and added:



    [MessageContract]
    public class FileDownloadMessage
    {
    [MessageBodyMember(Order = 1)]
    public MemoryStream FileByteStream;
    }


    to my IStreamService. And downloadStreamFile() now returns a FileDownloadMessage object.



    But after updating the service reference on my client, it thinks downloadStreamFile() returns a TestApp.StreamServiceRef.MemoryStream object.

    It also created a downloadStreamFileRequest() class, which I did not make and have no idea where it's coming from.










    share|improve this question



























      1












      1








      1








      I've spent most of my day trying to figure out why this isn't working. I have a WCF service that streams an object to the client. The client is then supposed to write the file to its disk. But when I call stream.Read(buffer, 0, bufferLength) it always returns 0. Here's my code:



      namespace StreamServiceNS
      {
      [ServiceContract]
      public interface IStreamService
      {
      [OperationContract]
      Stream downloadStreamFile();
      }
      }
      class StreamService : IStreamService
      {
      public Stream downloadStreamFile()
      {
      ISSSteamFile sFile = getStreamFile();
      BinaryFormatter bf = new BinaryFormatter();
      MemoryStream stream = new MemoryStream();
      bf.Serialize(stream, sFile);
      return stream;
      }
      }


      Service config file:



      <system.serviceModel>
      <services>
      <service name="StreamServiceNS.StreamService">
      <endpoint address="stream" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IStreamService"
      name="BasicHttpEndpoint_IStreamService" contract="SWUpdaterService.ISWUService" />
      </service>
      </services>
      <bindings>
      <basicHttpBinding>
      <binding name="BasicHttpBinding_IStreamService" transferMode="StreamedResponse"
      maxReceivedMessageSize="209715200"></binding>
      </basicHttpBinding>
      </bindings>
      <behaviors>
      <serviceBehaviors>
      <behavior>
      <serviceThrottling maxConcurrentCalls ="100" maxConcurrentSessions="400"/>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
      </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>


      Client:



      TestApp.StreamServiceRef.StreamServiceClient client = new StreamServiceRef.StreamServiceClient();
      try
      {
      Stream stream = client.downloadStreamFile();
      int bufferLength = 8 * 1024;
      byte buffer = new byte[bufferLength];

      FileStream fs = new FileStream(@"C:testtestFile.exe", FileMode.Create, FileAccess.Write);
      int bytesRead;
      while ((bytesRead = stream.Read(buffer, 0, bufferLength)) > 0)
      {
      fs.Write(buffer, 0, bytesRead);
      }
      stream.Close();
      fs.Close();
      }
      catch (Exception e) { Console.WriteLine("Error: " + e.Message); }


      Client app.config:



          <system.serviceModel>
      <bindings>
      <basicHttpBinding>
      <binding name="BasicHttpEndpoint_IStreamService" maxReceivedMessageSize="209715200" transferMode="StreamedResponse">
      </binding>
      </basicHttpBinding>
      </bindings>
      <client>
      <endpoint address="http://[server]/StreamServices/streamservice.svc/stream"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpoint_IStreamService"
      contract="StreamServiceRef.IStreamService" name="BasicHttpEndpoint_IStreamService" />
      </client>
      </system.serviceModel>


      (some code clipped for brevity)



      I've read everything I can find on making WCF streaming services, and my code looks no different than theirs. I can replace the streaming with buffering and send an object that way fine, but when I try to stream, the client always sees the stream as "empty". The testFile.exe gets created, but its size is 0KB.



      What am I missing?






      UPDATE

      Hrm. Well, I can't debug it because when I try to it tells me that the WCF Test Client does not support Streams. But, I know something is making it across in the stream because if I don't put the while loop, and just have one fs.Write(...), it will write 8KB once the stream closes. So something is getting across.



      I looked at your post and added:



      [MessageContract]
      public class FileDownloadMessage
      {
      [MessageBodyMember(Order = 1)]
      public MemoryStream FileByteStream;
      }


      to my IStreamService. And downloadStreamFile() now returns a FileDownloadMessage object.



      But after updating the service reference on my client, it thinks downloadStreamFile() returns a TestApp.StreamServiceRef.MemoryStream object.

      It also created a downloadStreamFileRequest() class, which I did not make and have no idea where it's coming from.










      share|improve this question
















      I've spent most of my day trying to figure out why this isn't working. I have a WCF service that streams an object to the client. The client is then supposed to write the file to its disk. But when I call stream.Read(buffer, 0, bufferLength) it always returns 0. Here's my code:



      namespace StreamServiceNS
      {
      [ServiceContract]
      public interface IStreamService
      {
      [OperationContract]
      Stream downloadStreamFile();
      }
      }
      class StreamService : IStreamService
      {
      public Stream downloadStreamFile()
      {
      ISSSteamFile sFile = getStreamFile();
      BinaryFormatter bf = new BinaryFormatter();
      MemoryStream stream = new MemoryStream();
      bf.Serialize(stream, sFile);
      return stream;
      }
      }


      Service config file:



      <system.serviceModel>
      <services>
      <service name="StreamServiceNS.StreamService">
      <endpoint address="stream" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IStreamService"
      name="BasicHttpEndpoint_IStreamService" contract="SWUpdaterService.ISWUService" />
      </service>
      </services>
      <bindings>
      <basicHttpBinding>
      <binding name="BasicHttpBinding_IStreamService" transferMode="StreamedResponse"
      maxReceivedMessageSize="209715200"></binding>
      </basicHttpBinding>
      </bindings>
      <behaviors>
      <serviceBehaviors>
      <behavior>
      <serviceThrottling maxConcurrentCalls ="100" maxConcurrentSessions="400"/>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
      </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>


      Client:



      TestApp.StreamServiceRef.StreamServiceClient client = new StreamServiceRef.StreamServiceClient();
      try
      {
      Stream stream = client.downloadStreamFile();
      int bufferLength = 8 * 1024;
      byte buffer = new byte[bufferLength];

      FileStream fs = new FileStream(@"C:testtestFile.exe", FileMode.Create, FileAccess.Write);
      int bytesRead;
      while ((bytesRead = stream.Read(buffer, 0, bufferLength)) > 0)
      {
      fs.Write(buffer, 0, bytesRead);
      }
      stream.Close();
      fs.Close();
      }
      catch (Exception e) { Console.WriteLine("Error: " + e.Message); }


      Client app.config:



          <system.serviceModel>
      <bindings>
      <basicHttpBinding>
      <binding name="BasicHttpEndpoint_IStreamService" maxReceivedMessageSize="209715200" transferMode="StreamedResponse">
      </binding>
      </basicHttpBinding>
      </bindings>
      <client>
      <endpoint address="http://[server]/StreamServices/streamservice.svc/stream"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpoint_IStreamService"
      contract="StreamServiceRef.IStreamService" name="BasicHttpEndpoint_IStreamService" />
      </client>
      </system.serviceModel>


      (some code clipped for brevity)



      I've read everything I can find on making WCF streaming services, and my code looks no different than theirs. I can replace the streaming with buffering and send an object that way fine, but when I try to stream, the client always sees the stream as "empty". The testFile.exe gets created, but its size is 0KB.



      What am I missing?






      UPDATE

      Hrm. Well, I can't debug it because when I try to it tells me that the WCF Test Client does not support Streams. But, I know something is making it across in the stream because if I don't put the while loop, and just have one fs.Write(...), it will write 8KB once the stream closes. So something is getting across.



      I looked at your post and added:



      [MessageContract]
      public class FileDownloadMessage
      {
      [MessageBodyMember(Order = 1)]
      public MemoryStream FileByteStream;
      }


      to my IStreamService. And downloadStreamFile() now returns a FileDownloadMessage object.



      But after updating the service reference on my client, it thinks downloadStreamFile() returns a TestApp.StreamServiceRef.MemoryStream object.

      It also created a downloadStreamFileRequest() class, which I did not make and have no idea where it's coming from.







      c# wcf streaming wcf-streaming






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 5 '11 at 23:29







      Marcus

















      asked Jan 5 '11 at 21:48









      MarcusMarcus

      3,64732649




      3,64732649
























          4 Answers
          4






          active

          oldest

          votes


















          2














          First of all, are you able to debug the service and see if the stream contains any data on that side before it gets sent to the client?



          I am returning a stream from my WCF service as well. I had a problem with it, too, though not quite the same issue. Hopefully the solution that I found will help. I found the solution here. This solution is dealing with uploading a stream, but the implementation in my download service worked fine.

          My SO question/solution is here (My own answer is the one you should probably look at, not the accepted answer as that is specific to AJAX)



          EDIT: The basic differences is that I wrapped my Stream in a MessageContract where I specified the Stream as the only member of the message and the basicHttp binding 'transferMode' attribute is 'Streamed'. I'm new to WCF, so I can't be sure this will help, but I hope it does!



          EDIT 2:
          I wouldn't use a service reference. You can run (from cmd prompt) 'svcutil.exe [mex url]' to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. Also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.



          The DownloadStreamFileRequest class is just a request object that the client passes to the base.Channel when making the call to the service. In my code below, it's called GetExportedFileRequest



          In my generated .cs file, I have the following method that returns System.IO.MemoryStreamand it gets this stream from the FileDownloadMessage object:



          public System.IO.MemoryStream GetExportedFile()
          {
          GetExportedFileRequest inValue = new GetExportedFileRequest();
          FileDownloadMessage retVal = ((IDailyBillingParser)(this)).GetExportedFile( inValue );
          return retVal.FileByteStream;
          }





          share|improve this answer


























          • Check my Update above.

            – Marcus
            Jan 5 '11 at 23:30











          • I wouldnt use a service reference. You can run (from cmd prompt) "svcutil.exe [mex url]" to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. EDIT: also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.

            – norepro
            Jan 6 '11 at 15:16













          • Thanks. I used svcutil to make the "reference" and did all the config stuff in code. I also ended up sending the stream as a filestream, not a memorystream, and for some reason that seemed to help.

            – Marcus
            Jan 14 '11 at 15:15



















          4














          I also had this problem and what worked for me was setting the position of the stream to 0 before sending it back to the client.






          share|improve this answer



















          • 1





            I know i am not supposed to say thank you in the comments but SERIOUSLY, THANK YOU SOOOO MUCH. you are a legend

            – Jobin Jose
            Apr 7 '16 at 11:18











          • its always the daft stuff that trips you up

            – MikeT
            Apr 18 '16 at 12:37



















          0














          Is their any reason you want to use "BinaryFormatter"? If no, you could return stream from wcf like this



          return new FileStream(@"D:yourFileName.txt", FileMode.Open, FileAccess.Read)






          share|improve this answer
























          • I used BinaryFormatter because the object I am putting in the stream is in memory; it hasn't been written to disk.

            – Marcus
            Jan 6 '11 at 14:39



















          0














          In case anyone is searching here I my error was returning a FileStream instead of a Stream from my WCF service - this always returned an empty stream. Changing the return type to a Stream solved my issue.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4609557%2fwcf-stream-read-always-returns-0-in-client%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            First of all, are you able to debug the service and see if the stream contains any data on that side before it gets sent to the client?



            I am returning a stream from my WCF service as well. I had a problem with it, too, though not quite the same issue. Hopefully the solution that I found will help. I found the solution here. This solution is dealing with uploading a stream, but the implementation in my download service worked fine.

            My SO question/solution is here (My own answer is the one you should probably look at, not the accepted answer as that is specific to AJAX)



            EDIT: The basic differences is that I wrapped my Stream in a MessageContract where I specified the Stream as the only member of the message and the basicHttp binding 'transferMode' attribute is 'Streamed'. I'm new to WCF, so I can't be sure this will help, but I hope it does!



            EDIT 2:
            I wouldn't use a service reference. You can run (from cmd prompt) 'svcutil.exe [mex url]' to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. Also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.



            The DownloadStreamFileRequest class is just a request object that the client passes to the base.Channel when making the call to the service. In my code below, it's called GetExportedFileRequest



            In my generated .cs file, I have the following method that returns System.IO.MemoryStreamand it gets this stream from the FileDownloadMessage object:



            public System.IO.MemoryStream GetExportedFile()
            {
            GetExportedFileRequest inValue = new GetExportedFileRequest();
            FileDownloadMessage retVal = ((IDailyBillingParser)(this)).GetExportedFile( inValue );
            return retVal.FileByteStream;
            }





            share|improve this answer


























            • Check my Update above.

              – Marcus
              Jan 5 '11 at 23:30











            • I wouldnt use a service reference. You can run (from cmd prompt) "svcutil.exe [mex url]" to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. EDIT: also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.

              – norepro
              Jan 6 '11 at 15:16













            • Thanks. I used svcutil to make the "reference" and did all the config stuff in code. I also ended up sending the stream as a filestream, not a memorystream, and for some reason that seemed to help.

              – Marcus
              Jan 14 '11 at 15:15
















            2














            First of all, are you able to debug the service and see if the stream contains any data on that side before it gets sent to the client?



            I am returning a stream from my WCF service as well. I had a problem with it, too, though not quite the same issue. Hopefully the solution that I found will help. I found the solution here. This solution is dealing with uploading a stream, but the implementation in my download service worked fine.

            My SO question/solution is here (My own answer is the one you should probably look at, not the accepted answer as that is specific to AJAX)



            EDIT: The basic differences is that I wrapped my Stream in a MessageContract where I specified the Stream as the only member of the message and the basicHttp binding 'transferMode' attribute is 'Streamed'. I'm new to WCF, so I can't be sure this will help, but I hope it does!



            EDIT 2:
            I wouldn't use a service reference. You can run (from cmd prompt) 'svcutil.exe [mex url]' to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. Also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.



            The DownloadStreamFileRequest class is just a request object that the client passes to the base.Channel when making the call to the service. In my code below, it's called GetExportedFileRequest



            In my generated .cs file, I have the following method that returns System.IO.MemoryStreamand it gets this stream from the FileDownloadMessage object:



            public System.IO.MemoryStream GetExportedFile()
            {
            GetExportedFileRequest inValue = new GetExportedFileRequest();
            FileDownloadMessage retVal = ((IDailyBillingParser)(this)).GetExportedFile( inValue );
            return retVal.FileByteStream;
            }





            share|improve this answer


























            • Check my Update above.

              – Marcus
              Jan 5 '11 at 23:30











            • I wouldnt use a service reference. You can run (from cmd prompt) "svcutil.exe [mex url]" to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. EDIT: also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.

              – norepro
              Jan 6 '11 at 15:16













            • Thanks. I used svcutil to make the "reference" and did all the config stuff in code. I also ended up sending the stream as a filestream, not a memorystream, and for some reason that seemed to help.

              – Marcus
              Jan 14 '11 at 15:15














            2












            2








            2







            First of all, are you able to debug the service and see if the stream contains any data on that side before it gets sent to the client?



            I am returning a stream from my WCF service as well. I had a problem with it, too, though not quite the same issue. Hopefully the solution that I found will help. I found the solution here. This solution is dealing with uploading a stream, but the implementation in my download service worked fine.

            My SO question/solution is here (My own answer is the one you should probably look at, not the accepted answer as that is specific to AJAX)



            EDIT: The basic differences is that I wrapped my Stream in a MessageContract where I specified the Stream as the only member of the message and the basicHttp binding 'transferMode' attribute is 'Streamed'. I'm new to WCF, so I can't be sure this will help, but I hope it does!



            EDIT 2:
            I wouldn't use a service reference. You can run (from cmd prompt) 'svcutil.exe [mex url]' to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. Also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.



            The DownloadStreamFileRequest class is just a request object that the client passes to the base.Channel when making the call to the service. In my code below, it's called GetExportedFileRequest



            In my generated .cs file, I have the following method that returns System.IO.MemoryStreamand it gets this stream from the FileDownloadMessage object:



            public System.IO.MemoryStream GetExportedFile()
            {
            GetExportedFileRequest inValue = new GetExportedFileRequest();
            FileDownloadMessage retVal = ((IDailyBillingParser)(this)).GetExportedFile( inValue );
            return retVal.FileByteStream;
            }





            share|improve this answer















            First of all, are you able to debug the service and see if the stream contains any data on that side before it gets sent to the client?



            I am returning a stream from my WCF service as well. I had a problem with it, too, though not quite the same issue. Hopefully the solution that I found will help. I found the solution here. This solution is dealing with uploading a stream, but the implementation in my download service worked fine.

            My SO question/solution is here (My own answer is the one you should probably look at, not the accepted answer as that is specific to AJAX)



            EDIT: The basic differences is that I wrapped my Stream in a MessageContract where I specified the Stream as the only member of the message and the basicHttp binding 'transferMode' attribute is 'Streamed'. I'm new to WCF, so I can't be sure this will help, but I hope it does!



            EDIT 2:
            I wouldn't use a service reference. You can run (from cmd prompt) 'svcutil.exe [mex url]' to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. Also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.



            The DownloadStreamFileRequest class is just a request object that the client passes to the base.Channel when making the call to the service. In my code below, it's called GetExportedFileRequest



            In my generated .cs file, I have the following method that returns System.IO.MemoryStreamand it gets this stream from the FileDownloadMessage object:



            public System.IO.MemoryStream GetExportedFile()
            {
            GetExportedFileRequest inValue = new GetExportedFileRequest();
            FileDownloadMessage retVal = ((IDailyBillingParser)(this)).GetExportedFile( inValue );
            return retVal.FileByteStream;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:17









            Community

            11




            11










            answered Jan 5 '11 at 22:18









            norepronorepro

            5571717




            5571717













            • Check my Update above.

              – Marcus
              Jan 5 '11 at 23:30











            • I wouldnt use a service reference. You can run (from cmd prompt) "svcutil.exe [mex url]" to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. EDIT: also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.

              – norepro
              Jan 6 '11 at 15:16













            • Thanks. I used svcutil to make the "reference" and did all the config stuff in code. I also ended up sending the stream as a filestream, not a memorystream, and for some reason that seemed to help.

              – Marcus
              Jan 14 '11 at 15:15



















            • Check my Update above.

              – Marcus
              Jan 5 '11 at 23:30











            • I wouldnt use a service reference. You can run (from cmd prompt) "svcutil.exe [mex url]" to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. EDIT: also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.

              – norepro
              Jan 6 '11 at 15:16













            • Thanks. I used svcutil to make the "reference" and did all the config stuff in code. I also ended up sending the stream as a filestream, not a memorystream, and for some reason that seemed to help.

              – Marcus
              Jan 14 '11 at 15:15

















            Check my Update above.

            – Marcus
            Jan 5 '11 at 23:30





            Check my Update above.

            – Marcus
            Jan 5 '11 at 23:30













            I wouldnt use a service reference. You can run (from cmd prompt) "svcutil.exe [mex url]" to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. EDIT: also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.

            – norepro
            Jan 6 '11 at 15:16







            I wouldnt use a service reference. You can run (from cmd prompt) "svcutil.exe [mex url]" to get a config file and a .cs file that may be more accurate than what you get from the service reference. I don't even have a service reference in my project and it works fine. Just add that generated .cs file to your project and add a 'using' statement for that. EDIT: also, you can add the config settings to your app or web config file on the client. Those 2 generated files get created in the directory you run the svcutil command in.

            – norepro
            Jan 6 '11 at 15:16















            Thanks. I used svcutil to make the "reference" and did all the config stuff in code. I also ended up sending the stream as a filestream, not a memorystream, and for some reason that seemed to help.

            – Marcus
            Jan 14 '11 at 15:15





            Thanks. I used svcutil to make the "reference" and did all the config stuff in code. I also ended up sending the stream as a filestream, not a memorystream, and for some reason that seemed to help.

            – Marcus
            Jan 14 '11 at 15:15













            4














            I also had this problem and what worked for me was setting the position of the stream to 0 before sending it back to the client.






            share|improve this answer



















            • 1





              I know i am not supposed to say thank you in the comments but SERIOUSLY, THANK YOU SOOOO MUCH. you are a legend

              – Jobin Jose
              Apr 7 '16 at 11:18











            • its always the daft stuff that trips you up

              – MikeT
              Apr 18 '16 at 12:37
















            4














            I also had this problem and what worked for me was setting the position of the stream to 0 before sending it back to the client.






            share|improve this answer



















            • 1





              I know i am not supposed to say thank you in the comments but SERIOUSLY, THANK YOU SOOOO MUCH. you are a legend

              – Jobin Jose
              Apr 7 '16 at 11:18











            • its always the daft stuff that trips you up

              – MikeT
              Apr 18 '16 at 12:37














            4












            4








            4







            I also had this problem and what worked for me was setting the position of the stream to 0 before sending it back to the client.






            share|improve this answer













            I also had this problem and what worked for me was setting the position of the stream to 0 before sending it back to the client.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 12 '13 at 6:11









            SnijderManSnijderMan

            412




            412








            • 1





              I know i am not supposed to say thank you in the comments but SERIOUSLY, THANK YOU SOOOO MUCH. you are a legend

              – Jobin Jose
              Apr 7 '16 at 11:18











            • its always the daft stuff that trips you up

              – MikeT
              Apr 18 '16 at 12:37














            • 1





              I know i am not supposed to say thank you in the comments but SERIOUSLY, THANK YOU SOOOO MUCH. you are a legend

              – Jobin Jose
              Apr 7 '16 at 11:18











            • its always the daft stuff that trips you up

              – MikeT
              Apr 18 '16 at 12:37








            1




            1





            I know i am not supposed to say thank you in the comments but SERIOUSLY, THANK YOU SOOOO MUCH. you are a legend

            – Jobin Jose
            Apr 7 '16 at 11:18





            I know i am not supposed to say thank you in the comments but SERIOUSLY, THANK YOU SOOOO MUCH. you are a legend

            – Jobin Jose
            Apr 7 '16 at 11:18













            its always the daft stuff that trips you up

            – MikeT
            Apr 18 '16 at 12:37





            its always the daft stuff that trips you up

            – MikeT
            Apr 18 '16 at 12:37











            0














            Is their any reason you want to use "BinaryFormatter"? If no, you could return stream from wcf like this



            return new FileStream(@"D:yourFileName.txt", FileMode.Open, FileAccess.Read)






            share|improve this answer
























            • I used BinaryFormatter because the object I am putting in the stream is in memory; it hasn't been written to disk.

              – Marcus
              Jan 6 '11 at 14:39
















            0














            Is their any reason you want to use "BinaryFormatter"? If no, you could return stream from wcf like this



            return new FileStream(@"D:yourFileName.txt", FileMode.Open, FileAccess.Read)






            share|improve this answer
























            • I used BinaryFormatter because the object I am putting in the stream is in memory; it hasn't been written to disk.

              – Marcus
              Jan 6 '11 at 14:39














            0












            0








            0







            Is their any reason you want to use "BinaryFormatter"? If no, you could return stream from wcf like this



            return new FileStream(@"D:yourFileName.txt", FileMode.Open, FileAccess.Read)






            share|improve this answer













            Is their any reason you want to use "BinaryFormatter"? If no, you could return stream from wcf like this



            return new FileStream(@"D:yourFileName.txt", FileMode.Open, FileAccess.Read)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 6 '11 at 10:53









            sreejithsreejith

            61




            61













            • I used BinaryFormatter because the object I am putting in the stream is in memory; it hasn't been written to disk.

              – Marcus
              Jan 6 '11 at 14:39



















            • I used BinaryFormatter because the object I am putting in the stream is in memory; it hasn't been written to disk.

              – Marcus
              Jan 6 '11 at 14:39

















            I used BinaryFormatter because the object I am putting in the stream is in memory; it hasn't been written to disk.

            – Marcus
            Jan 6 '11 at 14:39





            I used BinaryFormatter because the object I am putting in the stream is in memory; it hasn't been written to disk.

            – Marcus
            Jan 6 '11 at 14:39











            0














            In case anyone is searching here I my error was returning a FileStream instead of a Stream from my WCF service - this always returned an empty stream. Changing the return type to a Stream solved my issue.






            share|improve this answer




























              0














              In case anyone is searching here I my error was returning a FileStream instead of a Stream from my WCF service - this always returned an empty stream. Changing the return type to a Stream solved my issue.






              share|improve this answer


























                0












                0








                0







                In case anyone is searching here I my error was returning a FileStream instead of a Stream from my WCF service - this always returned an empty stream. Changing the return type to a Stream solved my issue.






                share|improve this answer













                In case anyone is searching here I my error was returning a FileStream instead of a Stream from my WCF service - this always returned an empty stream. Changing the return type to a Stream solved my issue.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 0:27









                azpcazpc

                25336




                25336






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4609557%2fwcf-stream-read-always-returns-0-in-client%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'