I am going through Get Started at FastEndpoints. I have followed the guide step by step for the bare minimum example, in a blank project, adding nothing else. But when I get to the line app.UseFastEndpoints()
in the program.cs file, it throws the following exception:
An unhandled exception of type 'System.TypeInitializationException' occurred in FastEndpoints.dll: 'The type initializer for 'FastEndpoints.Config' threw an exception.'
Adding some try catch, I can get the inner exception which is:
Could not load type 'Microsoft.AspNetCore.Http.Metadata.IEndpointMetadataProvider' from assembly 'Microsoft.AspNetCore.Http.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
I have had no luck on neither Google nor ChatGPT (3.5). Has anyone seen this issue before and if so, how did you solve it?
Code
(The code is exactly the same as in the Getting Started guide, but I'll add it for future reference)
Program.cs
using FastEndpoints;
var bld = WebApplication.CreateBuilder();
bld.Services.AddFastEndpoints();
var app = bld.Build();
app.UseFastEndpoints();
app.Run();
MyEndpoint.cs
using FastEndpoints;
public class MyEndpoint : Endpoint<MyRequest, MyResponse>
{
public override void Configure()
{
Post("/api/user/create");
AllowAnonymous();
}
public override async Task HandleAsync(MyRequest req, CancellationToken ct)
{
await SendAsync(new()
{
FullName = req.FirstName + " " + req.LastName,
IsOver18 = req.Age > 18
});
}
}
public class MyRequest
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class MyResponse
{
public string FullName { get; set; }
public bool IsOver18 { get; set; }
}
MyWebApp.csproj
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FastEndpoints" Version="5.18.0" />
</ItemGroup>
There seems to be a conflict with FastEndpoints version 5.18.0 and .NET SDK 7.0.100-preview (7.0.100-preview.6.22352.1 to be exact), which I was using.
At least updating to .NET SDK 7.0.401 solved the problem.
Alternatively it also worked by downgrading FastEndpoints to 5.10.0, but that's rarely a good solution.