2

我试图让ninject在生产环境中工作。静态容器已经有一个与它关联的内核!部署到虚拟应用程序时

我的解决方案包括以下项目

  • 数据
  • 型号
  • WebApi2
  • MVC5

一切都得到部署webrole天青的。

我的api被设置为mvc站点下的虚拟应用程序。我的应用程序是一个多租户应用程序,所以我希望用户能够像应用程序一样访问api。

https://theirbusiness.mydomain.com/api/api-call

对于我的地方发展我使用2点还不如我只好许多问题试图与蔚蓝的战斗得到它在本地工作的虚拟应用程序。所以我的服务定义为本地工作创建了2个网站。本地我没有问题

我的网站和api都提到ninject,我的数据和模型都没有。

当我部署,并尝试打API我得到

静态容器已有与之关联的内核错误!

该网站没有任何问题,它似乎是api。我添加ninject双方使用的NuGet

堆栈跟踪

[NotSupportedException异常:静态容器已有与之关联的内核] Ninject.Web.KernelContainer.set_Kernel(的iKernel值)193 Ninject.Web.NinjectWebHttpApplicationPlugin.Start()82 Ninject.Web.Common.Bootstrapper.b__0(INinjectHttpApplicationPlugin c)中89 Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable的1 series, Action 1动作)283 Ninject.Web。 Common.Bootstrapper.Initialize(Func`1 createKernelCallback)+410 MyNameSpace.Application.Api.App_Start.NinjectWebCommon.Start()362

[TargetInvocationException: Exception has been thrown by the target of an invocation.] 
    System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +417 
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35 
WebActivator.BaseActivationMethodAttribute.InvokeMethod() +761 
WebActivator.ActivationManager.RunActivationMethods() +1177 
WebActivator.ActivationManager.RunPreStartMethods() +75 
WebActivator.ActivationManager.Run() +97 

[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..] 
System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +888 
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +137 
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +160 
System.Web.Compilation.BuildManager.ExecutePreAppStart() +142 
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +838 

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..] 
    System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +452 
    System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +99 
    System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +1017 

我NinjectWebCommon.cs

using System; 
using System.Web; 
using Microsoft.Web.Infrastructure.DynamicModuleHelper; 
using Ninject; 
using Ninject.Web.Common; 
using MyNameSpace.Application.Api.App_Start; 
using MyNameSpace.Application.Api.Interface; 
using MyNameSpace.Application.Api.Repository; 

[assembly: WebActivator.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")] 


namespace MyNameSpace.Application.Api.App_Start 
{ 
public static class NinjectWebCommon 
{ 
    private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

    /// <summary> 
    /// Starts the application 
    /// </summary> 
    public static void Start() 
    { 
     DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); 
     DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); 
     bootstrapper.Initialize(CreateKernel); 
    } 

    /// <summary> 
    /// Stops the application. 
    /// </summary> 
    public static void Stop() 
    { 
     bootstrapper.ShutDown(); 
    } 

    /// <summary> 
    /// Creates the kernel that will manage your application. 
    /// </summary> 
    /// <returns>The created kernel.</returns> 
    private static IKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
     kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

     RegisterServices(kernel); 
     GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); 
     return kernel; 
    } 

    /// <summary> 
    /// Load your modules or register your services here! 
    /// </summary> 
    /// <param name="kernel">The kernel.</param> 
    private static void RegisterServices(IKernel kernel) 
    { 
        kernel.Bind<IBusinessRepository>().ToConstant(new BusinessRepository()); 
        kernel.Bind<IEmployeeRepository>().ToConstant(new EmployeeRepository()); 

    } 
} 

}

我的相关性范围

public class NinjectDependencyScope : IDependencyScope 
{ 
private IResolutionRoot resolver; 

internal NinjectDependencyScope(IResolutionRoot resolver) 
{ 
    Contract.Assert(resolver != null); 

    this.resolver = resolver; 
} 

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

    resolver = null; 
} 

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

    return resolver.TryGet(serviceType); 
} 

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

    return resolver.GetAll(serviceType); 
} 
} 

public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver 
{ 
private IKernel kernel; 

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

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

如果我在本地调试。将api设置为我的启动项目我可以在部署它时立即运行该应用程序,而不会出现任何问题,它会失败。我远程登录到azure webrole并删除了mvc网站,只保留api网站作为根网站。这并没有帮助这个问题。

在我的上述设置中看起来有什么不对吗?

回答

相关问题