【问题标题】:Ninject with WCF and Interception (for AOP)Ninject with WCF and Interception (for AOP)
【发布时间】:2012-01-16 23:31:04
【问题描述】:

我一直在尝试让 ninject 在 wcf 中工作,使用 wcf 扩展和使用 dynamicproxy2 扩展的拦截。我基本上已经创建了一个 Time 属性,并让它在一个基本场景中工作。我遇到麻烦的地方是在 ninject 模块中我使用构造函数参数创建服务绑定:

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

一切正常,但 Time 属性不会触发我的 Service1 或 MyDependency 中的任何内容。

时间属性是互联网上的标准属性。唯一真正的另一段代码是 CreateKernel 方法是 global.asax,它看起来像这样:

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

感谢您的帮助!

马特

编辑 2011 年 12 月 12 日:根据要求,我在下面添加了更多详细信息: 整个 wcf ninject 模块:

public class WcfNinjectModule : NinjectModule
{

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

global.asax中的create kernel方法如上,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 上触发。

马特

【问题讨论】:

  • 使用.WithConstructorArgument("dependency", Kernel.Get&lt;IMyDependency&gt;());的原因是什么?。 Ninject 会在没有它的情况下找到依赖项。如果这没有帮助,请您添加完整的问题,而不仅仅是部分问题。
  • 我只是更喜欢尽可能明确 - 但即使我删除它,我也会遇到同样的问题。我会尝试在上面添加更多细节,但很难添加所有内容。

标签: wcf attributes ninject aop ninject-extensions


【解决方案1】:

经过更多的工作(并编写了最后的帖子编辑),事实证明,仅删除 WithConstructorArgument 方法确实解决了我的问题,现在一切似乎都工作正常。

马特

【讨论】:

    猜你喜欢
    • 2011-06-28
    • 2013-10-21
    • 1970-01-01
    • 2021-10-26
    • 2021-08-31
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多