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.

How do you use Grace with the .Net 6.0 "minimal api"

See original GitHub issue

Love you lib, been using it for years.

I’m trying to figure out how to use Grace with the new “minimal api” templates for .Net 6.0. I’d call it “Startup-less”, since there is no magic Startup.cs class.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0

Specifically, in my old Startup.cs I had the following magic:

   public virtual void ConfigureContainer(IInjectionScope scope)
   {
        scope.Configure(c =>
        {
             c.Export<UserResolver>().As<IUserResolver>().Lifestyle.Singleton();
             c.Export<HttpHeaderResolver>().As<IHttpHeaderResolver>();
             c.Export<Profile>().As<IProfile>();
        }
   }

In the above article, to access the built-in DI container, you do the following:

var app = builder.Build();

app.MapControllers();

using (var scope = app.Services.CreateScope())
{
    var sampleService = scope.ServiceProvider.GetRequiredService<SampleService>();
    sampleService.DoSomething();
}

How do you accomplish the same with Grace?

Thanks!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
scovelcommented, Mar 25, 2022

Yes, I was doing .UseGrace, but needed to know how to get direct access to the container in the main() as opposed to the Startup.cs. It seems like your new syntax compiles. I need to throw a little more code together to make sure it’s all working.

builder.Host.ConfigureContainer<DependencyInjectionContainer>((_, c) =>
{
    c.Configure(block =>
    {
        // register all [Tracing] attributes for Interception
        block.OwningScope.InterceptAttribute<TracingAttribute, TracingInterceptionHandler>();
    });
});

Does this look correct?

0reactions
scovelcommented, Mar 29, 2022

Yes, that did. Thanks for all your help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial: Create a minimal API with ASP.NET Core
Start Visual Studio 2022 and select Create a new project. · In the Create a new project dialog: Enter Empty in the Search...
Read more >
David Grace's Post
One of my favourite features of .NET 6 is Minimal APIs. Minimal APIs allows you to put API endpoints in the Program.cs file...
Read more >
NET 6.0 - Minimal API Tutorial and Example
A step by step tutorial on how to build a minimal .NET 6.0 API from scratch with a couple of example endpoints/routes, with...
Read more >
Minimal APIs in .NET 6
In this article, we are going to explain the core idea and basic concepts of the minimal APIs in .NET 6 with examples....
Read more >
Minimal APIs in .NET 6 — A Complete Guide(Beginners ...
NET6, you can use extension methods that come with minimal APIs. app.MapGet(“/books”, async (BooksDB db) =>await db.Books.ToListAsync()) ...
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