WCF problem with uploading large file hosted in IIS

I am trying to upload large files to a WCF service hosted in IIS.

I use the Restful and Streaming method.

But I can not upload files larger than 64 KB.

I tried a lot, changing all the size related elements in the web.config , but could not fix the problem.

Here is my code and config, please let me know if anyone finds any problems in the code and how I can fix it.

Operating contract

 [OperationContract] [WebInvoke(UriTemplate = "/UploadImage/{filename}")] bool UploadImage(string filename, Stream image); 

Fulfillment of the work contract

 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)) > 0) { fileStream.Write(buffer, 0, count); } fileStream.Close(); image.Close(); } return true; } catch (Exception ex) { return false; } } 

web.config

  <system.serviceModel> <services> <service name="Service" behaviorConfiguration="ServiceBehavior"> <endpoint address="http://localhost/WCFService1" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="webBinding" contract="IService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <bindings> <webHttpBinding> <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" > </binding> </webHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> 

and

 <httpRuntime maxRequestLength="2097151"/> 

Service hosted in IIS

Client code (console application)

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

With this large part of the code, I can upload a 64 KB file, but when I try to download a file larger than 64 KB, I get an error like

The remote server returned an error: (400) Bad Request


I did what you said, but still I have the same problem, here is what my configuration looks like now, can you tell me what is still missing here?

 <services> <service name="Service" behaviorConfiguration="ServiceBehavior"> <endpoint address="http://localhost/WCFService1" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="webBinding" contract="IService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <bindings> <webHttpBinding> <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"> <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> </binding> </webHttpBinding> </bindings> 
+7
source share
6 answers

Big data transfer problem with wcf:

I have a wcf 4.0 service hosted on IIS 7 server, Windows 2008. When I call my service with little data, say 4K or 5K bytes, then the request is processed easily, but when trying to load a large size, it gave me the following errors.

  • Bad request 400
  • File not found 404 13 as shown in IIS 7 logs
  • There is no endpoint listening to the myservice url service

In all cases, I was able to transfer a small data request with my client application to the server, but for a large message. Failed to complete the request.

I tried to use all the methods to solve these problems, but nothing worked for me.

After scratching my head for a week and reading all the articles and blogs, I finally realized that

 <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="myBinding" closeTimeout="00:10:00" maxBufferPoolSize="250000000" maxReceivedMessageSize="250000000" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" messageEncoding="Text"> <readerQuotas maxDepth="4500000" maxStringContentLength="4500000" maxBytesPerRead="40960000" maxNameTableCharCount="250000000" maxArrayLength="4500000"/> </basicHttpBinding> </bindings> </system.serviceModel> </configuration> <system.web> <httpRuntime executionTimeout="4800" maxRequestLength="500000000"/> </system.web> <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="500000000"></requestLimits> </requestFiltering> </security> </system.webServer> <!-- other useful setting --> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 

So, I think it can help someone for days ....

+23
source

In accordance with the size you specified, it seems to be an error due to the limit getting to the binding level, i.e. readerQuota values. You can confirm that the server failure exceeded the binding level by capturing WCF traces. We cannot see the configuration you placed to provide the best view of the visible information.

I would capture WCF traces at a detailed level to fix the problem.

Btw, did you try to increase maxRequestLength? http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength.aspx

NTN, Amit Bhatia

+2
source

What a fixed 404 message size was too big a problem for me was part of the WaseemM example:

 <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="500000000"></requestLimits> </requestFiltering> </security> 
+2
source

Look ... everyone who encounters this 64K problem is loading a piece, skipping the most critical point. We set high values ​​for maxAllowedContentLength, maxReceivedMessageSize ... blah blah, but still nothing works :). We all missed the most basic point (... well, at least I was), no matter what binding configuration we set, will we mention that our ENDPOINT SHOULD SHOULD BE CONFIGURED? ... no where ... So basically we should let ENDPOINT know that it should follow our BINDING CONFIG .

 <endpoint address="" binding="webHttpBinding" behaviorConfiguration="Your behaviour config name" bindingConfiguration="YOUR BINDING CONFIG NAME" contract="Your contract service" /> 
+1
source

If big data transfer - if your task is to use MTOM. Just search for "MTOM WCF".

0
source

Chandrachur is correct, regardless of what you specify in <binding/> or <readerQuotas/> , you need to add "bindingConfiguration="my binding config name" to <endpoint/> . Otherwise, it will not work, even if your the binding setting is correct. you need to make sure that your configs are applied to your endpoint. for this you need to set the "bindingConfiguration" correctly.

0
source

All Articles