Async IServiceProvider in .NET Core DI

Although it is theoretically possible to use async/await during object resolution, you should consider the following constraints: Constructors can’t be asynchronous, and Construction of object graphs should be simple, reliable and fast Because of these constraints, it’s best to postpone everything that involves I/O until after the object graph has been constructed. So instead of … Read more

Update/change roles claim (or any other claim) in JWT

Refresh tokens don’t seem to be the solution if you care about the changes you make being instant, you probably don’t want an user to access moderation tools for some time if you revoke his permissions. What you could do is keep a version number in the jwt token relative to the user, much like … Read more

Using a C# 7 tuple in an ASP.NET Core Web API Controller

It doesn’t work because named tuple names are not quite “real”, it’s mostly syntax sugar provided by compiler. If you look at ValueTuple set of types, by which named tuples are represented, you will see that they have properties like Item1, Item2 and so on. Compiler will rewrite all your references to named tuple names … 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

The input was not valid .Net Core Web API

Don’t use FromBody. You’re submitting as x-www-form-urlencoded (i.e. standard HTML form post). The FromBody attribute is for JSON/XML. You cannot handle both standard form submits and JSON/XML request bodies from the same action. If you need to request the action both ways, you’ll need two separate endpoints, one with the param decorated with FromBody and … Read more

Set dummy IP address in integration test with Asp.Net Core TestServer

You can write middleware to set custom IP Address since this property is writable: public class FakeRemoteIpAddressMiddleware { private readonly RequestDelegate next; private readonly IPAddress fakeIpAddress = IPAddress.Parse(“127.168.1.32”); public FakeRemoteIpAddressMiddleware(RequestDelegate next) { this.next = next; } public async Task Invoke(HttpContext httpContext) { httpContext.Connection.RemoteIpAddress = fakeIpAddress; await this.next(httpContext); } } Then you can create StartupStub class … Read more

How can I use DateOnly/TimeOnly query parameters in ASP.NET Core 6?

Turns out, there are two solutions: Custom ModelBinder + Swagger configuration Custom TypeConverter (Swagger will automatically see it, and update UI accordingly) I went with TypeConverter, and everything worked! Since .Net team are not planning to add full support for DateOnly/TimeOnly in .Net 6, I’ve decided to create a NuGet to do so: https://www.nuget.org/packages/DateOnlyTimeOnly.AspNet (source … Read more

Unable to resolve service for type ‘Swashbuckle.AspNetCore.Swagger.ISwaggerProvider’

A call to services.AddSwaggerGen(); appears to be missing in the ConfigureServices. public static IServiceCollection AddSwaggerGen( this IServiceCollection services, Action<SwaggerGenOptions> setupAction = null) { // Add Mvc convention to ensure ApiExplorer is enabled for all actions services.Configure<MvcOptions>(c => c.Conventions.Add(new SwaggerApplicationConvention())); // Register generator and it’s dependencies services.AddTransient<ISwaggerProvider, SwaggerGenerator>(); services.AddTransient<ISchemaGenerator, SchemaGenerator>(); services.AddTransient<IApiModelResolver, JsonApiModelResolver>(); // Register custom configurators … Read more