System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. download big file in mvc 4 - asp.net-mvc-4

I want to download a file in mvc That size is 200mb
The following error occurs :
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown
my code is :
public FileResult Download(string name)
{
var videoFilePath = HostingEnvironment.MapPath("~/VideoFile/" + name + ".mp4");
//The header information
var file = new FileInfo(videoFilePath);
//Check the file exist, it will be written into the response
if (file.Exists)
{
return File(videoFilePath, System.Net.Mime.MediaTypeNames.Application.Octet,
file.Name);
}
return null;
}
The error does not occur when the file size is small
why ?

Recently I kept getting System.OutOfMemoryException exception while working on a web application with Visual Studio 2013.
The full exception message title was as follow:
System.OutOfMemoryException Exception of type 'System.OutOfMemoryException' was thrown.
To resolve this issue, I had to restart Visual Studio or go to the Windows Task Manager and terminate IIS Express process.
This error could happen due to a variety of reasons related to memory consumption of the application. Looking at my local machine memory, there was always enought memory available! Then what was the cause?
Turns out, Visual Studio uses IIS Exoress 32 bit version by default whereas 64 bit version is available right out there! So as a work around you go to the following path and make Visual studio to use 64 bit version of IISExpress.
Tools | Options | Projects and solutions | Web Projects => Use 64 bit version of IIS Express for websites and projects
Apparently behind the scene Visual Studio changes a flag in windows registry which the key could be found at this path.
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0\WebProjects --> Use64BitIISExpress

I believe your MVC 4 project needs to be updated to support 200 mb files and also, IIS needs to be updated to support 200 mb files.
To change it at the IIS level, I think adding this to your web.config file should work. (untested)
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="209715200"/>
</requestFiltering>
</security>
</system.webServer>
And to pull from another SO post, add the following to your web.config file
<location path="File">
<system.web>
<httpRuntime executionTimeout="60" maxRequestLength="4096" />
</system.web>
</location>
<location path="Picture">
<system.web>
<httpRuntime executionTimeout="60" maxRequestLength="1024" />
</system.web>
</location>
Where "File" and "Picture" are your controller names.
On that link, the accepted answer also addresses upload timeouts, which might be a problem as well for a 200 mb file.

Related

.NET Core 3 & IIS Express maxAllowedContentLength not working

I have a JavaScript application which is posting files to .NET Core 3 Web API.
Web API project works locally on IIS Express.
Files below 30 MB are working fine, but large files does not get through.
Here I tried;
1- Update %userprofile%\my documents\iisexpress\config\applicationhost.config file with new maxAllowedContentLength value.
2- Add RequestSizeLimit attribute to Controller Action.
Is there anything else I am missing here?
I had the same problem (since .NET Core 3), and found a workaround first, then a proper solution.
Workaround: in your web.config file, use "AspNetCoreModule" instead of "AspNetCoreModuleV2", but you no longer benefit from the advantages of the new module.
A solution: add this to your startup.cs file
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = int.MaxValue; // or your desired value
});
I found this info here: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1#application-configuration. IIS options > In-process hosting model, then attribute "MaxRequestBodySize". For this to work, you still need to have your web.config configured with something like this:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647" /> //or your desired value
</requestFiltering>
</security>
Hope it helps!
For NetCore 3.1
1° you can add the attributes :
[RequestSizeLimit(300000000)] // for 300Mb files
[RequestFormLimits(MultipartBodyLengthLimit = 300000000)]
2° in web config add the follow line:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="300000000"
</requestFiltering>
</security>

asp.net core out-of-process fails to start correctly

