Can't get file size when loading from WCF service host by web browser

145 Views Asked by At

I have some test WCF service with streamed webHttpBinding, which allows to download files by web browser. The problem is browser doesn't show file size and download progress. This is the service contract:

[ServiceContract]
public interface IDataTransferService
{
    [WebGet(UriTemplate = "download?file={fileName}")]
    Stream GetDownloadStream(string fileName);
}

This is the service implementation:

public sealed class DataTransferService : IDataTransferService
{
    public Stream GetDownloadStream(string fileName)
    {
        var context = WebOperationContext.Current;
        var stream = File.OpenRead(fileName);
        WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = $"attachment; filename={Path.GetFileName(fileName)}";
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
        WebOperationContext.Current.OutgoingResponse.Headers["Content-Length"] = stream.Length.ToString();
        return stream;
    }
}

And this is the service configuration:

  <system.serviceModel>
    <services>
      <service name="WcfStreamingTest.Server.DataTransferService">
        <endpoint contract="WcfStreamingTest.IDataTransferService"
                  address="http://localhost:8000/streamingtest/api/transfer"
                  binding="webHttpBinding"
                  bindingConfiguration="streamedWeb"
                  behaviorConfiguration="web"/>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="streamedWeb" transferMode="Streamed" sendTimeout="00:15:00" />
      </webHttpBinding>
    </bindings>    
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

Name of file shows correctly, but information about file size is missing.

1

There are 1 best solutions below

1
On

Try to change transferMode to StreamedResponse

<binding name="streamedWeb" transferMode="StreamedResponse" sendTimeout="00:15:00" />