Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF problem with uploading large file, hosted in IIS
    primarykey
    data
    text
    <p>I am trying to upload large files to a WCF Service hosted in IIS.</p> <p>I am using Restful and Streaming method.</p> <p>But I am not able to upload files which is more than 64KB.</p> <p>I tried lot by changing all the size-related elements in <code>web.config</code> file, but failed to fix the issue.</p> <p>Here is my code and config, please let me know if anyone find any issues in the code and how can I fix.</p> <p><strong>Operation Contract</strong></p> <pre><code>[OperationContract] [WebInvoke(UriTemplate = "/UploadImage/{filename}")] bool UploadImage(string filename, Stream image); </code></pre> <p><strong>Implementation</strong> of Operation Contract</p> <pre><code>public bool UploadImage(string filename, Stream image) { try { string strFileName = ConfigurationManager.AppSettings["UploadDrectory"].ToString() + filename; FileStream fileStream = null; using (fileStream = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None)) { const int bufferLen = 1024; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = image.Read(buffer, 0, bufferLen)) &gt; 0) { fileStream.Write(buffer, 0, count); } fileStream.Close(); image.Close(); } return true; } catch (Exception ex) { return false; } } </code></pre> <p><strong>web.config</strong></p> <pre><code> &lt;system.serviceModel&gt; &lt;services&gt; &lt;service name="Service" behaviorConfiguration="ServiceBehavior"&gt; &lt;endpoint address="http://localhost/WCFService1" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="webBinding" contract="IService"&gt; &lt;identity&gt; &lt;dns value="localhost"/&gt; &lt;/identity&gt; &lt;/endpoint&gt; &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt; &lt;/service&gt; &lt;/services&gt; &lt;bindings&gt; &lt;webHttpBinding&gt; &lt;binding name="webBinding" transferMode="Streamed" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00" receiveTimeout="00:25:00" &gt; &lt;/binding&gt; &lt;/webHttpBinding&gt; &lt;/bindings&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name="ServiceBehavior"&gt; &lt;serviceMetadata httpGetEnabled="true"/&gt; &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;/behaviors&gt; &lt;/system.serviceModel&gt; </code></pre> <p>and</p> <pre><code>&lt;httpRuntime maxRequestLength="2097151"/&gt; </code></pre> <p>Service is hosted in hosted in IIS</p> <p>Client side Code (console application)</p> <pre><code>private static void UploadImage() { string filePath = @"F:\Sharath\TestImage\TextFiles\SampleText2.txt"; string filename = Path.GetFileName(filePath); string url = "http://localhost/WCFService1/Service.svc/"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "UploadImage/" + filename); request.Accept = "text/xml"; request.Method = "POST"; request.ContentType = "txt/plain"; FileStream fst = File.Open(filePath, FileMode.Open); long imgLn = fst.Length; fst.Close(); request.ContentLength = imgLn; using (Stream fileStream = File.OpenRead(filePath)) using (Stream requestStream = request.GetRequestStream()) { int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int byteCount = 0; while ((byteCount = fileStream.Read(buffer, 0, bufferSize)) &gt; 0) { requestStream.Write(buffer, 0, byteCount); } } string result; using (WebResponse response = request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { result = reader.ReadToEnd(); } Console.WriteLine(result); } </code></pre> <p>With this much of code I am able to upload 64KB of file, but when I try to upload a file of more than 64KB in size, I am getting error like, </p> <blockquote> <p><code>The remote server returned an error: (400) Bad Request</code></p> </blockquote> <hr> <p>i did what you told but still I am getting same problem, this is how my config looks like now, can you please tell me what is still missing here</p> <pre><code>&lt;services&gt; &lt;service name="Service" behaviorConfiguration="ServiceBehavior"&gt; &lt;endpoint address="http://localhost/WCFService1" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="webBinding" contract="IService"&gt; &lt;identity&gt; &lt;dns value="localhost"/&gt; &lt;/identity&gt; &lt;/endpoint&gt; &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt; &lt;/service&gt; &lt;/services&gt; &lt;bindings&gt; &lt;webHttpBinding&gt; &lt;binding transferMode="Streamed" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00" receiveTimeout="00:25:00" name="webBinding"&gt; &lt;readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/&gt; &lt;/binding&gt; &lt;/webHttpBinding&gt; &lt;/bindings&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload