How to configure concurrency in .NET Core Web API?

ASP.NET Core application concurrency is handled by its web server. For example: Kestrel var host = new WebHostBuilder() .UseKestrel(options => options.ThreadCount = 8) It is not recommended to set Kestrel thread count to a large value like 1K due to Kestrel async-based implementation. More info: Is Kestrel using a single thread for processing requests like … Read more

How to enable cors in ASP.NET Core 6.0 Web API project?

Add service builder.Services.AddCors and app add app.UseCors(“corsapp”); replace builder.WithOrigins(“*”) with builder.WithOrigins(“http://localhost:800”, “https://misite.com”); check documentation var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); //services cors builder.Services.AddCors(p => p.AddPolicy(“corsapp”, builder => { builder.WithOrigins(“*”).AllowAnyMethod().AllowAnyHeader(); })); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } … Read more

Read request body twice

If you’re using application/x-www-form-urlencoded or multipart/form-data, you can safely call context.Request.ReadFormAsync() multiple times as it returns a cached instance on subsequent calls. If you’re using a different content type, you’ll have to manually buffer the request and replace the request body by a rewindable stream like MemoryStream. Here’s how you could do using an inline … Read more

How to implement a “pure” ASP.NET Core Web API by using AddMvcCore()

What is the difference between AddMvc() and AddMvcCore()? The first thing key thing to understand is that AddMvc() is just a pre-loaded version of AddMvcCore(). You can see the exact implementation of the AddMvc() extension at the GitHub repository. I like using default VS templates as much as the next guy, but sometimes you need … Read more

How to compile .NET Core app for Linux on a Windows machine

Using dotnet build command, you may specify –runtime flag -r|–runtime < RUNTIME_IDENTIFIER > Target runtime to build for. For a list of Runtime Identifiers (RIDs) you can use, see the RID catalog. RIDs that represent concrete operating systems usually follow this pattern [os].[version]-[arch] Fo example, to build a project and its dependencies for Ubuntu 16.04 … Read more

What is the best practice in EF Core for using parallel async calls with an Injected DbContext?

Using any context.XyzAsync() method is only useful if you either await the called method or return control to a calling thread that’s doesn’t have context in its scope. A DbContext instance isn’t thread-safe: you should never ever use it in parallel threads. Which means, just for sure, never use it in multiple threads anyway, even … Read more

How do I resolve the issue the request matched multiple endpoints in .Net Core Web Api

What you’re trying to do is impossible because the actions are dynamically activated. The request data (such as a query string) cannot be bound until the framework knows the action signature. It can’t know the action signature until it follows the route. Therefore, you can’t make routing dependent on things the framework doesn’t even know … Read more