C# Azure Function v3 依赖注入(inject) - 使用 Scrutor 进行程序集扫描

标签 c# dependency-injection .net-core azure-functions

我正在尝试对 Azure Functions v3 使用依赖注入(inject)。我在以下文章中使用了 Microsoft 推荐的启动方法:-

https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection

事件正确触发,这很棒。然后,我将调用我在多个项目类型中使用的依赖项解析助手。

我使用 Scrutor 扫描程序集,这样我就不必手动将每个接口(interface)添加到类(AddTransient 等)。这在 ASP.NET Core Web API 项目中效果很好,但对 Azure Functions 根本不起作用。根本没有添加我的解决方案依赖项。

这是我的代码:-

    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 年 8 月 9 日更新

我仍然在使用 Scrutor 进行程序集扫描时遇到问题,我相信这与运行时加载 dll 的方式有关,但我对此不是 100% 确定。我最终不得不按照标准的 Microsoft 文档手动注册服务/类型。 Scrutor 可以在其他任何地方工作,但不能用于 Azure Functions。我希望我做错了什么,但找不到解决方案。

最佳答案

感谢@HariHaran 的建议。

我尝试使用 Autofac 并设法让它与我的 ASP .NET Core 3.0 Web API 项目一起工作。 Autofac 提供的程序集扫描不起作用,因此我不得不求助于扫描调用程序集以查找我自己的 dll。我无法添加您在评论中提到的 NuGet 包 (AzureFunctions.Autofac),因为它与 Autofac.Extensions.DependencyInjection 之间存在版本冲突。

通过我设法为上述 Autofac 进程进行的新程序集扫描,然后我又尝试使用内置的 .NET Core 容器和 Scrutor。这是我能够创建的辅助方法 - 这适用于 Web API 项目和 Azure 函数:-

Azure Functions 的启动类

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();
        }
    }
}

Web API 和 Azure Functions 使用的 DI Helper

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()
            );
        }
    }
}

关于C# Azure Function v3 依赖注入(inject) - 使用 Scrutor 进行程序集扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58540037/

相关文章:

c# - 如何在 XAML 中定义的 Canvas 子 UIElement 上方的 Canvas 上显示绘图视觉

c# - 如何在c#中分隔名字和姓氏?

dependency-injection - Aurelia Singleton 查看模型

c# - 这是使用命令模式的正确方法吗?

c# - 使用 LINQ GroupBy 链生成具有聚合的多级层次结构

c# - Entity Framework 搜索多个词

java - 优化 @Provides 中的代码以绑定(bind)到我的 guice 包中

c# - 没有映射到数据操作中使用的列 'CleaningEmployees.Id' 的属性

docker - docker hub上的microsoft/dotnet:nanoserver在哪里

c# - configuration.GetValue 列表返回 null