2014-02-13 44 views
1

我試圖使用Autofac OnActivated創建一個類Autofac OnActivated不工作/調用

除了

builder.Register(c => new MyThing(c.Resolve<MyOtherThingDependancy>())) 
    .As<IMyThing>() 
    .SingleInstance() 
    .OnActivated(c=> new MyOtherThing(c.Instance)); // i only need this to be instantiated once 

MyOtherThing的線條具有類似構造函數:

public MyOtherThing(IMyThing myThing) 

然而,它不是射擊

MyOtherThing永遠不會被實例化

我在做什麼錯?

回答

4

我不知道爲什麼你的OnActivated是不會發生的,但你可以這樣做,而不是:

builder.Register(c => new MyThing(c.Resolve<MyOtherThingDependancy>())) 
    .As<IMyThing>() 
    .SingleInstance() 

builder.RegisterType<MyOtherThing>().AutoActivate(); 

我認爲使用AutoActivate有更清晰的意圖,然後你的風格。

+1

我不知道AutoActivate的....我沒有看到它的文檔,但已經我同意,更加清晰的意圖! – Alex