引用Azure Function中的类库.dll文件,是否需要在Startup.cs中设置服务



我已经使用Microsoft.Azure.Functions.Extensions.DependencyInjectionMicrosoft.Extensions.DependencyInjection在Azure函数中设置了DI。这就是我的创业公司:

public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient<IThingFactory, ThingFactory>();
}
}

这在项目中运行良好,但我添加了对SomeOtherProject.dll(我的类库项目之一(的引用。

我的问题是:我需要为每个接口设置服务吗;我将要使用的SomeOtherProject的实现?

这取决于您。

如果你想使用Azure的依赖项注入功能(建议使用(,你应该始终为SomeOtherProject.dll中的每个interfaces & implementations设置服务。

例如,在SomeOtherProject.dll中,有名为IRepositoryan interface和名为Repositorya class,它们实现了接口。然后在azure函数中,引用这个SomeOtherProject.dll,你想像这个示例一样使用依赖注入,你必须像这个builder.Services.AddSingleton<IRepository, Repository>();一样在Startup class中注册它们

但是,如果您不选择使用Dependency Injection,则没有必要这样做,只需直接使用它们即可。

我认为没有必要。如果您使用dll引用,在添加引用之后,应该可以自由使用SomerProject的接口和实现。

最新更新