2

我正在構建一個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); 

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

那麼,有沒有人知道什麼是錯的,我該如何解決這個問題?

回答

3

我與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

相關問題