首页 > 解决方案 > 在 Azure Function 中引用类库 .dll 文件,是否需要在 Startup.cs 中设置服务?

问题描述

我已经在 Azure Function 中使用Microsoft.Azure.Functions.Extensions.DependencyInjectionand设置了 DI Microsoft.Extensions.DependencyInjection。所以这是我的启动:

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

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

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

标签: c#azuredependency-injectionreferenceazure-functions

解决方案


这取决于你。

如果你想使用Azure 函数的依赖注入(推荐),你应该总是为每个interfaces & implementationsfrom设置服务SomeOtherProject.dll

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

但是如果你不选择使用Dependency Injection,则无需这样做,直接使用它们即可。


推荐阅读