2

ASP.NET Web API 2 プロジェクトを構築しています。依存性注入を使用したかったので、目的のために Ninject ライブラリをインストールしました。

最初、NuGet Package Manager を使用してインストールしようとしましたが、インストール時に次のエラーが発生しました。

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'.

次に、NuGet パッケージ マネージャー コンソールで次のコマンドを実行することを提案するこの投稿の指示に従いました。

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

このコマンドを入力すると、パッケージが正常にインストールされました。

これは、この Ninject GitHub 投稿の指示に従って設定した Ninject の構成です。

    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);

私の構成方法から、すべてが正常に機能します。

それで、何が間違っているのか、どうすればこれを修正できるのか、誰にもわかりませんか?

4

1 に答える 1