简体   繁体   English

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

[英]Referencing class library .dll file in Azure Function, do I need to setup the services in Startup.cs?

I've setup DI in my Azure Function by using Microsoft.Azure.Functions.Extensions.DependencyInjection and Microsoft.Extensions.DependencyInjection .我已经使用Microsoft.Azure.Functions.Extensions.DependencyInjectionMicrosoft.Extensions.DependencyInjection在我的 Azure 函数中设置了 DI。 So this is my startup:所以这是我的启动:

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

This works fine within the project however I have added a reference to SomeOtherProject.dll (One of my class library projects).这在项目中工作正常,但是我添加了对SomeOtherProject.dll (我的类库项目之一)的引用。

My question is: do i need to setup services for each of the interfaces & implementations from SomeOtherProject that i'll be using?我的问题是:我是否需要为我将使用的SomeOtherProject每个接口和实现设置服务?

That depends on you.这取决于你。

If you want to use Dependency Injection of Azure function (it's recommended), you should always setup services for each of the interfaces & implementations from SomeOtherProject.dll .如果您想使用Azure 函数的依赖注入(推荐),您应该始终为SomeOtherProject.dll每个interfaces & implementations设置服务。

For example, in SomeOtherProject.dll , you have an interface named IRepository and a class named Repository which implements the interface.例如,在SomeOtherProject.dll ,您有an interface名为IRepository an interfacea class实现该接口的名为Repository a class And then in the azure function, you reference this SomeOtherProject.dll , you want to use Dependency injection like this sample , you must register them in the Startup class like this builder.Services.AddSingleton<IRepository, Repository>();然后在 azure 函数中,你引用这个SomeOtherProject.dll ,你想像这个示例一样使用依赖注入,你必须像这个builder.Services.AddSingleton<IRepository, Repository>();一样在Startup class注册它们builder.Services.AddSingleton<IRepository, Repository>();

But if you don't choose to use Dependency Injection , there is no need to do that, just use them directly.但是如果你不选择使用Dependency Injection ,则没有必要这样做,直接使用它们即可。

I think it is no need.我认为没有必要。 if you are using dll reference, after adding references,it should be free to use SomeOtherProject's interfaces and implementations.如果你使用的是dll引用,添加引用后,应该可以自由使用SomeOtherProject的接口和实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我使用的是 OWIN Startup.cs 类并将所有配置移到那里,我是否需要 Global.asax.cs 文件? - Do I need a Global.asax.cs file at all if I'm using an OWIN Startup.cs class and move all configuration there? 为什么启动我的Azure Function时没有使用startup.cs文件? - Why is the startup.cs file not being used when I start my Azure Function? how to read azure app config values in C# startup.cs class in azure function - how to read azure app config values in C# startup.cs class in azure function 如何从 class 库中获取 IServiceProvider,其中 serviceProvider 构建在 startup.cs 中? - How can I get the IServiceProvider from a class library where the serviceProvider is builded in startup.cs? ASP.NET 5类库中的Startup.cs - Startup.cs in ASP.NET 5 class library 我怎样才能有一个包含两个 .Net Core 项目的解决方案并使用两个 Startup.cs 文件来设置服务? - How can I have one Solution with Two .Net Core projects and use both Startup.cs files to setup services? 在 startup.cs 中动态注册服务 - dynamically register services in startup.cs 如何从 Startup.cs 中写入日志? - How do I write logs from within Startup.cs? 如何使用 Blazor 从 .CS 文件而不是 .RAZOR 文件访问在 Startup.cs 中注释的范围服务中的数据? - How do I access data within a scoped service, annotated in Startup.cs, from a .CS file NOT a .RAZOR file using Blazor? 在 .NET 5 隔离进程中使用 Azure Functions v3 时,如何添加以前在 Startup.cs 中的代码 --&gt; 配置方法 - How do I add code that was previously in Startup.cs -->Configure method when using Azure Functions v3 in .NET 5 isolated process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM