2

我需要创建 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);
            }
        }
    }
}

网络配置:

<?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();
}

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

4

1 回答 1

0

如果你想分部分发送它,你应该在 requestStream.Write() 之后的 while 循环中添加一个刷新

requestStream.Flush();

这是因为流可能会缓存输出,直到需要发送它。请参阅http://msdn.microsoft.com/en-us/library/system.io.stream.flush.aspx

于 2011-11-01T18:32:15.540 回答