c# - 在 Azure Function 中引用类库 .dll 文件,是否需要在 Startup.cs 中设置服务?
问题描述
我已经在 Azure Function 中使用Microsoft.Azure.Functions.Extensions.DependencyInjection
and设置了 DI Microsoft.Extensions.DependencyInjection
。所以这是我的启动:
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient<IThingFactory, ThingFactory>();
}
}
这在项目中运行良好,但是我添加了对SomeOtherProject.dll
(我的类库项目之一)的引用。
我的问题是:我是否需要为SomeOtherProject
我将要使用的每个接口和实现设置服务?
解决方案
这取决于你。
如果你想使用Azure 函数的依赖注入(推荐),你应该总是为每个interfaces & implementations
from设置服务SomeOtherProject.dll
。
例如,在 中SomeOtherProject.dll
,您已经an interface
命名IRepository
并a class
命名Repository
了哪个实现了接口。然后在 azure 函数中,你引用 this SomeOtherProject.dll
,你想像这个示例一样使用依赖注入,你必须Startup class
像这样注册它们builder.Services.AddSingleton<IRepository, Repository>();
但是如果你不选择使用Dependency Injection
,则无需这样做,直接使用它们即可。
推荐阅读
- elasticsearch - 在 ElasticSearch 查询、索引中使用逗号分隔的字符串进行搜索
- xml - XSLT 转换:基于一组条件的子节点的条件副本
- java - 简单类与继承类之间的映射
- reactjs - 在 Wordpress 页面链接上创建 React 小应用程序/页面?
- flutter - 从包颤动中共享依赖项
- php - 计算两个日期之间的天数应为两天,间隔为 1 天,但应计为 1 天
- flutter - 如何在颤动中序列化数组数组
- python - 一个python函数,它返回一个带有for循环的函数列表
- excel - 从标准另存为对话框获取文件名
- reactjs - 如何在点击反应原生时使用缩略图更改主图像滑块?