Ninject.Web.WebApi.OwinHost v3.2.x的Ninject依赖项

时间:2014-04-12 16:46:33

标签: asp.net-web-api ninject azure-web-roles owin

我试图在Azure辅助角色中隐藏ApiControllers。我之前在同一解决方案中的asp.net mvc 5 / web api 2项目中使用相同的控制器完成了注射。此WorkerRole项目中的另一个非注入测试控制器工作并提供预期结果。

在尝试添加NuGet包Ninject.Web.WebApi.OwinHost(版本3.2.x)时,看起来该包是错误的?要求Ninject版本≥3.0.0.0&& < 3.1.0.0

我已经尝试了

Install-Package Ninject.Web.WebApi.OwinHost -ProjectName WorkerRole1
Install-Package Ninject.Web.WebApi.OwinHost -Pre -ProjectName WorkerRole1
Install-Package Ninject.Web.WebApi.OwinHost -Version 3.2.0 -ProjectName WorkerRole1
Install-Package Ninject.Web.WebApi.OwinHost -Version 3.2.1-unstable-002 -ProjectName WorkerRole1

我能够成功添加Ninject.Web.WebApi.SelfHost,但我相信我需要Owin的味道,并且SelfHost似乎没有安装NinjectWebCommon.cs或同等版本。一旦我成功地将OwinHost包括在内,我就不知道在工作者角色中将ninject连接到owin self host的位置。我假设它将被支架化。

尝试安装Ninject.Web.WebApi.OwinHost

的结果
PM> Install-Package Ninject.Web.WebApi.OwinHost -Pre -ProjectName WorkerRole1
Attempting to resolve dependency 'Ninject.Web.WebApi (≥ 3.2.0-unstable)'.
Attempting to resolve dependency 'Ninject (≥ 3.2.0.0 && < 3.3.0.0)'.
Attempting to resolve dependency 'Ninject.Web.Common (≥ 3.2.0.0 && < 3.3.0.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi (≥ 5.0 && < 6.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.WebHost (≥ 5.1.2 && < 5.2.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Core (≥ 5.1.2 && < 5.2.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Client (≥ 5.1.2)'.
Attempting to resolve dependency 'Newtonsoft.Json (≥ 4.5.11)'.
Attempting to resolve dependency 'Ninject.Web.Common.OwinHost (≥ 3.0 && < 4.0)'.
Attempting to resolve dependency 'Ninject.Extensions.ContextPreservation (≥ 3.0 && < 4.0)'.
Attempting to resolve dependency 'Ninject (≥ 3.0.0.0 && < 3.1.0.0)'.
Install-Package : Updating 'Ninject 3.2.2.0' to 'Ninject 3.0.1.10' failed. Unable to find versions of 'Ninject.Web.WebApi, Ninject.Web.Common' that are compatible with 'Ninject 3.0.1.10'.
At line:1 char:1
+ Install-Package Ninject.Web.WebApi.OwinHost -Pre -ProjectName WorkerRole1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

PM> 

然而,在检查WorkerRole中已安装软件包的版本时可能无关,我注意到许多软件包是特定于IIS的。 Azure WorkerRole Owin自托管主机需要这些吗?特别是对于Microsoft.AspNet.WebApi.SelfHost我看到以下表示法,但我确实安装了Microsoft.AspNet.WebApi.OwinSelfHost。

  

这是一个用于托管ASP.NET Web API的旧版程序包   进程(在IIS之外)。请使用   用于新项目的Microsoft.AspNet.WebApi.OwinSelfHost包。

WorkerRole中已安装的软件包

PM> Get-Package -ProjectName WorkerRole1

