gpt4 book ai didi

Azure Functions -> 无法将 IServiceProvider 或 IFunctionsHostBuilder 注入(inject)工厂类构造函数

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

我指的是this解释如何在 Azure 函数中实现 DI 的链接。我想实现 Factory我的模式在哪里Factory class在构造工厂对象时应该能够解决其他依赖关系。

这是代码。

public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{

System.Console.WriteLine("***************************I am in startup....");
builder.Services.AddScoped<PayloadProcessorFactory>();

/* I tried to explicitly add IFunctionsHostBuilder to Services but its not working.
builder.Services.AddScoped<IFunctionsHostBuilder>((s) =>{
System.Console.WriteLine("test");
return builder;
});*/

/*No problem with below two dependencies and are getting injected without any issue*/
builder.Services
.AddScoped<IPayloadProcessor,DoctorPayloadProcessor>()
.AddScoped<DoctorPayloadProcessor>();

builder.Services
.AddScoped<IPayloadProcessor,PatientPayloadProcessor>()
.AddScoped<PatientPayloadProcessor>();
}
}

这是我的工厂类

public class PayloadProcessorFactory
{
private readonly IFunctionsHostBuilder builder;
private readonly ILogger _logger;
public PayloadProcessorFactory(IFunctionsHostBuilder builder,ILogger<PayloadProcessorFactory> logger)
{
_logger = logger;
this.builder = builder; //Builder is always null
_logger.LogDebug("Constructing PayloadProcesorFactory");
}
}

在上面的代码中,builder 的值始终为 null,因此我无法使用以下语句解析依赖项

payloadProcessor = builder.Services.BuildServiceProvider()
.GetRequiredService<DoctorPayloadProcessor>();

Startup.cs我尝试显式添加 IFunctionsHostBuilderservice collection但在那之后,我遇到了以下问题。

Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Extensions.DependencyInjection.dll but was not handled in user code: 'Unable to resolve service for type 'Microsoft.Azure.WebJobs.Script.IEnvironment' while attempting to activate 'Microsoft.Azure.WebJobs.Script.Configuration.ScriptHostOptionsSetup'.'

问题我该如何注入(inject)IServiceProvider到我的工厂类以构建复杂的对象并解决多个依赖关系?

项目详情

  • Dotnet 框架 - netcoreapp2.1
  • Azure 函数版本:2.0

最佳答案

以下是 .NET Core 中使用 DI 的工厂模式示例。您不需要注入(inject)服务提供者来解析类。

工厂接口(interface)示例

public interface IFactory
{
IWorker Create(WorkerType workerType);
}

工厂实现示例

public class Factory: IFactory
{
private readonly IEnumerable<IWorker> _workers;

public AuthenticationEngineFactory(IEnumerable<IWorker> workers)
{
_workers = workers;
}

public IWorker Create(WorkerType workerType)
{
var worker = _workers.FirstOrDefault(x => x.WorkerType == workerType);
if (worker is null) throw new System.ArgumentException($"Worker with type '{workerType}' is not supported.");
return worker;
}
}

工作界面示例

public interface IWorker
{
WorkerType WorkerType { get; } // This can be an enum.
void DoWork();
}

工作线程实现示例。

public class Worker1Implementation : IWorker
{
public WorkerType WorkerType => WorkerType.WorkerType1;

public void DoWork() {}; // Add implementation
}

public class Worker2Implementation : IWorker
{
public WorkerType WorkerType => WorkerType.WorkerType2;

public void DoWork() {}; // Add implementation
}

在您的 Startup.cs 函数中

builder.Services.AddSingleton<IFactory, Factory>();
builder.Services.AddScoped<IWorker, Worker1Implementation>();
builder.Services.AddScoped<IWorker, Worker2Implementation>();

关于Azure Functions -> 无法将 IServiceProvider 或 IFunctionsHostBuilder 注入(inject)工厂类构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64007285/

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