简体   繁体   English

属性路由Web API中的ninject设置器注入

[英]ninject setter injection in attribute routing Web API

I am trying ninject.mvc5 to do DI on Web API application. 我正在尝试ninject.mvc5在Web API应用程序上执行DI。

I added ninject and ninject.mvc5 in my main MVC project. 我在主MVC项目中添加了ninject和ninject.mvc5。 I added one class library for Web API controllers and using attribute routing. 我为Web API控制器添加了一个类库,并使用了属性路由。 I was trying to inject an object using Setter Injection on both MVC project and class library. 我试图在MVC项目和类库上使用Setter Injection注入对象。 Object properly injected in MVC project whereas it's null in class library. 对象已正确注入MVC​​项目中,而在类库中为null。

Below code block is from MVC application where Stage contains correct object. 下面的代码块来自MVC应用程序,其中Stage包含正确的对象。

public class HomeController : Controller
{
    [Inject]
    public IStage Stage { get; set; }
}

Below code is from class library where its failing. 下面的代码来自其失败的类库。

public class UserController : ApiController
{
    [Inject]
    public IStage Stage { get; set; }
}

Here are the bindings: 这里是绑定:

public class WebApiApplication : Ninject.Web.Common.WebHost.NinjectHttpApplication
{
    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        //RegisterDependencies();
    }

    protected override IKernel CreateKernel()
    {
        var kernal = new StandardKernel();
        kernal.Load(Assembly.GetExecutingAssembly(), 
            Assembly.Load("UserLibrary"), 
            Assembly.Load("StageContracts"), 
            Assembly.Load("StageLibrary"));
        return kernal;
    }
}

public class DependencyMapper : NinjectModule 
{ 
    public override void Load() 
    { 
        this.Bind<IStage>().To<Stage>(); 
    } 
}

Not sure what am I missing here. 不知道我在这里想念什么。 Any help would be much appreciated. 任何帮助将非常感激。

I suspect your DependencyMapper class is not being loaded. 我怀疑您的DependencyMapper类未加载。

From the documentation concerning the overload you are using to load assemblies: 从有关用于加载程序集的过载的文档中:

public static Assembly Load(string assemblyString)

assemblyString Type: System.String The long form of the assembly name. assemblyString类型:System.String程序集名称的长格式。

For instance: 例如:

string longName = "system, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";

Make sure to provide the long names when loading assemblies for the kernel. 加载内核程序集时,请确保提供长名称。

Thanks @JuanR and @NKosi. 谢谢@JuanR和@NKosi。

Finally, I got solution. 最后,我找到了解决方案。 Since the project solution contains MVC and Web API both, we have to set DependencyResolver of both types - System.Web.Http.Dependencies.IDependencyResolver, System.Web.Mvc.IDependencyResolver . 由于项目解决方案同时包含MVC和Web API,因此我们必须设置两种类型的DependencyResolver- System.Web.Http.Dependencies.IDependencyResolver, System.Web.Mvc.IDependencyResolver Below link is the one helped to solve it. 以下链接是帮助解决该问题的链接。

http://blog.developers.ba/simple-way-share-container-mvc-web-api/ http://blog.developers.ba/simple-way-share-container-mvc-web-api/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM