locked
WCF Streaming problem RRS feed

  • Question

  • User1455999623 posted

    Hi,

    I am creating a WCF service using basicHttpBinding. I simply want the service to return the Stream object to the client.

    Below is the code which is giving null object to the client.

    Below is the Interface Code

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace WcfService2
    {    
        [ServiceContract]
        public interface IStreamingSample
        {
            [OperationContract]
            Stream GetStream(string data);  
        }
    }
    

    Service (Interface implementation)

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace WcfService2
    {
        public class Service1 : IStreamingSample
        {        
            public Stream GetStream(string data)
            {
                data = @"C:\BROM_DLL.log";
                try
                {
                    FileStream imageFile = File.OpenRead(data);
                    return imageFile;
                }
                catch (IOException ex)
                {                
                    throw new FaultException(ex.Message);
                }
            }
        }
    }

    Web.config file of service

         
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="FileTRF">
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <!-- 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 -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <bindings>
         <basicHttpBinding>
            <binding name="HttpStreaming" maxReceivedMessageSize="67108864" transferMode="Streamed"/>
        </basicHttpBinding>
        </bindings>    
        <services>
          <service name="WcfService2.Service1" behaviorConfiguration="FileTRF">
            <endpoint address="" binding="basicHttpBinding" contract="WcfService2.IStreamingSample" bindingConfiguration="HttpStreaming"></endpoint>
          </service>
        </services>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>

    Client side C# code implementing the sevice

     using (ServiceReference1.StreamingSampleClient client = new ServiceReference1.StreamingSampleClient())
                {
                    var data = client.GetStream(@"C:\BROM_DLL.log");
                }

    Result: Getting null in data after executing the GetStream method.


    Error

    Any help would be highly appreciated.

     

     

     

     

    Monday, March 17, 2014 4:36 AM

Answers

  • User-417640953 posted

    Hi shashank,

    Thank you post the issue to asp.net forum.

    Based on your description and code provided, I see you want to complete the wcf download file feature.

    Ant you are using the basichttpbinding with streamed transferMode.

    http://msdn.microsoft.com/en-us/library/ms789010(v=vs.110).aspx

    Your code looks fine in my side, please try to resite the position of stream in wcf side.

      public Stream GetStream(string data)
            {
                data = @"C:\BROM_DLL.log";
                try
                {
                    FileStream imageFile = File.OpenRead(data);
                    imageFile.Position = 0;
                    return imageFile;
                }
                catch (IOException ex)
                {                
                    throw new FaultException(ex.Message);
                }
            }
    

    Besides, I see you get the data in client side as type MessageBodyStream. That means you have get the stream from wcf side.

    Just read it do your job with the data stream variable.

     ServiceGetStream.StreamingSampleClient client = new ServiceGetStream.StreamingSampleClient();
     Stream fileStream = client.GetStream("");
     StreamReader bodyReader = new StreamReader(fileStream);
     string bodyString = bodyReader.ReadToEnd();
     int length = bodyString.Length;
    //test the length
      Response.Write(length);

    Thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 18, 2014 3:46 AM