gpt4 book ai didi

c# - 使用 Autofac 在 Azure Functions 启动类中未解析 ILogger

转载 作者:行者123 更新时间:2023-12-03 03:27:52 24 4
gpt4 key购买 nike

我正在尝试获取 ILogger<T> 的实例解决Startup中的依赖关系时进程版本中的 Azure Function v4 的类。

只有这样不起作用,并且会导致异常、null 或不工作的记录器。

我关注了以下AutoFac documentation for Azure Functions .

我注册了一些类型并尝试解析 Startup 中的类型类(class)。问题是ILogger<T> 。在这种情况下 Logger<EventBusServiceBus> .

在示例中,我尝试解析 IEventBusConfigureEventBus方法。正因为如此,services.AddSingleton<IEventBus, EventBusServiceBus>(sp => ....);正试图在 AddEventBus 中解决方法。

我不明白为什么它无法解析,因为我在 Autofac 容器中看到了一些注册类型。

查看代码中的注释。

谢谢!

文档中的简化启动:

internal class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
// Use IServiceCollection.Add extension method to add features as needed, e.g.
//builder.Services.AddDataProtection();

builder.Services.AddSingleton(GetContainer(builder.Services));

// Important: Use AddScoped so our Autofac lifetime scope gets disposed
// when the function finishes executing
builder.Services.AddScoped<LifetimeScopeWrapper>();

builder.Services.Replace(ServiceDescriptor.Singleton(typeof(IJobActivator), typeof(AutofacJobActivator)));
builder.Services.Replace(ServiceDescriptor.Singleton(typeof(IJobActivatorEx), typeof(AutofacJobActivator)));

builder.Services.AddCustomIntegrations(configuration);
builder.Services.AddEventBus(configuration);

var serviceProvider = builder.Services.BuildServiceProvider();
ConfigureEventBus(serviceProvider);
}

private static IContainer GetContainer(IServiceCollection serviceCollection)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(serviceCollection);
containerBuilder.RegisterModule<LoggerModule>();

// This is a convenient way to register all your function classes at once
containerBuilder.RegisterAssemblyTypes(typeof(Startup).Assembly)
.InNamespaceOf<Function1>();

// TODO: Register other dependencies with the ContainerBuilder like normal

return containerBuilder.Build();
}

private void ConfigureEventBus(ServiceProvider sp)
{
var eventBus = sp.GetRequiredService<BuildingBlocks.EventBus.Abstractions.IEventBus>();

// Unable to use eventBus here
}
}

一些扩展方法:

static class CustomExtensionsMethods
{
public static IServiceCollection AddCustomIntegrations(this IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
{
var serviceBusConnectionString = configuration["EventBusConnection"];

var subscriptionClientName = configuration["SubscriptionClientName"];

return new DefaultServiceBusPersisterConnection(serviceBusConnectionString);
});

return services;
}

public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
{
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();

// Get autofac container so we rolve dependencies from there
var autoFacContainer = sp.GetRequiredService<IContainer>();


// This doesn't work
////var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();

// This does work
var iLifetimeScope = autoFacContainer.Resolve<ILifetimeScope>();


// This doesn't work
//var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();


// This doesn't work either but at least now no error/exception. Resolves into empty logger.
var loggerFactory = new LoggerFactory();
var logger = loggerFactory.CreateLogger<EventBusServiceBus>();


// Tried these for logging without luck.
// Don't understand because I see a loggerfactory and Ilogger in the container...

//var loggerFactory = autoFacContainer.ResolveNamed<ILoggerFactory>("loggerFactory");
//var loggerFactory = autoFacContainer.Resolve<ILoggerFactory>();
////var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
////var logger = loggerFactory.CreateLogger<EventBusServiceBus>();

////autoFacContainer.Resolve<ILogger<EventBusServiceBus>>();

var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
string subscriptionName = configuration["SubscriptionClientName"];

return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, iLifetimeScope, subscriptionName);
});

services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();

return services;
}
}

最佳答案

所以我现在确实找到了一个可行的解决方案。也许有人有更好的解决方案,但这目前对我有用......

public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
{
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();

// Get autofac container so we resolve dependencies from there
var autoFacContainer = sp.GetRequiredService<IContainer>();

// This does work
var iLifetimeScope = autoFacContainer.Resolve<ILifetimeScope>();

// Create a new logger that is connected to the console with a minimum loglevel from host.json based on the namespace
var loggerFactory = LoggerFactory.Create(builder =>
{
var logLevel = configuration.GetValue<LogLevel>("AzureFunctionsJobHost:logging:logLevel:" + typeof(Startup).Namespace, LogLevel.Information);

builder.AddConsole();
builder.SetMinimumLevel(logLevel);
});
var logger = loggerFactory.CreateLogger<EventBusServiceBus>();

var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
string subscriptionName = configuration["SubscriptionClientName"];

return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, iLifetimeScope, subscriptionName);
});

return services;
}

所以重要的部分是:

// Create a new logger that is connected to the console with a minimum loglevel from host.json based on the namespace
var loggerFactory = LoggerFactory.Create(builder =>
{
var logLevel = configuration.GetValue<LogLevel>("AzureFunctionsJobHost:logging:logLevel:" + typeof(Startup).Namespace, LogLevel.Information);

builder.AddConsole();
builder.SetMinimumLevel(logLevel);
});
var logger = loggerFactory.CreateLogger<EventBusServiceBus>();

关于c# - 使用 Autofac 在 Azure Functions 启动类中未解析 ILogger<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75315012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com