2011-06-20 6 views
2

大きなファイルをアップロードするには、WCF RESTサービスを作成する必要があります。私はstreamed webHttpBindingとしてエンドポイントを作ったが、ストリーミングされていない。ストリーミングされた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(); 
} 

方法UploadFileのコードのみrequestStream.Close(後に呼び出される)が呼び出されます。どうして?

答えて

0

あなたは部品でそれを送信する場合は、requestStream.Write(後にwhileループでフラッシュを追加する必要があります)

requestStream.Flush(); 

このことは、それを送信するためにnessecaryだまで、ストリームが出力をキャッシュする可能性があるため。 http://msdn.microsoft.com/en-us/library/system.io.stream.flush.aspx

+0

回答ありがとうございます。残念ながら、うまくいきません。 requestStream.Flush()をwhileループのrequestStream.Write()の後に追加し、いずれにしても、requestStream.Close()が呼び出された後にのみUploadFileが呼び出されます。 – vpp

関連する問題