Documentation bug: How to inject asp.net core dependency injection

Posts   
 
    
mbp
User
Posts: 20
Joined: 03-Jun-2021
# Posted on: 03-Jun-2021 10:36:18   

On the section "Using another Dependency Injection framework" it states that you can use

RuntimeConfiguration.SetDependencyInjectionInfo(services, [...])

inside the method

public void ConfigureServices(IServiceCollection services)

However, SetDependencyInjectionInfo takes an IServiceProvider type and not the IServiceCollection type.

I had better luck with putting the call to "SetDependencyInjectionInfo" here:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    RuntimeConfiguration.SetDependencyInjectionInfo(app.ApplicationServices, 
            (s, e)=>e.AuditorToUse=(IAuditor)s.GetService(typeof(IAuditor)), 
            (s, e) => e.AuthorizerToUse=(IAuthorizer)s.GetService(
                    typeof(IMyAuthorizer<>).MakeGenericType(e.GetType())));
}
Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 03-Jun-2021 21:27:32   

I haven't tried this, but I believe you can get the ServiceProvider by calling BuildServiceProvider extension method on the ServiceCollection.

mbp
User
Posts: 20
Joined: 03-Jun-2021
# Posted on: 04-Jun-2021 09:09:25   

"Avoid calls to BuildServiceProvider in ConfigureServices"

From: https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 04-Jun-2021 10:49:21   

This is what we use in our tests:

[TestFixture]
public class IServiceProviderDITests
{
    [OneTimeSetUp]
    public void Init()
    {
        var host = Host.CreateDefaultBuilder().ConfigureServices((_, services) =>
                                                         services.AddSingleton<IAuditor, Auditor1>()
                                                                 .AddSingleton<IMyAuditor<CustomerEntity>, Auditor2<CustomerEntity>>()
                                                                 .AddTransient<IAuthorizer, Authorizer1>()
                                                                 .AddTransient<IMyAuthorizer<CustomerEntity>, Authorizer2<CustomerEntity>>())
           .Build();
        
        RuntimeConfiguration.AddConnectionString("ConnectionString.SQL Server (SqlClient)", "data source=***************;initial catalog=Northwind;integrated security=SSPI;persist security info=False;packet size=4096");
        RuntimeConfiguration.ConfigureDQE<SQLServerDQEConfiguration>(c => c.SetTraceLevel(TraceLevel.Verbose)
                                                                           .AddDbProviderFactory(typeof(System.Data.SqlClient.SqlClientFactory))
                                                                           .SetDefaultCompatibilityLevel(SqlServerCompatibilityLevel.SqlServer2012));
        RuntimeConfiguration.SetDependencyInjectionInfo(host.Services, (s, e)=>e.AuditorToUse=(IAuditor)s.GetService(typeof(IAuditor)), 
                                                        (s, e) => e.AuthorizerToUse=(IAuthorizer)s.GetService(typeof(IMyAuthorizer<>).MakeGenericType(e.GetType())));
        RuntimeConfiguration.Tracing.SetTraceLevel("ORMPersistenceExecution", TraceLevel.Info);
        RuntimeConfiguration.Tracing.SetTraceLevel("ORMPlainSQLQueryExecution", TraceLevel.Info);
    }

Looking at it, the equivalent for aspnet is indeed a different method. I think we looked at the wrong method because this forum sets up the LLBLGen Pro work in ConfigureServices (tho we don't use an external DI framework). We'll correct the docs ASAP (we're not asp.net specialists wink )

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 04-Jun-2021 11:03:01   

Fixed

Frans Bouma | Lead developer LLBLGen Pro