2014-01-14 67 views
0

使用ninject幾年(在基本級別),我正努力使其與全新解決方案中的webapi2一起使用。這適用於我的mvc4應用程序與webapi。WebApi 2和Ninject - 未解決

錯誤:

普通嫌疑犯告訴你,綁定工作不

<Error> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
An error occurred when trying to create a controller of type 'TenantController'. Make sure that the controller has a parameterless public constructor. 
</ExceptionMessage> 
<ExceptionType>System.InvalidOperationException</ExceptionType> 
<StackTrace> 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext() 
</StackTrace> 
<InnerException> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
Type 'AIControlPoint.Data.Api.Controllers.TenantController' does not have a default constructor 
</ExceptionMessage> 
<ExceptionType>System.ArgumentException</ExceptionType> 
<StackTrace> 
at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
</StackTrace> 
</InnerException> 
</Error> 

的webapi2控制器是非常基本的,如果我創建上下文作爲一個無參數初始化

的一部分確實工作
public class TenantController : ApiController 
    { 
     private TenantConfigurationContext tenantContext; 

     public TenantController(TenantConfigurationContext tenantContext) 
     { 
      this.tenantContext = tenantContext; 

     } 

     public List<TenantConfiguration> Get() 
     { 
      var tenants = (from t in tenantContext.TenantConfigurations 
          select t) 
          .ToList(); 

      return tenants; 
     } 
    } 
} 

而且我NinjectWebCommon是這個

[assembly: WebActivator.PreApplicationStartMethod(typeof(AIControlPoint.Data.Api.App_Start.NinjectWebCommon), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(AIControlPoint.Data.Api.App_Start.NinjectWebCommon), "Stop")] 

namespace AIControlPoint.Data.Api.App_Start 
{ 
    using System; 
    using System.Web; 

    using Microsoft.Web.Infrastructure.DynamicModuleHelper; 

    using Ninject; 
    using Ninject.Web.Common; 

    public static class NinjectWebCommon 
    { 
     private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

     /// <summary> 
     /// Starts the application 
     /// </summary> 
     public static void Start() 
     { 
      DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); 
      DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); 
      bootstrapper.Initialize(CreateKernel); 
     } 

     /// <summary> 
     /// Stops the application. 
     /// </summary> 
     public static void Stop() 
     { 
      bootstrapper.ShutDown(); 
     } 

     /// <summary> 
     /// Creates the kernel that will manage your application. 
     /// </summary> 
     /// <returns>The created kernel.</returns> 
     private static IKernel CreateKernel() 
     { 
      var kernel = new StandardKernel(); 
      kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
      kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

      System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel); 

      RegisterServices(kernel); 
      return kernel; 
     } 

     /// <summary> 
     /// Load your modules or register your services here! 
     /// </summary> 
     /// <param name="kernel">The kernel.</param> 
     private static void RegisterServices(IKernel kernel) 
     { 
      kernel.Bind<TenantConfigurationContext>().To<TenantConfigurationContext>(); 
     }   
    } 
} 

我已經安裝了的NuGet包:ninject,ninject.mvc3,ninject.web.common,ninject.webapi.dependencyresolver

有沒有人成功地得到這個工作?任何幫助將非常感激。

感謝 史蒂芬

回答

0

您直接在控制器 將被強耦合TenantConfigurationContext內部創建TenantConfigurationContext的一個實例。 如果任何TenantConfigurationContext的實現更改或發佈該組件的新版本,則必須自行更改控制器類。 依賴注入消除了緊密耦合,使應用程序變得靈活,可重用。 您正在使用Ninject作爲依賴框架,但您試圖實現它的方式有一些 問題。請參閱下面的步驟:

首先,你需要創建一個類似下面的接口和類:

public interface ITenantConfigurationContext 
{ 
    List<TenantConfiguration> TenantConfigurations(); 
} 
public class TenantConfigurationContext : ITenantConfigurationContext 
{ 
    public List<TenantConfiguration> TenantConfigurations() 
    { 
     return (get data from DB here); 
    } 
} 

然後調用ITenantConfigurationContext在網頁API控制器:

public class TenantController : ApiController 
{ 
    private ITenantConfigurationContext tenantContext; 
    public TenantController(ITenantConfigurationContext tenantContext) 
    { 
     this.tenantContext = tenantContext; 

    } 
    public List<TenantConfiguration> Get() 
    { 
     var tenants = (from t in tenantContext.TenantConfigurations 
         select t) 
         .ToList(); 

     return tenants; 
    } 
} 

最後,Ninject註冊ITenantConfigurationContext :

private static void RegisterServices(IKernel kernel) 
{ 
    kernel.Bind<ITenantConfigurationContext>().To<TenantConfigurationContext>(); 
} 

備註:

您可能還需要從NuGet安裝Ninject.Web.WebApi-RC軟件包。

+0

對不起,我接受答案有點晚,但現貨!我有錯誤的塊金包。 –