2011-12-09 126 views
2

我一直在努力使用wcf擴展和攔截dynamicproxy2擴展在wcf中工作的ninject。我基本上已經創建了一個時間屬性,並將其全部用於基本場景。當我得到麻煩的是,當ninject模塊中創建一個構造函數的參數我的服務綁定:Ninject與WCF和攔截(對於AOP)

Bind<IMyDependency>().To<MyDependency>(); 
Bind<IService1>().To<Service1>().WithConstructorArgument("dependency", Kernel.Get<IMyDependency>()); 

一切工作正常,但時間屬性在我的服務1或MyDependency東西不會着火。

時間屬性是標準的浮動在互聯網上。唯一的其他部分的代碼確實是CreateKernel方法是在Global.asax,它看起來像這樣:

protected override IKernel CreateKernel() { 
    IKernel kernel = new StandardKernel(
     new NinjectSettings() { LoadExtensions = false }, 
     new WcfNinjectModule(), 
     new DynamicProxy2Module() 
    ); 
    return kernel; 
} 

感謝您的幫助!

馬特

編輯12/12/2011:按照要求,我已經添加了下面一些細節: 整個WCF ninject模塊:

public class WcfNinjectModule : NinjectModule 
{ 

    public override void Load() 
    { 
     Bind<IMyDependency>().To<MyDependency>(); 
     Bind<IService1>().To<Service1>(); 
    } 
} 

的創建全球核方法。 asax在上面,並且global.asax從NinjectWcfApplication繼承。

服務方法是這樣的:

public class Service1 : IService1 
{ 
    private IMyDependency _dependency; 

    public Service1() 
    { 
    } 
    public Service1(IMyDependency dependency) 
    { 
     _dependency = dependency; 
    } 

    [Time] 
    public virtual string GetData(string value) 
    { 
     return string.Format(_dependency.GetMyString(), value); 
    } 
} 
public interface IMyDependency 
{ 
    string GetMyString(); 
} 

public class MyDependency : IMyDependency 
{ 
    [Time] 
    public virtual string GetMyString() 
    { 
     return "Hello {0}"; 
    } 
} 

這是否幫助?

由於除去'WithConstructor'參數,時間攔截屬性將在GetMyString上觸發,但不會在GetData上觸發。

馬特

+0

請告訴我使用'.WithConstructorArgument的原因( 「依賴性」,Kernel.Get ());'? Ninject會在沒有它自己的情況下找到依賴關係。 如果這沒有幫助,請您添加完整的問題,而不僅僅是部分。 –

+0

我只是希望儘可能明確 - 但即使我將其刪除,也會遇到同樣的問題。我會嘗試在上面添加更多細節,但很難添加所有內容。 –

回答

0

一點更多的工作(和編寫最後發表的文章編輯)後,事實證明,只是去掉WithConstructorArgument方法沒有解決我的問題,現在一切都似乎是工作的罰款。

馬特