C#Azure Function v3依赖项注入-使用Scrutor进行程序集扫描
我正在尝试将依赖项注入与Azure v3函数一起使用。我在下面的文章中使用了Microsoft推荐的启动方法:- 这项活动正在正确地进行,这是非常棒的。然后,我将调用一个在多个项目类型中使用的依赖项解析助手 我使用Scrutor扫描程序集,这样就不必手动将每个接口添加到类中(AddTransient等)。这在ASP.NET核心Web API项目中非常有效,但在Azure函数中根本不起作用。根本没有添加我的解决方案依赖项 这是我的代码:-C#Azure Function v3依赖项注入-使用Scrutor进行程序集扫描,c#,dependency-injection,.net-core,azure-functions,C#,Dependency Injection,.net Core,Azure Functions,我正在尝试将依赖项注入与Azure v3函数一起使用。我在下面的文章中使用了Microsoft推荐的启动方法:- 这项活动正在正确地进行,这是非常棒的。然后,我将调用一个在多个项目类型中使用的依赖项解析助手 我使用Scrutor扫描程序集,这样就不必手动将每个接口添加到类中(AddTransient等)。这在ASP.NET核心Web API项目中非常有效,但在Azure函数中根本不起作用。根本没有添加我的解决方案依赖项 这是我的代码:- public static void AddSo
public static void AddSolutionServices(this IServiceCollection services)
{
services.Scan(scan => scan
.FromCallingAssembly()
.FromApplicationDependencies()
.AddClasses(classes => classes.Where(types => types.FullName.StartsWith("MyNamespace.")))
.UsingRegistrationStrategy(RegistrationStrategy.Append)
.AsMatchingInterface()
.WithTransientLifetime()
);
}
这是我第一次尝试编写Azure函数,所以我想知道是否不可能对这种类型的应用程序使用程序集扫描。任何帮助都将不胜感激
谢谢
更新日期2020年9月8日
我仍然在使用Scrutor进行程序集扫描时遇到问题,我相信这与运行时加载DLL的方式有关,但我不能100%确定这一点。最后,我不得不按照标准的Microsoft文档手动注册服务/类型。Scrutor可以在其他任何地方工作,但不能用于Azure功能。我希望我做错了什么,但找不到解决办法。谢谢@HariHaran的建议 我尝试使用Autofac,并设法使它与我的ASP.NETCore3.0WebAPI项目一起工作。Autofac提供的程序集扫描不起作用,因此我不得不求助于扫描调用程序集以查找我自己的DLL。我无法添加您在评论(AzureFunctions.Autofac)中提到的NuGet软件包,因为它与Autofac.Extensions.DependencyInjection之间存在版本冲突 通过新的程序集扫描,我成功地完成了上述Autofac过程,然后我又尝试使用内置的.NET核心容器和Scrutor。这是我能够创建的助手方法-这适用于Web API项目和Azure函数:- Azure功能的启动类
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(MyNamespace.Gateway.Queue.Function.Startup))]
namespace MyNamespace.Gateway.Queue.Function
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSolutionServices();
}
}
}
using Scrutor;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace Microsoft.Extensions.DependencyInjection
{
public static class DotNetCoreBootstrapper
{
public static void AddSolutionServices(this IServiceCollection services)
{
string path = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
List<Assembly> assemblies = new List<Assembly>();
foreach (string dll in Directory.GetFiles(path, "MyNamespace*.dll"))
{
assemblies.Add(Assembly.LoadFrom(dll));
}
services.Scan(scan => scan
.FromAssemblies(assemblies)
.AddClasses(classes => classes.Where(types =>
types.FullName.StartsWith("MyNamespace.")))
.UsingRegistrationStrategy(RegistrationStrategy.Append)
.AsMatchingInterface()
.WithTransientLifetime()
);
}
}
}
Web API和Azure函数使用的DI帮助程序
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(MyNamespace.Gateway.Queue.Function.Startup))]
namespace MyNamespace.Gateway.Queue.Function
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSolutionServices();
}
}
}
using Scrutor;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace Microsoft.Extensions.DependencyInjection
{
public static class DotNetCoreBootstrapper
{
public static void AddSolutionServices(this IServiceCollection services)
{
string path = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
List<Assembly> assemblies = new List<Assembly>();
foreach (string dll in Directory.GetFiles(path, "MyNamespace*.dll"))
{
assemblies.Add(Assembly.LoadFrom(dll));
}
services.Scan(scan => scan
.FromAssemblies(assemblies)
.AddClasses(classes => classes.Where(types =>
types.FullName.StartsWith("MyNamespace.")))
.UsingRegistrationStrategy(RegistrationStrategy.Append)
.AsMatchingInterface()
.WithTransientLifetime()
);
}
}
}
使用Scrutor;
使用System.Collections.Generic;
使用System.IO;
运用系统反思;
命名空间Microsoft.Extensions.DependencyInjection
{
公共静态类DotNetCoreBootstrapper
{
公共静态void AddSolutionServices(此IServiceCollection services)
{
字符串路径=path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
列表程序集=新列表();
foreach(Directory.GetFiles(路径“MyNamespace*.dll”)中的字符串dll)
{
Add(Assembly.LoadFrom(dll));
}
services.Scan(Scan=>Scan
.FromAssembly(程序集)
.AddClasses(类=>classes.Where(类型=>
types.FullName.StartsWith(“MyNamespace.”)
.使用RegistrationStrategy(RegistrationStrategy.Append)
.AsMatchingInterface()
.WithTransientLifetime()
);
}
}
}
而不是Scrutor
你能尝试使用它吗?我希望能够坚持使用.NET Core容器,而不是使用其他容器。Autofac有更好的社区和功能,并且它支持.NET Core和Framework。真高兴你在这里记录了你的工作,节省了很多时间!对不起,我已经有一段时间没发这个了。最后我没有去看电影。扫描时无法获取相关DLL。我认为这可能是由于Azure函数中加载DLL的方式造成的,但我不能100%确定这一点。我将在上面添加一个更新。我最终发现:)我在github上用Scrutor打开了一个票证,如果您有任何需要添加的内容: