2015-12-14 81 views
0

我知道有很多帖子與此相似。但是,我經歷了他們,一直無法找到解決我的問題的方法。Asp.Net API 2 Ninject和OWIN OAuth配置

我正在使用OWIN實現OAth身份驗證到以前的項目中,該項目利用Ninject進行依賴注入。

使用OWIN從原始的Global.asax Application_Start配置方法切換到創建用於配置OWIN的Start.cs文件/類來實現OATH。

我面臨的問題是無論我在運行代碼時嘗試什麼實現,API請求總是返回錯誤「確保控制器具有無參數公共構造函數」。

請有人可以點亮一下爲什麼以下方法都不起作用?

我第一次嘗試解決該問題是使用app.UseNinjectWebApi與app.UseNinjectMiddleware。其次,我嘗試了一個NinjectDependencyResolver,在之前的許多帖子中都提到過。 Link to similar post

下面是兩個實現的代碼。

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     var config = new HttpConfiguration(); 
     var kernel = NinjectWebCommon.CreateKernel(); 

     // Using this and UseNinjectWebAPI 
     app.UseNinjectMiddleware(() => kernel); 

     ConfigureOAuthTokenGeneration(app); 
     ConfigureWebApi(config); 

     app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 

     WebApiConfig.Register(config); 
     app.UseNinjectWebApi(config); 

     // Also tried this (when the following lines are uncommented UseNinjectMiddleware and useNinjectWebApi is commented) 
     // This causes "Make sure that the controller has a parameterless public constructor" error 
     //GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); 
     //app.UseWebApi(config); 
    } 

    private void ConfigureOAuthTokenGeneration(IAppBuilder app) 
    { 
     // Configure the db context and user manager to use a single instance per request 
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 

     // Plugin the OAuth bearer JSON Web Token tokens generation and Consumption will be here 

    } 

    private void ConfigureWebApi(HttpConfiguration config) 
    { 
     var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First(); 
     jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
    } 
} 

這是我NinjectWebCommon類先進

回答

0

我終於解決了這個問題,解決辦法是使用與Ninject.Web.Common來到原NinjectWebCommon類

//[assembly:  WebActivatorEx.PreApplicationStartMethod(typeof(TimeUp.Web.Api.App_Start.NinjectWebCommon), "Start")] 
//[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(TimeUp.Web.Api.App_Start.NinjectWebCommon), "Stop")] 

namespace Shopping 
{ 
    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> 
     public static IKernel CreateKernel() 
     { 
      var kernel = new StandardKernel(); 
      try 
      { 
       kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
       kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

       RegisterServices(kernel); 

       return kernel; 
      } 
      catch 
      { 
       kernel.Dispose(); 
       throw; 
      } 
     } 

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

謝謝圖書館。這個類的start方法在Startup.Configuration之前觸發,因此可以通過引導程序使用此類Dependency Resolver來使用它。這是我的工作配置。

public void Configuration(IAppBuilder app) 
    { 
     var config = new HttpConfiguration(); 

     config.DependencyResolver = new NinjectResolver(new Ninject.Web.Common.Bootstrapper().Kernel); 
     WebApiConfig.Register(config); 

     app.UseWebApi(config); 
    }