question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with using .Net 6 minimal hosting

See original GitHub issue

Hi

I’m trying to use Lamar with the default .Net 6 Web API project generated by Visual Studio 2022.

The following minimal Program.cs works (without Lamar):

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

var app = builder.Build();

app.MapControllers();

app.Run();

I can access their example WeatherForecastController here:

http://localhost:5156/weatherforecast

If I then add this Lamar dependency:

<PackageReference Include="Lamar.Microsoft.DependencyInjection" Version="7.1.1" />

According the the documentation here:

  https://jasperfx.github.io/lamar/guide/#lamar-with-asp-net-core-minimal-hosting

I would expect this Program.cs to work:

using Lamar.Microsoft.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseLamar((context, registry) =>
{
    registry.AddControllers();
});

var app = builder.Build();

app.MapControllers();

app.Run();

But I get a 404 error when trying to access http://localhost:5156/weatherforecast.

The Kestrel log shows:

      info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
            Request starting HTTP/1.1 GET http://localhost:5156/weatherforecast - -
      dbug: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
            Wildcard detected, all requests with hosts will be allowed.
      dbug: Microsoft.AspNetCore.Routing.Matching.DfaMatcher[1000]
            No candidates found for the request path '/weatherforecast'
      dbug: Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware[2]
            Request did not match any endpoints
      dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[9]
            Connection id "0HMF4N473N8HB" completed keep alive response.
      info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
            Request finished HTTP/1.1 GET http://localhost:5156/weatherforecast - - - 404 0 - 541.4319ms

However this Program.cs works fine:

using Lamar.Microsoft.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseLamar();

builder.Host.ConfigureContainer<Lamar.ServiceRegistry>(services =>
{
    services.AddControllers();
});

var app = builder.Build();

app.MapControllers();

app.Run();

Is the documentation at https://jasperfx.github.io/lamar/guide/#lamar-with-asp-net-core-minimal-hosting still applicable to .Net 6 minimal hosting?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
jeremydmillercommented, Feb 4, 2022

@tmc101 Short term, yeah, take AddControllers() outside of UseLamar(). I’ll try to address this early next week and change the mechanics ever so slightly of UseLamar() so that it’s being applied against the host builder’s ServiceCollection and see if I can fix that permanently.

I’d rather hold off just at the moment to see if that can be fixed. Folks don’t read the docs generally anyway, so I’d rather try to actually fix it.

1reaction
tmc101commented, Jan 31, 2022

This look like a duplication of https://github.com/JasperFx/lamar/issues/320

The documentation needs to be updated - as the ‘Hello World’ example doesn’t work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate from ASP.NET Core 5.0 to 6.0
The new .NET 6 minimal hosting model for ASP.NET Core apps requires only one file and a few lines of code. Apps migrating...
Read more >
Net6 updating Program.cs minimal hosting model with ...
I have a Single solution containing 2 projects, and I have a problem updating the Program.cs in order to get the Dependency Injection...
Read more >
Minimal hosting making its way to ASP.NET Core with . ...
Feather HTTP like minimal hosting is making its way to the aspnetcore framework with .NET 6. It will be more like express.js way....
Read more >
Cleaner Startup for .NET 6's Minimal Approach
A cleaner approach to application startup and configuration in .NET 6 after Microsoft both killed and kept the old startup file.
Read more >
New dependency injection features in .NET 6
In this post I talk about some of the new features added to the DI libraries in .NET 6, as well a performance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found