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>