I'm attempting to us the RunFromPackage App Setting for an Azure Web Site.
I'm using the following stack
asp.net core (out-of-process)
Targeting .Net Framework 4.7.2
and I can no longer get my web application to run correctly. when I hit the url all I get is
"The page cannot be displayed because an internal server error has occurred."
in the response.
I have launched my application using the Kudu powershell window with the command
".{applicationName}.exe"
and it start up fine. No errors or anything
Viewing the event viewer logs all I see is
APPLICATION_MANAGER::~APPLICATION_MANAGER | this=000001D1CD999A60 [TID 8872] [PID 8028]
When turning on the Failed Request Tracing Logs I see the following relevant information
URL_CACHE_ACCESS_START RequestURL="/favicon.ico" 15:37:30.729
URL_CACHE_ACCESS_END PhysicalPath="", URLInfoFromCache="false", URLInfoAddedToCache="true", ErrorCode="The operation completed successfully.
(0x0)" 15:37:30.729
GENERAL_GET_URL_METADATA PhysicalPath="", AccessPerms="545" 15:37:30.729
HANDLER_CHANGED OldHandlerName="", NewHandlerName="aspNetCore", NewHandlerModules="AspNetCoreModule", NewHandlerScriptProcessor="", NewHandlerType="" 15:37:30.729
MODULE_SET_RESPONSE_ERROR_STATUS
Warning ModuleName="IIS Web Core", Notification="BEGIN_REQUEST", HttpStatus="500", HttpReason="Internal Server Error", HttpSubStatus="0", ErrorCode="Access is denied.
(0x80070005)", ConfigExceptionInfo=""
I have tried to turn on the asp.net core module logging but I get no log files. I have also tried to turn on the stdoutlog but nothing is appearing to log.
Here is a copy of my web.config
<configuration>
<system.webServer>
<security>
<access sslFlags="SslNegotiateCert" />
</security>
<serverRuntime uploadReadAheadSize="30000000" />
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" stdoutLogEnabled="true" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="OutOfProcess" arguments="%LAUNCHER_ARGS%">
<handlerSettings>
<handlerSetting name="debugFile" value="\\?\%home%\LogFiles\aspnetcore-debug.log" />
<handlerSetting name="debugLevel" value="FILE,TRACE" />
</handlerSettings>
</aspNetCore>
</system.webServer>
</configuration>
I'm not really sure what is going on. All I can gather is something is going wrong with the IIS Module. from the error message it appears that it cannot read or process my web.config ErrorCode="Access is denied."
Strangely enough I had a previous build of the application up and running. I have tried to isolate the changes that may have broke the site but I cant seem to find out what has cause this.
It appears that this was due to the fact I was trying to get only client authentication on one endpoint.
The following setting
<access sslFlags="SslNegotiateCert" />
in the web.config, and Require incoming Certificate in the App Settings caused my issue.
I then tried the feature of "Certificate exclusion paths" but having this and the web.config access node makes the web server very upset and causes the error message
"The page cannot be displayed because an internal server error has occurred."
I had to remove the node from my web.config file, leave the Required SSL, and set my Certificate Exclusion path and everything turned back online.

Visual Studio 2015. Failed to register URL for site access is denied IIS Express. Access denied 0x80070005

