多项目解决方案的 Autofac 范围配置

Autofac scope configuration for multi-project solution

我有一个 ASP.NET 网络应用程序,到目前为止它一直在使用 Autofac 配置 class,它为应用程序中的服务指定了 InstancePerRequest()

从那时起,我在同一解决方案中创建了一个新的控制台应用程序,它将负责 运行 自动化作业流程。因为我不能为我的控制台应用程序使用 InstancePerRequest 配置,所以我需要更改我的配置。理想情况下,我不想将所有配置复制并粘贴到我的 JobRunner 应用程序中并在那里使用“InstancePerLifetimeScope()”配置。

有没有更好的解决方案,我可以在很大程度上使用相同的配置来为两个项目提供服务?也许有一种方法可以覆盖我的 Job Runner 应用程序的配置,但不必为每个服务指定范围更改?

虽然InstancePerLifetimeScopeInstancePerRequest经常有相同的行为,但他们肯定有不同的意图。我会避免使用 InstancePerLifetimeScope 作为替代品,因为它很容易产生意想不到的副作用。例如,如果您有一个服务,您原本打算只在 Web 请求期间存在,但突然间它在您的应用程序期间存在(因为它无意中从根范围解析)。

这种影响在你的工作中会更糟 运行ner,特别是如果你没有创建自己的生命周期范围 - 在这种情况下,一切 都会存在根范围,这意味着一个作业将与依赖它的所有其他作业共享服务实例。

在幕后,InstancePerRequest() 实际上只是委托给 InstancePerMatchingLifetimeScope() 一个众所周知的生命周期标签(你可以从 MatchingScopeLifetimeTags.RequestLifetimeScopeTag 获得)。因此,您可以实现您所要求的一种方法是您可以切断中间人......例如,您可以更改您的 Autofac 模块以将生命周期标签作为构造函数参数:

internal class MyModule : Module
{
    private string _lifetimeScopeTag;

    public MyModule(string lifetimeScopeTag)
    {
        _lifetimeScopeTag = lifetimeScopeTag;
    }

    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterAssemblyTypes()
            // equivalent to: .InstancePerRequest()
            .InstancePerMatchingLifetimeScope(_lifetimeScopeTag);
    }    
}

现在,当您从 Web 构建容器时,您需要提供众所周知的生命周期范围标签:

internal static class WebIoC
{
    public static IContainer BuildContainer()
    {
        var lifetimeScopeTag = MatchingScopeLifetimeTags.RequestLifetimeScopeTag;

        var builder = new ContainerBuilder();
        builder.RegisterModule(new MyModule(lifetimeScopeTag));

        return builder.Build();
    }
}

为了您的工作 运行ner,您现在可以模仿这种行为,使用您自己的生命周期范围标签!

internal static class JobRunnerIoC
{    
    public const string LifetimeScopeTag = "I_Love_Lamp";

    public static IContainer BuildContainer()
    {
        var builder = new ContainerBuilder();
        builder.RegisterModule(new MyModule(LifetimeScopeTag));

        // Don't forget to register your jobs!
        builder.RegisterType<SomeJob>().AsSelf().As<IJob>();
        builder.RegisterType<SomeOtherJob>().AsSelf().As<IJob>();

        return builder.Build();
    }
}

(我在这里假设您的每个作业都实现了一个接口,假设它看起来像这样):

public interface IJob
{
    Task Run();
}

现在您只需要 运行 作业在它们自己的生命周期范围内,使用您刚刚制作的标签,例如:

public class JobRunner
{
    public static void Main(string[] args)
    {
        var container = JobRunnerIoC.BuildContainer();

        // Find out all types that are registered as an IJob
        var jobTypes = container.ComponentRegistry.Registrations
            .Where(registration => typeof(IJob).IsAssignableFrom(registration.Activator.LimitType))
            .Select(registration => registration.Activator.LimitType)
            .ToArray();

        // Run each job in its own lifetime scope
        var jobTasks = jobTypes
            .Select(async jobType => 
            {
                using (var scope = container.BeginLifetimeScope(JobRunnerIoC.LifetimeScopeTag))
                {
                    var job = scope.Resolve(jobType) as IJob;
                    await job.Run();
                }
            });

        await Task.WhenAll(jobTasks);
    }
}