Closed tmc101 closed 2 years ago
This issue can be seen in lamar\src\LamarWithMinimalApiOnNet6\Program.cs since commit:
https://github.com/JasperFx/lamar/commit/5c6cff1a118b395437e6e518b1241b810c0d73b4
Accessing the HelloController fails:
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.
@tmc101 It did at the time the docs were produced. I happily take pull requests for that if you're interested.
Hi @jeremydmiller Thanks for the reply.
I guess a change in dotnet 6 could have broken commit 5c6cff1a118.
In https://github.com/JasperFx/lamar/issues/320 you recommend putting the AddControllers() call outside of UseLamar().
This does work:
// use Lamar as DI.
builder.Host.UseLamar((context, registry) =>
{
// register services using Lamar
registry.For<ITest>().Use<MyTest>();
registry.IncludeRegistry<MyRegistry>();
});
// add the controllers
builder.Services.AddControllers();
Would this be the canonical way to use Lamar with .Net 6 minimal hosting?
If so I can submit a pull request to update lamar\src\LamarWithMinimalApiOnNet6\Program.cs and the docs.
@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.
I'm finally looking at this today.
*I have the "fix" for this in a local branch, but need a touch of assistance from someone else to get a test in place for the behavior. Once that's done, I'll push a version of Lamar with this fix.
Just move the call to AddControllers()
outside of the UseLamar()
block like so:
var builder = WebApplication.CreateBuilder(args);
// DO IT HERE!
builder.Host.ConfigureServices(s => s.AddControllers());
// use Lamar as DI.
builder.Host.UseLamar((context, registry) =>
{
// register services using Lamar
registry.For<ITest>().Use<MyTest>();
registry.IncludeRegistry<MyRegistry>();
// NOT HERE!
// registry.AddControllers();
});
var app = builder.Build();
app.MapControllers();
app.MapGet("/", (ITest service) => service.SayHello());
app.Run();
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):
I can access their example WeatherForecastController here:
http://localhost:5156/weatherforecast
If I then add this Lamar dependency:
According the the documentation here:
I would expect this Program.cs to work:
But I get a 404 error when trying to access http://localhost:5156/weatherforecast.
The Kestrel log shows:
However this Program.cs works fine:
Is the documentation at https://jasperfx.github.io/lamar/guide/#lamar-with-asp-net-core-minimal-hosting still applicable to .Net 6 minimal hosting?