Id                             Version              Description/Release Notes                                                                                                                                                               
--                             -------              -------------------------                                                                                                                                                               
EntityFramework                6.1.0                Entity Framework is Microsoft's recommended data access technology for new applications.                                                                                                
Microsoft.AspNet.Identity.Core 2.0.0                Core interfaces for ASP.NET Identity.                                                                                                                                                   
Microsoft.AspNet.Identity.E... 2.0.0                ASP.NET Identity providers that use Entity Framework.                                                                                                                                   
Microsoft.AspNet.WebApi        5.0.0                This package contains everything you need to host ASP.NET Web API on IIS. ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of client...
Microsoft.AspNet.WebApi.Client 5.1.2                This package adds support for formatting and content negotiation to System.Net.Http. It includes support for JSON, XML, and form URL encoded data.                                      
Microsoft.AspNet.WebApi.Core   5.1.2                This package contains the core runtime assemblies for ASP.NET Web API. This package is used by hosts of the ASP.NET Web API runtime. To host a Web API in IIS use the Microsoft.AspNe...
Microsoft.AspNet.WebApi.Owin   5.1.2                This package allows you to host ASP.NET Web API within an OWIN server and provides access to additional OWIN features.                                                                  
Microsoft.AspNet.WebApi.Owi... 5.1.2                This package allows you to host ASP.NET Web API within your own process using the OWIN HttpListener server. ASP.NET Web API is a framework that makes it easy to build HTTP services ...
Microsoft.AspNet.WebApi.Sel... 5.1.2                This is a legacy package for hosting ASP.NET Web API within your own process (outside of IIS). Please use the Microsoft.AspNet.WebApi.OwinSelfHost package for new projects.            
Microsoft.AspNet.WebApi.Web... 5.0.0                This package contains everything you need to host ASP.NET Web API on IIS. ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of client...
Microsoft.Data.Edm             5.6.1                Classes to represent, construct, parse, serialize and validate entity data models. Targets .NET 4.0, Silverlight 4.0, or .NET Portable Lib with support for .NET 4.0, SL 4.0, Win Pho...
Microsoft.Data.OData           5.6.1                Classes to serialize, deserialize and validate OData payloads. Enables construction of OData producers and consumers. Targets .NET 4.0, Silverlight 4.0 or .NET Portable Lib with sup...
Microsoft.Data.Services.Client 5.6.1                LINQ-enabled client API for issuing OData queries and consuming OData payloads. Supports OData v3. Targets .NET 4.0, Silverlight 4.0 or .NET Portable Lib with support for .NET 4.5, ...
Microsoft.Owin                 2.1.0                Provides a set of helper types and abstractions for simplifying the creation of OWIN components.                                                                                        
Microsoft.Owin.Diagnostics     2.1.0                Provides middleware components to assist in developing OWIN-based applications.                                                                                                         
Microsoft.Owin.Host.HttpLis... 2.1.0                OWIN server built on the .NET Framework's HttpListener class. Currently the default server used for self-hosting.                                                                       
Microsoft.Owin.Hosting         2.1.0                Provides default infrastructure types for hosting and running OWIN-based applications.                                                                                                  
Microsoft.Owin.SelfHost        2.1.0                Includes components needed to host an OWIN-based application in a custom process.                                                                                                       
Microsoft.WindowsAzure.Conf... 2.0.3                Windows Azure Configuration Manager provides a unified API to load configuration settings regardless of where the application is hosted - whether on-premises or in a Cloud Service.    
Newtonsoft.Json                6.0.2                Json.NET is a popular high-performance JSON framework for .NET                                                                                                                          
Ninject                        3.2.2.0              Stop writing monolithic applications that make you feel like you have to move mountains to make the simplest of changes. Ninject helps you use the technique of dependency injection ...
Ninject.Web.Common             3.2.0.0              Bootstrapper for web projects                                                                                                                                                           
Ninject.Web.Common.Selfhost    3.2.0.0              Adds self hosting support to web common.                                                                                                                                                
Ninject.Web.WebApi             3.2.0.0              Extension for Ninject providing integration with ASP.NET MVC WebAPI 2                                                                                                                   
Ninject.Web.WebApi.Selfhost    3.2.0.0              Extension for Ninject providing integration with ASP.NET MVC WebAPI 2 Selfhost                                                                                                          
Owin                           1.0                  OWIN IAppBuilder startup interface                                                                                                                                                      
System.Spatial                 5.6.1                Contains classes and methods that facilitate geography and geometry spatial operations. Targets .NET 4.0, Silverlight 4.0 or .NET Portable Lib with support for .NET 4.0, SL 4.0, Win...
WindowsAzure.Storage           3.1.0.1              This client library enables working with the Windows Azure storage services which include the blob service for storing binary and text data, the table service for storing structured...

PM> 

2 个答案:

答案 0 :(得分:8)

我知道这已经很晚了,但我遇到了同样的问题,并通过Google搜索找到了这个问题。我最终用-DependencyVersion

解决了这个问题
Install-Package Ninject.Web.WebApi.OwinHost -DependencyVersion Highest

如果您不想更新所有依赖的NuGet包,我发现为Ninject.Web.WebApi.OwinHost手动安装所有与Ninject相关的依赖项,然后安装Ninject.Web.WebApi.OwinHost也得到了围绕这个问题。

答案 1 :(得分:0)

我今天遇到了同样的问题,我通过以下方式解决了这个问题 将以下类实现添加到项目中

    public class NinjectDependencyScope : IDependencyScope
        {
            IResolutionRoot resolver;

            public NinjectDependencyScope(IResolutionRoot resolver)
            {
                this.resolver = resolver;
            }

            public object GetService(Type serviceType)
            {
                if (resolver == null)
                    throw new ObjectDisposedException("this", "This scope has been disposed");

                return resolver.TryGet(serviceType);
            }

            public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
            {
                if (resolver == null)
                    throw new ObjectDisposedException("this", "This scope has been disposed");

                return resolver.GetAll(serviceType);
            }

            public void Dispose()
            {
                IDisposable disposable = resolver as IDisposable;
                if (disposable != null)
                    disposable.Dispose();

                resolver = null;
            }
        }

    // This class is the resolver, but it is also the global scope
    // so we derive from NinjectScope.

public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
    IKernel kernel;

    public NinjectDependencyResolver(IKernel kernel)
        : base(kernel)
    {
        this.kernel = kernel;
    }

    public IDependencyScope BeginScope()
    {
        return new NinjectDependencyScope(kernel.BeginBlock());
    }
}

2-然后在NinjectWebCommon

将以下代码行添加到此方法CreateKernel(): GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); 所以完整的实施将是:

private static IKernel CreateKernel()
        {
           var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
                // Install our Ninject-based IDependencyResolver into the Web API config
                GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

我自己尝试了这个代码并且运行成功