【问题标题】:C# Azure Function v3 Dependency Injection - assembly scanning with ScrutorC# Azure Function v3 依赖注入 - 使用 Scrutor 进行程序集扫描
【发布时间】:2020-02-20 17:09:57
【问题描述】:

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

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

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

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

这是我的代码:-

    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。我希望我做错了什么,但无法找到解决方案。

【问题讨论】:

  • 你可以尝试使用nuget.org/packages/AzureFunctions.Autofac而不是Scrutor
  • 我希望能够坚持使用 .NET Core 容器,而不是使用其他容器。
  • Autofac 有更好的社区和功能,它同时支持 .NET Core 和 Framework

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


【解决方案1】:

感谢@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 Function 的启动类

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 Function 使用的 DI 助手

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

【讨论】:

  • 真的很高兴你在这里记录了你的工作,这节省了很多时间!
  • 对不起 - 我发布这个已经有一段时间了。最后我没有Scutor。扫描时无法获取相关的 dll。我认为这可能是由于在 Azure Functions 中加载 dll 的方式,但我对此不是 100% 确定的。我会在上面添加一个更新。
  • 我最终发现 :) 如果你有什么要补充的,我在 github 上用 Scrutor 开了一张票:github.com/khellang/Scrutor/issues/128
猜你喜欢
  • 1970-01-01
  • 2021-05-30
  • 2021-10-10
  • 2020-08-09
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
相关资源
最近更新 更多