Skip to main content

273 posts tagged with "Fixes"

Fixes for Common Issues and Errors

View All Tags

Fixed: "Body was inferred but the method does not allow inferred body parameters" in a Minimal API

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

After I added one small GET endpoint, every request to my minimal API answered 500, health checks included. The log said Body was inferred but the method does not allow inferred body parameters. The endpoint took a record as a parameter, and ASP.NET Core assumes that comes from the JSON body, which GET doesn't get. [AsParameters] tells it to read the query string instead.

Fixed: "Cannot resolve scoped service 'InventoryDbContext' from root provider" in ASP.NET Core

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

I wanted to read something from the database once, while the app started, so I asked app.Services for the DbContext in Program.cs. The app crashed: Cannot resolve scoped service 'Dewiride.Inventory.Data.InventoryDbContext' from root provider. Scoped services need a scope, and at startup there's no request to provide one. You can create one yourself in two lines.

Fixed: "Cannot consume scoped service 'DbContextOptions' from singleton" in ASP.NET Core

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

My API refused to start: Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions1[…InventoryDbContext]' from singleton 'Dewiride.Inventory.Api.CategoryCache'.A small cache class, registered as a singleton, asked for theDbContext. A singleton lives as long as the app, and a DbContextbelongs to one request, so ASP.NET Core stops it.IServiceScopeFactory` is the way for a singleton to use one.

Fixed: "415 Unsupported Media Type" from a POST to an ASP.NET Core Minimal API

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A POST with perfectly good JSON came back as 415 Unsupported Media Type, and the API's log had nothing to say about it. The JSON wasn't the problem: the request never said it was JSON. curl labels a -d body as a web form unless you tell it otherwise, and a minimal API endpoint that expects JSON won't touch that.

Fixed: "error CA1848: For improved performance, use the LoggerMessage delegates" in .NET

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

The most ordinary line of logging you can write, logger.LogInformation("Counted {Count} products", count), stopped my build: error CA1848: For improved performance, use the LoggerMessage delegates instead of calling 'LoggerExtensions.LogInformation'. The solution turns analyzer warnings into errors, so this rule has teeth. The fix is the [LoggerMessage] source generator, which is a few lines more and gives you faster logging.

Fixed: "error CS0266: Cannot implicitly convert type 'decimal?' to 'decimal'" in C# (Nullable Decimal)

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

error CS0266: Cannot implicitly convert type 'decimal?' to 'decimal'. An explicit conversion exists (are you missing a cast?) A nullable value can be empty, and a plain decimal can't hold "empty", so C# won't copy one to the other without being told how. The cast the message suggests compiles, but it throws at runtime if the value is null, so choose what should happen in that case.

Fixed: "error CS1593: Delegate 'RequestDelegate' does not take 2 arguments" in a Minimal API

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A small minimal API endpoint wouldn't compile: error CS1593: Delegate 'RequestDelegate' does not take 2 arguments. My lambda had two parameters, as lambdas do, so the message made no sense at first. It's about the lambda's return value: TypedResults.NotFound() and TypedResults.Ok(…) have different types, so C# can't tell what the lambda returns, and ends up trying a different MapGet overload.