ASP.NET Web API request does not reach controller action - c#

I have trouble posting 'large' objects to my web api using the HttpClient's PostAsJsonAsync method. The objects contain a base64 encoded image file.
After a period which seems like the client's timeout setting, the exception
'A task was canceled.'
is thrown by the HttpClient. The timeout is already set very high (10 minutes). This only happens with files larger than ~2 MB. Anything smaller works fine. I know that the request does not even hit the first line of code in the controller method because the first line is a logging line and the log is empty. There are no exceptions in the server event viewer.
It is hard to pin down the issue because the controller works fine when I deploy the web api on my local IIS. But when I deploy it on my Azure VM, it only work for small files. The web.config files are identical.
maxRequestLength is already set high enough.
<httpRuntime targetFramework="4.5.2" maxRequestLength="1048576" />
Instead of IIS hosting in an Azure VM, i've just tried to deploy the API as an app service. The exact same thing happens there.

You can also try setting this limit:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
Note that contrary to the maxRequestLength parameter, this value is in bytes.

Related

Correct configuration to increase Umbraco Processing Memory

I keep getting a SystemOutOfMemoryException when processing large files like 1.6GB in size. I was just wondering if my configuration in web.config is correct. Below are the lines of code I modified to support large files that is more than 1GB in size
<httpRuntime requestValidationMode="2.0" enableVersionHeader="false" maxRequestLength="457286400" executionTimeout="3600" targetFramework="4.5" fcnMode="Single" />
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="3294967295" />
</requestFiltering>
</security>
I'm not sure if this is correct. I also changed the IIS server to run in x64 via Tools-> Options-> Project and Solutions-> Web Projects -> "Use the 65bit version of IIS"
I'm not sure what I'm missing but I keep getting the system out of memory exception even though I already increased the memory size
UPDATE:
I'm processing a large files like .mp4 files or just anytype of file regardless of type as long as the client requires it.
Also I'm not uploading the file using a frontend. What we do is upload the large file via Filezilla and if the file is uploaded to the server we use the Umbraco MediaService to process this file to have an entry in the Media Page. We are also using Hangfire to trigger our background service to process the MediaService
Accepting large files like this, is not a best practice. Eg this gives the possibility to perform a Ddos attack. Also you will need a lot of memory as the file will be handled in memory before persisting it to disk, hence the issue you have with memory.
The better solution would be to chunk them with javascript and send over small packets, and the stitch them back together server side. There is a pretty good explanation on how to build this yourselves : https://www.dotnetcurry.com/aspnet-mvc/893/upload-big-files-aspnet-mvc-azure-storage
(depending on how you handle your sessions, adding it to the session might not resolve your memory issue. I guess you'll need to save it to disk)
Unrelated to the memroy exception, but make sure you wrap this in a <location path="xxx/yyy"> your web.config stuff </location> so that only this URL will accept large files.
Managed to solve this issue by adding a 2nd and 3rd parameter in my Umbraco MediaService. Initially I was using Service.MediaService.Save(iMediaFile) but it seems that when using large file it needed to know who uploaded the file hence it needs the 2nd parameter which is the user_id and I added the third parameter to suppress any error message. As I was encountering an out of memory exception where in fact the error was I didn't pass the user_id in the second paramter. Hence my new code became Service.MediaService.Save(iMediaFile, user_id, false)
Again the solution was not really a configuration as my configuration was enough to process the file. The error was due to missing 2nd parameter

Length requests on HttpDelete in WebAPI?

I have a [HttpDelete] action in my Web API, and when I invoke it with a long URL, I get 400.
I know for sure it's the URL length since I managed to fail / pass the request by adding and removing a single character.
I had a similar issue with [HttpGet] request length which I had resolved by adding configurations to my web.config:
Under system.web:
<httpRuntime maxRequestLength="1048576" maxQueryStringLength="32768" maxUrlLength="65536" waitChangeNotification="2147483647" maxWaitChangeNotification="2147483647" requestPathInvalidCharacters="" targetFramework="4.5" />
Under system.webServer:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" maxQueryString="32768" maxUrl="65536"/>
</requestFiltering>
</security>
But the issue seems to be reproducing in the Delete action as well, and I'm failing to come with a solution. I'm not getting anything in IIS trace and IIS logs as well.
How to fix this?
So I think I found the cause, looks like Microsoft limits URL segments to 260.
more information here, look for the UrlSegmentMaxLength
https://support.microsoft.com/en-us/kb/820129
Edit:
So this is defiantly the issue.
It's not just for Delete, but for any other http type of action, that has a long segment in the URL.
the solution is to update the registry key with the value you want, should you need url segments which are over 260 characters, which is the default value.
Hope this will help someone else.

How to return 504 (gatewayTimeout) in WebApi

In my company API (we use WebAPI 2) we want to return 504 (gateway timeout) when controller action takes to much time. Sometimes inside our WebAPI actions we cooperate with external company APIs, which not always work correctly, so it could be convinient for user.
I added entry in web.config, but it doesn't work.
<system.transactions>
<defaultSettings timeout="00:00:10" />
</system.transactions>
How can I resolve this problem?
timeout is set in number of seconds, and I Think you should use httpruntime executiontimeout instead of transaction timeout wich only affects the transactions within the request
<system.web>
<httpRuntime executionTimeout="10" />
</system.web>

ASP.NET 4.0 C# File Upload multiple files using JQuery maxRequestLength needs to be over 4gb

I need to somehow do a work around with an asp.net File Uploader so it can take more than 4gb of multiple files. Is there any way to set the web.config or write a C# function that just bypasses the Max Limit of File space that is used by ASP.Net and IIS 7? I tried to look at this http://msdn.microsoft.com/en-us/library/hh195568.aspx but I have no idea how to use the function. Please help, anyone with information about a work around that would be great since microsoft has put limits I'm guessing there has to be something out there that can bypass the limitations.
Unfortunately maxRequestLength is an variable of Int32 type (maximum value is 2147483647 - 0x7FFFFFFF)
But if you want to extend maxRequestLangh for your application then you can do that by adding couple of attributes to your httpRuntime in web.config:
<httpRuntime maxRequestLength="<size in bytes>" executionTimeout="<timeout in seconds>" />
Here is an example (2 gb of max lenght and about 10 minutes of timeout):
<httpRuntime maxRequestLength="2147483647" executionTimeout="600" />
Also you need to add security part to system.webServer:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647" />
</requestFiltering>
</security>
Might be useful:
MSDN: maxAllowedContentLength
MSDN: httpRuntime Element
Large File Upload in IIS
Fixing File Upload Size Limit in IIS 7

File Upload Causes Page To Hang

When attempting to upload a file of 35 megs, the website hangs.
I have set a break point on the server side before the File.SaveAs(path) command is even called.
I do a check on the server side to make sure the file is less than 20 megs, but it doesn't even reach this point. The web page just continues to load until I get a connection was reset error.
Increase maxAllowedContentLength in web.config
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1048576000" />
</requestFiltering>
</security>
</system.webServer>
In Web.config in system.web :
<httpRuntime maxRequestLength="100000"/>
100000kb =100 MB
If you upload the file with simple HTTP-POST you need to wait for the whole file to be uploaded. You can't abort the upload earlier in the controller, only with web.config as cosset's answer suggests.
You could use maybe Flash to check the file size on the client already, don't know details about that though
Could this be a good chance to use async as shown here?
There is also an excellent article on async and await here.
Web.Config issues aside, a file that big is going to take some time. I would try not to block the current thread with such an action.

Categories

Resources