簡體   English   中英

AutoFac 委托工廠和生命周期范圍

[英]AutoFac Delegate Factories and the Lifetime Scope

我在我的應用程序中使用委托工廠。 那是因為我用 AutoFac 創建的組件使用需要一些參數的服務類。

我想做的下一件事情是關心這些服務是否被正確清理並使用 AutoFacs 生命周期范圍機制釋放資源。 然而,問題是當我使用委托工廠創建組件時,它們似乎沒有被放入生命周期范圍內,並且在釋放生命周期范圍后不會調用 Dispose。

我想避免通用的 Owned<> 因為我不想將我的應用程序綁定到特定的 IOC。

http://nblumhardt.com/2011/01/an-autofac-lifetime-primer/中解釋了委托工廠創建的組件被放入與委托工廠相同的生命周期范圍內。

我寫了一個小程序來演示這一點。 在這個程序中,我希望調用 Dispose 函數。 不幸的是,這不會發生。 我想念這里嗎……? 下面的代碼有什么問題嗎? 如何保證委托工廠生產的組件進入委托工廠的生命周期?

using Autofac;
using Autofac.Core;
using System;

namespace FactoryTest
{
class Component : IDisposable
{
    public Component(int i) { }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var builder = new ContainerBuilder();

        builder.Register<Func<int, Component>>(c => (x) =>
        {
            // code that is important and that should run
            // inside the delegate factory.
            return new Component(x);
            });

        IContainer root = builder.Build();

        using (ILifetimeScope lf = root.BeginLifetimeScope())
        {
            Func<int, Component> compDelegatefac = lf.Resolve<Func<int, Component>>();
            Component cmp = compDelegatefac(2);
        }
    }
}
}

編輯:如果你不想使用自動生成的工廠,你可以按照委托工廠

namespace FactoryTest
{
    class Component : IDisposable
    {
        public delegate Component Factory(int i);
        public Component(int i) { Console.WriteLine(i); }

        public void Dispose()
        {
            Console.WriteLine("Component disposed");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
           var builder = new ContainerBuilder();

            builder.RegisterType<Component>().AsSelf();

            IContainer root = builder.Build();

            using (ILifetimeScope lf = root.BeginLifetimeScope())
            {
                var compDelegatefac = lf.Resolve<Component.Factory>();
                Component cmp = compDelegatefac(2);
            }
            GC.Collect();

            Console.Read();
         }
    }
 }

在您的示例中,Lifetime Scope 運行良好。

在您的代碼中,您為委托注冊了一個工廠。 這意味着它將處理Func<int, Component>的對象,但不會為您的Component (因為它由Func創建,而不是由Autofac創建)。

這是一個示例(調用 Dispose):

void Main()
{
    var builder = new ContainerBuilder();

    builder.Register<Component>(c=>new Component(123));

    IContainer root = builder.Build();

    using (ILifetimeScope lf = root.BeginLifetimeScope())
    {
        var comp = lf.Resolve<Component>();     
    }
}

class Component : IDisposable
{
    public Component(int i) { }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

在這個例子中,它創建了一個Component的實例,然后(一旦LifetimeScope結束)在它上面調用Dispose

編輯:

void Main()
{
    var builder = new ContainerBuilder();

    builder.Register<Component>(c => new Component(123));
    builder.RegisterType<ComponentUser>().AsSelf();

    IContainer root = builder.Build();

    using (ILifetimeScope lf = root.BeginLifetimeScope())
    {
        var comp = lf.Resolve<ComponentUser>();
    }
}

class ComponentUser
{
    Component Component { get; set;}

    public ComponentUser(Func<Component> func) 
    {
        Component = func();
    }
}

class Component : IDisposable
{
    public Component(int i) { }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

它是如何工作的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM