I know there are many posts very similar to this one. However, I have been through them and have not been able to find a solution to my problem.
I am implementing OAth authentication using OWIN into a previous project which utilises Ninject for dependency injection.
To implement OATH using OWIN switched from the original Global.asax Application_Start configuration method to creating a Start.cs file/class which is used to configure OWIN.
The problem I am facing is that no matter what implementation I try upon running the code, an API request always returns the error "Make sure that the controller has a parameterless public constructor".
Please can some one shed some light on why neither of the below approaches work?
My first attempt at resolving the issue was to use app.UseNinjectMiddleware with app.UseNinjectWebApi. Secondly I tried a NinjectDependencyResolver which has been mentioned on many previous posts. Link to similar post.
Below is the code contining both implementations.
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();
}
}
Here is my NinjectWebCommon class
//[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 )
{
}
}
}
Thanks in advanced