I am running an ASP.NET Core web app and want to upload large files.
I know that when running IIS, the limits can be changed via web.config:
<httpRuntime maxRequestLength="1048576" />
...
<requestLimits maxAllowedContentLength="1073741824" />
How can you do the equivalent while running the new ASP.NET Core Kestrel web server?
I get the exception "Request body too large."
I found this helpful announcement that confirms there is a 28.6 MB body size limit starting with ASP.NET Core 2.0, but more importantly shows how to get around it!
To summarize:
For a single controller or action, use the [DisableRequestSizeLimit] attribute to have no limit, or the [RequestSizeLimit(100_000_000)] to specify a custom limit.
To change it globally, inside of the BuildWebHost() method, inside the Program.cs file, add the .UseKestrel option below:
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
}
For additional clarity, you can also refer to the Kestrel options documentation.
The other answer is for ASP.NET Core 2.0, but I would like to provide the solution for .NET Core 3.x web API.
Your code in program.cs must be like this to work:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
});
});
}
Related
Serilog community.
Firstly, thank you for the great library!
I am trying out Asp.Net on Net 5 Preview 7 at the time of writing this question. I have created 2 web API projects one targeting [netcoreapp3.1] and another targeting [net5].
Below is my bootstrapping code, it is identical for both APIs
using System.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Formatting.Elasticsearch;
namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console(new ExceptionAsObjectJsonFormatter(renderMessage: true))
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
The problem is when I am looking at logs, I no longer see TraceId and SpanId for the API targeting [net5] with the DotNet 5 preview 7 SDK.
I also tested using the vanilla Logger and there was no issue there. Do I have to configure something, did I just miss something or is the DotNet 5 Preview SDK not fully supported yet?
Thanks for any info, much appreciated.
PS: Serilog Nuget packages used
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Formatting.Elasticsearch" Version="8.2.0" />
Seems there was a change for logging in DotNet 5. Have a look at the GitHub issue linked here for the details,
serilog-aspnetcore github issue
In version 3.0 and up of Net Core a web application can be setup this way:
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureAppConfiguration(config =>
{
var settingsFile = Environment.GetEnvironmentVariable("APP_SETTINGS");
if (!string.IsNullOrWhiteSpace(settingsFile))
{
config.AddJsonFile(settingsFile, optional: false);
}
});
webBuilder.UseStartup<Startup>();
});
While using CreateDefaultBuilder very convenient and abstracts a lot of boilerplate, it also forcibly enables IIS integrations for the app. In my case, I want to run the app using raw Kestrel without IIS proxying requests.
Is my only choice to extract all the boilerplate from CreateDefaultBuilder() if I want to not use the IIS integrations, or can I somehow overwrite this? I've been studying the source code for this methods and I can't see a way to solve this.
Do you know of an easier method to achieve this other than having to write all the setup code myself?
How to implement logging in blazor
I’m using Blazor (3.1) Server approach
I want to log (to a file) some events
I have try this extension: https://github.com/BlazorExtensions/Logging but I can’t make it to work as it says.
Can someone point me to a working example? Or Tell me if I’m doing something wrong (I’m getting this error with this approach:
A circular dependency was detected for the service of type 'Microsoft.JSInterop.IJSRuntime)
In order to implement logging for Blazor server you would follow the same approach as you would for a .NET Core or ASP.NET Core application.
Namely in your Program.cs file you would need to modify the CreateHostBuilder method to configure your loggers in a manner such as
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
You could then inject an ILogger into your razor components or throughout the rest of your application using dependency injection.
public class AboutModel : PageModel
{
private readonly ILogger _logger;
public AboutModel(ILogger<AboutModel> logger)
{
_logger = logger;
}
public string Message { get; set; }
public void OnGet()
{
Message = $"About page visited at {DateTime.UtcNow.ToLongTimeString()}";
_logger.LogInformation(Message);
}
}
Be sure to check the Microsoft documentation for information on built in loggers, third party loggers, and just logging in general.
you can use Serilog
this implementation for server side
https://www.c-sharpcorner.com/article/log-data-into-file-using-serilog-framework-in-blazor-server-app/
and this for client side
https://nblumhardt.com/2019/11/serilog-blazor/
also you will need to read
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0
Right now, it appears that logging with Blazor.Extensions.Logging is not supported for Blazor Server-Side applications due to the circular dependency with IJSRuntime:
https://github.com/BlazorExtensions/Logging/issues/44
PHeuter: "IJSRuntime may need to log and the logger needs IJSRuntime to do the logging."
I have a default website that runs well on MacOS
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
However I need to run Websockets, and MVC code, however the following Kestrel configuration doesn't allow me to view index.html on port 500001
How do I (learn how to) properly configure this for MVC and Websockets on MacOS?
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost
.CreateDefaultBuilder(args)
// Increase Shutdown timeout to accomodate background tasks.
//.UseShutdownTimeout(TimeSpan.FromSeconds(10))
.UseStartup<Startup>()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel((hostingContext, options) =>
{
if (hostingContext.HostingEnvironment.IsDevelopment())
{
options.Listen(IPAddress.Loopback, 50001, listenOptions =>
{
listenOptions.UseHttps("localhost.p12", "1234");
});
}
});
Configuring ASP.NET itself is done inside your Startup class (which you've already specified with UseStartup<Startup>() and not inside your Program class.
Open your Startup.cs file and ensure you have:
UseStaticFiles() to serve static files like index.html (index.html is not a Razor View for ASP.NET MVC nor a Razor Page, as those have the extension .cshtml and are generally named after their corresponding controller actions in TitleCase.
UseMvc() to use the ASP.NET Core MVC system with Controller and Razor View support.
I am running an ASP.NET Core web app and want to upload large files.
I know that when running IIS, the limits can be changed via web.config:
<httpRuntime maxRequestLength="1048576" />
...
<requestLimits maxAllowedContentLength="1073741824" />
How can you do the equivalent while running the new ASP.NET Core Kestrel web server?
I get the exception "Request body too large."
I found this helpful announcement that confirms there is a 28.6 MB body size limit starting with ASP.NET Core 2.0, but more importantly shows how to get around it!
To summarize:
For a single controller or action, use the [DisableRequestSizeLimit] attribute to have no limit, or the [RequestSizeLimit(100_000_000)] to specify a custom limit.
To change it globally, inside of the BuildWebHost() method, inside the Program.cs file, add the .UseKestrel option below:
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
}
For additional clarity, you can also refer to the Kestrel options documentation.
The other answer is for ASP.NET Core 2.0, but I would like to provide the solution for .NET Core 3.x web API.
Your code in program.cs must be like this to work:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
});
});
}