流式webHttpBinding未流式传输

时间:2011-06-20 09:24:43

标签: wcf upload webhttpbinding

我需要创建WCF REST服务来上传大文件。我将端点设置为流式webHttpBinding,但它还没有流式传输。

服务示例:

[ServiceContract]
public interface IFiles
{
    [OperationContract]
    void UploadFile(Stream stream);
}

public class Files : IFiles
{
    public void UploadFile(Stream stream)
    {
        const int BUFFER_SIZE = 64 * 1024;
        byte[] buffer = new byte[BUFFER_SIZE];

        using (TextWriter logWriter = new StreamWriter("d:\\UploadedFile.log"))
        using (Stream fileStream = new FileStream("d:\\UploadedFile", System.IO.FileMode.Create, System.IO.FileAccess.Write))
        {
            int readBytes = 0;
            while (0 < (readBytes = stream.Read(buffer, 0, BUFFER_SIZE)))
            {
                fileStream.Write(buffer, 0, readBytes);
                logWriter.WriteLine("{0}: {1} bytes saved", DateTime.Now, readBytes);
            }
        }
    }
}

的Web.config:

<?xml version="1.0"?>
<configuration>

  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="WebHttpBinding" maxBufferSize="65536" maxBufferPoolSize="524288"
          maxReceivedMessageSize="1073741824" transferMode="Streamed" />
      </webHttpBinding>
    </bindings>
    <services>
      <service name="WcfService2.Files">
        <endpoint behaviorConfiguration="WebHttpBehavior" binding="webHttpBinding"
          bindingConfiguration="WebHttpBinding" name="Files" contract="WcfService2.IFiles" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebHttpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata />
          <serviceDebug />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.web>
    <httpRuntime maxRequestLength="500000" />
  </system.web>

</configuration>

客户代码:

using (Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlString);
    request.Method = "POST";
    request.ContentType = "application/octet-stream";
    //request.ContentLength = fileStream.Length;
    //request.AllowWriteStreamBuffering = false;
    request.SendChunked = true;
    Stream requestStream = request.GetRequestStream();

    const int BUFFER_SIZE = 32 * 1024;
    byte[] buffer = new byte[BUFFER_SIZE];
    int readBytes = 0;
    while (0 < (readBytes = fileStream.Read(buffer, 0, BUFFER_SIZE)))
    {
        requestStream.Write(buffer, 0, readBytes);
        Console.WriteLine("{0}: {1} bytes sent", DateTime.Now, readBytes);
        System.Threading.Thread.Sleep(200);
    }
    requestStream.Close();

    WebResponse response = request.GetResponse();
}

方法代码只有在调用requestStream.Close()之后才会调用UploadFile。为什么呢?

1 个答案:

答案 0 :(得分:0)

如果你想分批发送它,你应该在requestStream.Write()

之后的while循环中添加一个flush。
requestStream.Flush();

这是因为流可以缓存输出,直到发送它为止。见http://msdn.microsoft.com/en-us/library/system.io.stream.flush.aspx