Skip to main content

66 posts tagged with "Web API"

Building HTTP Web APIs That Other Apps Call

View All Tags

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.

Fixed: "Sequence contains no elements" with EF Core FirstAsync

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A request for a category that doesn't exist answered 500, with InvalidOperationException: Sequence contains no elements. The database was fine, and so was the SQL: it ran and found nothing. FirstAsync treats "nothing" as an exception. For an ID a client sent, nothing is a normal answer, so the code should use FirstOrDefaultAsync and reply 404.

Fixed: "JSON deserialization for type was missing required properties" in an ASP.NET Core Minimal API

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A POST with an empty JSON body answered 400 Bad Request with no explanation, and the log said JSON deserialization for type 'Dewiride.Inventory.Data.Entities.Category' was missing required properties including: 'name'. The C# required keyword on my entity was doing its job, too early and too quietly. A request record with validation attributes says the same thing in a way a client can read.

Fixed: "A possible object cycle was detected" When Returning EF Core Entities from a Web API

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

An endpoint that returned EF Core entities answered 500, with JsonException: A possible object cycle was detected. The path in the message gave it away: $.Products.Category.Products.Category.Products…. A category has products, and each product points back to its category, so the JSON would never end. The fix isn't a JSON setting: it's not returning entities in the first place.

Fixed: DbUpdateConcurrencyException "The database operation was expected to affect 1 row(s), but actually affected 0 row(s)" in EF Core

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded. EF Core expected its UPDATE to change one row, and MySQL said none. I got this message three different ways while building an API, and each needs a different answer. Telling them apart takes one look at the SQL.

Fixed: "InvalidCastException: Unable to cast object of type 'System.Attribute[]' to type 'IEnumerable`1[ValidationAttribute]'" with AsParameters in /openapi/v1.json

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

The API itself worked, but /openapi/v1.json started answering 500: InvalidCastException: Unable to cast object of type 'System.Attribute[]' to type 'System.Collections.Generic.IEnumerable1[System.ComponentModel.DataAnnotations.ValidationAttribute]'.The cause was a record used with[AsParameters], with validation attributes on its constructor parameters. It's a bug in ASP.NET Core 10, and adding property:` to each attribute avoids it.

Fixed: "BadHttpRequestException: Failed to read parameter from the request body as JSON" Returns 500 Instead of 400 in a Minimal API

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A request with broken JSON got a 500 Internal Server Error from my minimal API, and the log said BadHttpRequestException: Failed to read parameter "CategoryRequest request" from the request body as JSON. The client's request was wrong, so it should have been a 400. It turned out to depend on the environment: in Development the bad request throws, and UseExceptionHandler turns anything thrown into a 500 unless you tell it otherwise.

How to Connect a .NET C# Web API to a MySQL Database — Part 2: Create MySQL Tables with EF Core Migrations

· 45 min read
Jagdish Kumawat
Founder @ Dewiride

Part 1 connected a .NET C# Web API to MySQL, but the database is still empty. This part describes the inventory data as C# classes and lets EF Core migrations create the matching MySQL tables, with the column types, keys, indexes and constraints you'd otherwise write by hand. You'll read the SQL before it runs, apply it, change the model and add a second migration, and then check every table from inside MySQL.

How to Run an ASP.NET Core App on a Fixed Port (launchSettings.json, --urls and ASPNETCORE_HTTP_PORTS)

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A new ASP.NET Core project listens on a port the template picked at random, such as 5255. That's fine until you write it into a .http file, a README or another app's settings, and a teammate's copy uses a different one. There are three ways to choose the port, for different situations. This guide tries each one on the same .NET 10 Web API and shows which one wins.