2011-07-29 5 views
2

ILogなどの特定のタイプのプロパティインジェクションを実行するモジュールがあります。は、子生涯スコープのautofacモジュールを継承します。

protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) 
{ 
     registration.Activated += [doing property injection]; 
} 

それは同じ範囲内で正常に動作しますが、子スコープで、AttachToComponentRegistrationはもうトリガーされることはありません、私は、プロパティの注入を可能にするためにモジュールを再度登録する必要があります。

私の質問は、どのように子供の生涯スコープで登録されたモジュールを継承するのですか?またはこれを行う他の方法がありますか?

class Program 
{ 
    static void Main(string[] args) 
    { 
     var builder = new ContainerBuilder(); 
     builder.RegisterModule(new TestModule()); 
     builder.RegisterType<Test>().As<ITest>(); 
     var container = builder.Build(); 

     container.Resolve<ITest>().Say(); // output test11111 

     var scope = container.BeginLifetimeScope("nested", b => 
                  { 
                   // b.RegisterModule(new TestModule()); 
                   b.RegisterType<Test2>().As<ITest2>(); 
                  }); 

     scope.Resolve<ITest>().Say(); 
     scope.Resolve<ITest2>().Say(); 
    } 
} 

public interface ITest 
{ 
    void Say(); 
} 

public class Test : ITest 
{ 
    public void Say() 
    { 
     Console.WriteLine("test1111111"); 
    } 
} 

public interface ITest2 
{ 
    void Say(); 
} 

public class Test2 : ITest2 
{ 
    public void Say() 
    { 
     Console.WriteLine("test2222222"); 
    } 
} 

public class TestModule : Module 
{ 
    protected override void AttachToComponentRegistration(Autofac.Core.IComponentRegistry componentRegistry, Autofac.Core.IComponentRegistration registration) 
    { 
     Console.WriteLine("called for " + registration.Activator.LimitType); 
     base.AttachToComponentRegistration(componentRegistry, registration); 
    } 
} 
+0

あなたはどのオートファックバージョンを使用していますか? –

+0

関連する問題があります:http://code.google.com/p/autofac/issues/detail?id=218 ...また、子スコープ内にコンポーネントを作成/登録するコードを投稿してください?ありがとう! –

+0

Nicholas、plsはソースコードの上でチェックアウトし、ネストされたスコープコンポーネントには "called"が表示されません。 –

答えて

関連する問題