使用Ninject将Web api 2应用程序部署到Azure时出错

时间:2014-12-21 21:30:00

标签: azure deployment ninject

这是我使用ninject的Owin启动类。我的应用程序完美地运行localhost但是当我将应用程序部署到azure网站时,我收到如下错误:

{
"message": "An error has occurred.",
"exceptionMessage": "An error occurred when trying to create a controller of type   CompanyController'. Make sure that the controller has a parameterless public constructor.",
"exceptionType": "System.InvalidOperationException","stackTrace": "at    System.Web.Http.Dispatcher.DefaultHttpControllerActivator.
Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type   controllerType)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.
CreateController(HttpRequestMessage request)\r\n   at     System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
"innerException": {
    "message": "An error has occurred.",
    "exceptionMessage": "Type 'Sinerp.Api.Controllers.CompanyController' does not have a default constructor",
    "exceptionType": "System.ArgumentException",
    "stackTrace": "   at System.Linq.Expressions.Expression.New(Type type)\r\n   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
  }}

我正在寻找很长一段时间。但结果令人失望。感谢ansvers

public class Startup
{

    public static IKernel NinjectKernel { get; set; }

    /// <summary>
    /// OWIN host için gereken Configuration methodu OWIN burayı nameconventions
    /// sayesinde görebiliyor ve uygulama buradan başlıyor ;-)
    /// </summary>
    /// <param name="app">OWIN standart arayüzü olmazsa olması</param>
    public void Configuration(IAppBuilder app)
    {
        //Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());

        app.UseErrorPage();
        NinjectKernel = IoC.NinjectKernel;

        var config = new HttpConfiguration();



        ConfigureOAuth(app);

        FluentValidationModelValidatorProvider.Configure(config);

        WebApiConfig.Register(config);
        AutoMapperConfig.RegisterMappings();


//#if DEBUG
        app.UseCors(CorsOptions.AllowAll);
//#endif
        app.UseWebApi(config);
        app.UseNinjectMiddleware(() => NinjectKernel)
       .UseNinjectWebApi(config);
    }

    public void ConfigureOAuth(IAppBuilder app)
    {
        var oAuthServerOptions = new OAuthAuthorizationServerOptions()
        {

            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/auth/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
            Provider = new CustomAuthorizationServerProvider(),
            RefreshTokenProvider = new CustomRefreshTokenProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(oAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
        //app.CreatePerOwinContext<IAuthRepository>((options, context) => context.);
    }
}


public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "SinerpApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional}
            );

        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }
}

1 个答案:

答案 0 :(得分:0)

Owin配置应该是这样的。首先,我在调用UserWebApi后调用Ninject Middleware 并且像魅力一样工作

app.UseNinjectMiddleware(() => NinjectKernel)
   .UseNinjectWebApi(config);
app.UseWebApi(config);