Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Minimal API example #37

Answered by NikolayPianikov
Serg046 asked this question in Q&A
Discussion options

Although there is WebAPI example, it is still not clear how to apply this pattern to modern minimal APIs. Ideally, I'd have one root and bind all the services in that composition but not sure what the proper way is.

You must be logged in to vote

@Serg046 I've added an example that uses a single composition root in a minimal web application. Hopefully it will clarify the use cases. Feel free to ask questions if something is not clear, I will answer and add it to the documentation.

Replies: 2 comments 2 replies

Comment options

@Serg046 I've added an example that uses a single composition root in a minimal web application. Hopefully it will clarify the use cases. Feel free to ask questions if something is not clear, I will answer and add it to the documentation.

You must be logged in to vote
1 reply
Comment options

Serg046 Feb 22, 2024
Collaborator Author

Thank you Nikolay, I get the idea. The only thing is that ILogger<Program> logger is not resolved for me. It works if I move it lower so that it is resolved from the original container, like this

internal partial class Program(
 IWeatherForecastService weatherForecast)
{
 private void Run(WebApplication app)
 {
 app.MapGet("/", async (ILogger<Program> logger) =>
 {
 logger.LogInformation("Start of request execution");
 return await weatherForecast.CreateWeatherForecastAsync().ToListAsync();
 });
 app.Run();
 }
}

Do you have the same? Do you have an idea how to make your exact sample working?

Answer selected by Serg046
Comment options

Thank you Nikolay, I get the idea. The only thing is that ILogger<Program> logger is not resolved for me. It works if I move it lower so that it is resolved from the original container, like this

It looks weird.

Do you have the same? Do you have an idea how to make your exact sample working?

Works for me in both scenarios.

Have you experimented with this example? Or your own? If you have your own, please make sure your composition is defined like this.

Are you able to share it?

You must be logged in to vote
1 reply
Comment options

Serg046 Feb 27, 2024
Collaborator Author

Thanks again, your sample helped. I had to put builder.Host.UseServiceProviderFactory(composition) before builder.Build() which is quite obvious eventually but I didn't notice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /