[英]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.DependencyInjection
和Microsoft.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 interface
和a 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.