I enabled SSL in Visual Studio 2015 in order to implement Facebook and Google login locally.
I changed the project URL in the Web tab of the project's properties to https://localhost:44300/ and decorated the controller with the RequireHttps attribute - ref #msdn.
Everything worked fine locally.
I reverted settings to HTTP to test something else and that caused me a problem when I tried to get back to HTTPS.
I found this SO question and tried almost every suggested solution.
Error detail:
Failed to register URL "url" for site "site" application "path".
Error description: Access is denied. (0x80070005).
I had to issue this command in DOS to solve the problem in VS 2015:
netsh http add urlacl url=http://{ip_addr}:{port}/ user=everyone
Strangely this was only needed when I moved the project to a different PC. On the original PC I didn't need it.
Turned out this very answer on the same question thread by Cayne led me to the solution.
The port change didn't work because applicationhost.config file, located in .vs folder specific for VS2015, kept bindings combo of old port for Http and Https as a default setting. No matter how many times did I change port to something else while trying with Http (only got clogged with mass of new web site bindings in the config file) as soon as I wanted to switch back to SSL it ended up with the first bindings combo. The port it complained about that can't be registered any more.
Once I deleted that first bindings combo everything was fine.
I hope this will help someone in the future.
Go to C:\Users{username}\Documents\IISExpress\config and open the applicationhost.config file.
Search for the <sites> tag in the document. You will see some lines similar to the following.
<site name="WebSite1" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:8080:localhost" />
</bindings>
</site>
Replace the line <binding protocol="http" bindingInformation="*:8080:localhost" /> as follows.
<binding protocol="http" bindingInformation="*:{required_port_number}:*" />
I think you can even remove the * marks in bindingInformation.
Then restart IIS Server (remove all IIS server related operations using Task Manager and go to C:\Program Files\IIS Express folder and run iisexpress.exe: you might need to Run as Administrator).
A console will open and if all went well, following lines will be displayed.
Successfully registered URL "http://*:{required_port_number}/" for site "Website1" application "/"
...
Also check in browser whether the required URL works now.
Here's a very useful resource...

IIS 8 giving an error

I am trying to work on IIS on my windows 8 but IIS giving an error. Error is as following.
"There was an error while performing this operation.
Details:
Filename:
\?\C:\Windows\Microsoft.NET\Framework64\v4.0.30319\CONFIG\
web.config
Line number: 24
Error: The configuration section 'fullTrustAssemblies' cannot be read beacause
it is missing a section declaration."
IIS gives an error when I click on application pool or "Sites".
Replace the block with
<configuration>
<system.webServer>
<system.web>
<fullTrustAssemblies>
<clear/>
</fullTrustAssemblies>
</system.web>
</system.webServer>
</configuration>
Removes all references to full-trust assemblies from an application.
IIS comes with complete MSDN Documents its already given solution for this
Click Here to clear full trust assemblies

IIS 7.5 Error on Restful WCF 4.0

I've been trying to do a simple restful wcf service that will return JSON. Its working if i will run it in the development server. However if I deploy it on IIS 7.5, i will have this error when i accessed it using http://localhost:70
HTTP Error 500.19 - Internal Server
Error The requested page cannot be
accessed because the related
configuration data for the page is
invalid.
Config Error The configuration section
'standardEndpoints' cannot be read
because it is missing a section
declaration
Here is my configuration file: This is the default file generated by the VS2010.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="LocationService" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
Im new to WCF specially on .net 4.0 and IIS 7.5.
Can anybody help? Or anybody has experienced the same and has fixed already?
Do you definitely have the IIS application pool for your site configured to run with ASP .NET 4.0?
Right click your Virtual Directory in IIS Manager > Manage Application > Advanced Settings > read the app pool name.
Then go to Application Pools, find that name and make sure the .NET Framework column says v4.0.
I had the same error on a w2008 x64 with the app pool running .net 4.0; after installing SP2 the issue disappeared
This issue can be seen on Windows Server 2008 without service pack 2 installed. To fix the problem install Windows Server 2008 Service Pack 2.
Taken from Ram Poornalingam's WebLog entry from the 26th October 2009:
If you encounter the following error in your web application (things hosted in IIS) “The configuration section cannot be read because it is missing a section declaration"
examples
“The configuration section 'standardEndpoints' cannot be read because it is missing a section declaration”
“The configuration section ‘tracking’ cannot be read because it is missing a section declaration”
then you need to install either SP2 of Vista/Win2k8 or the hotfix mentioned in KB article 958854.
Sorry to ask a question that may seem obvious to some, but it might help others (mainly me) if you could clarify the last step:
Then go to Application Pools...
Where do I find Application Pools ?
If you can't tell I am used to working for big companies where someone else did that for me and now I am playing developer and IT director.
Thanks
Ok, after 10 seconds of research (I opened my eyes) and looked right above Sites in IIS Manager