Ninject导致NotImplementedException在HttpContextBase.get_Response()

问题描述:

我正在构建一个ASP.NET Web API 2项目。我想使用依赖注入,所以我安装了Ninject库。Ninject导致NotImplementedException在HttpContextBase.get_Response()

起初,我试图通过使用NuGet包管理器来安装它,但我在安装时有以下错误:

Install failed. Rolling back... 
Updating 'Microsoft.Owin 3.0.0' to 'Microsoft.Owin 2.0.0' failed. 
Unable to find versions of 'Microsoft.Owin.Security, 
Microsoft.Owin.Security.Cookies, Microsoft.Owin.Security.OAuth, 
Microsoft.AspNet.WebApi.Owin, Microsoft.Owin.Host.SystemWeb, 
Microsoft.Owin.Security.Facebook, Microsoft.Owin.Security.Google, 
Microsoft.Owin.Security.MicrosoftAccount, Microsoft.Owin.Security.Twitter' 
that are compatible with 'Microsoft.Owin 2.0.0'. 

我的话,跟着this post该指令建议中运行以下命令NuGet程序包管理器控制台:

Install-Package Ninject.Web.WebApi.OwinHost -Version 3.2.1 -DependencyVersion Highest 

输入此命令后,程序包安装成功。

这是Ninject的配置,我按照this Ninject GitHub post中的说明设置。

public void Configuration(IAppBuilder app) 
    { 
     ConfigureAuth(app); 

     app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(GlobalConfiguration.Configuration); 
    } 

    private static StandardKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     kernel.Load(Assembly.GetExecutingAssembly()); 
     return kernel; 
    } 

这是问题出现的地方。我想在我的一些控制器中使用CreatedAtRoute()方法。

return CreatedAtRoute("GetMyResourceByIdRoute", 
      new { id = myResource.Id }, 
      myResource); 

当我做的方法的调用,使用CreatedAtRoute()我得到以下异常:

System.NotImplementedException 

Stack trace: at System.Web.HttpContextBase.get_Response()\r\n 
at System.Web.UI.Util.GetUrlWithApplicationPath(HttpContextBase context, 
String url)\r\n at System.Web.Routing.RouteCollection.NormalizeVirtualPath 
(RequestContext requestContext, String virtualPath)\r\n at 
System.Web.Routing.RouteCollection.GetVirtualPath(RequestContext 
requestContext, String name, RouteValueDictionary values)\r\n at 
System.Web.Http.WebHost.Routing.HostedHttpRouteCollection.GetVirtualPath 
(HttpRequestMessage request, String name, IDictionary`2 values)\r\n at 
System.Web.Http.Routing.UrlHelper.GetVirtualPath(HttpRequestMessage request, String routeName, IDictionary`2 routeValues)\r\n at 
System.Web.Http.Routing.UrlHelper.Route(String routeName, IDictionary`2 routeValues)\r\n at 
System.Web.Http.Routing.UrlHelper.Link(String routeName, IDictionary`2 routeValues)\r\n at 
System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult`1.Execute()\r\n at 
System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult`1.ExecuteAsync(CancellationToken cancellationToken)\r\n at 
System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at 
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at 
System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n 
--- End of stack trace from previous location where exception was thrown 
---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at 
System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at 
System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext() 

我得出的结论是有一些问题,我的Ninject配置只是因为当我注释掉

app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(GlobalConfiguration.Configuration); 

从我的配置方法,一切工作正常。

那么,有没有人知道什么是错的,我该如何解决这个问题?

我与NotImplementedException试图撰写与this.Url.Link("DefaultApi", new { controller="controller_name", id = "some_id" })

原来的Web API配置不正确的链接时,偶然发现了同样的问题。关键是要使用完全不同的HttpConfiguration,而不是全球性的:

// initialize web pages & mvc routing first 
    AreaRegistration.RegisterAllAreas(); 
    ConfigureRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 

    // set up IOC and configure authentication 
    app.UseNinjectMiddleware(CreateKernel); 
    ConfigureAuth(app); 

    // finally the web api, on its own http configuration 
    var webApiConfig = new HttpConfiguration(); 
    webApiConfig.MapHttpAttributeRoutes(); 
    webApiConfig.Routes.MapHttpRoute(
    name: "DefaultApi", 
    routeTemplate:api/{controller}/{id}", 
    defaults: new { id = RouteParameter.Optional }); 
    app.UseNinjectWebApi(webApiConfig); 

    // ensure all the configs are ready 
    webApiConfig.EnsureInitialized(); 
    GlobalConfiguration.Configuration.EnsureInitialized(); 

确保你得到了最新OWIN软件包,包括Ninject.Web.Common.OwinHostNinject.Web.WebApi.OwinHost

+0

呵呵,它现在有效。谢谢你,兄弟! – Yulian 2015-08-21 16:27:02

+0

这个答案对我也有帮助。非常感谢你。你能否解释为什么这个解决方案能够正常工作,以及为什么需要这种配置改变。 – Isadora 2016-07-26 17:36:37