ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

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

Closed scovel closed 2 years ago

scovel commented 2 years ago

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!

silkfire commented 2 years ago

There's been support added for this to Grace recently:

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseServiceProviderFactory(new GraceServiceProviderFactory());

builder.Host.ConfigureContainer<DependencyInjectionContainer>(c => c.Configure(...));

var app = builder.Build();

@ipjohnson Perhaps this could be added to the documentation and/or the README on the front page?

scovel commented 2 years ago

Thanks for the quick response. Another reason I like Grace. Your support is excellent.

Is this in the Beta? I don't see it in 7.2.1, I'm not seeing it in 8.0.0-beta132.

builder.Host.UseServiceProviderFactory(new GraceServiceProviderFactory());

  Program.cs(17, 44): [CS7036] There is no argument given that corresponds to the required formal parameter 'configuration' of 'GraceServiceProviderFactory.GraceServiceProviderFactory(IInjectionScopeConfiguration)'

Am I missing an import or something?

Thanks

silkfire commented 2 years ago

Yep it should be in 8.0.0-beta132, but I haven't tried it out myself.

I may have messed up the syntax. Try this instead:

builder.Host.ConfigureContainer<DependencyInjectionContainer>((_, c) => c.Configure(...));

Also, if I'm not mistaken you can replace this:

builder.Host.UseServiceProviderFactory(new GraceServiceProviderFactory());

with this:

builder.Host.UseGrace();

scovel commented 2 years ago

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?

ipjohnson commented 2 years ago

@scovel did @silkfire suggestion work for you?

scovel commented 2 years ago

Yes, that did. Thanks for all your help.