Azure功能->;无法将IServiceProvider或IFunctionsHostBuilder注入工厂类构造函数
我指的是解释如何在Azure函数中实现DI的链接。我想实现一个Azure功能->;无法将IServiceProvider或IFunctionsHostBuilder注入工厂类构造函数,azure,azure-functions,azure-webjobs,azure-function-app,azure-functions-runtime,Azure,Azure Functions,Azure Webjobs,Azure Function App,Azure Functions Runtime,我指的是解释如何在Azure函数中实现DI的链接。我想实现一个工厂模式,其中我的工厂类应该能够在构造工厂对象时解析其他依赖项 这是代码 public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { System.Console.WriteLine("***************************I
工厂
模式,其中我的工厂类
应该能够在构造工厂对象时解析其他依赖项
这是代码
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>();
}
}
问题
如何将IServiceProvider
注入到我的工厂类中,以构建复杂的对象并解决多个依赖关系
项目详细信息
- Dotnet框架-netcoreapp2.1
- Azure功能版本:2.0
- 下面是一个在.NET内核中使用DI的工厂模式示例。
您不需要注入服务提供者来解析这些类
样本工厂界面
public interface IFactory
{
IWorker Create(WorkerType workerType);
}
public interface IWorker
{
WorkerType WorkerType { get; } // This can be an enum.
void DoWork();
}
示例工厂实现
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 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>();
builder.Services.AddSingleton();
builder.Services.addScope();
builder.Services.addScope();
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
}
builder.Services.AddSingleton<IFactory, Factory>();
builder.Services.AddScoped<IWorker, Worker1Implementation>();
builder.Services.AddScoped<IWorker, Worker2Implementation>();