2

我正在尝试让 ninject 在生产环境中工作。

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

  • 数据
  • 模型
  • WebAPI2
  • MVC5

一切都被部署为 webrole 到 azure。

我的 api 设置为 mvc 站点下方的虚拟应用程序。我的应用程序是一个多租户应用程序,因此我希望用户能够以与应用程序相同的方式访问 api,例如

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

对于我的本地开发,我使用的是 2 个站点而不是虚拟应用程序,因为我在尝试与 azure 进行斗争以使其在本地工作时遇到了许多问题。所以我的服务定义为本地工作创建了 2 个站点。在本地我没有问题

我的网站和api都引用了ninject,我的数据和模型没有。

当我部署并尝试访问 api 时出现错误

静态容器已经有一个与之关联的内核!

该网站没有任何问题,它似乎只是api。我使用 nuget 添加了 ninject

堆栈跟踪

[NotSupportedException: 静态容器已经有一个与之关联的内核!] Ninject.Web.KernelContainer.set_Kernel(IKernel value) +193 Ninject.Web.NinjectWebHttpApplicationPlugin.Start() +82 Ninject.Web.Common.Bootstrapper.b__0(INinjectHttpApplicationPlugin c) +89 Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable 1 series, Action1 action) +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 站点作为根站点。这对问题没有帮助。

我的上述设置看起来有问题吗?

4

2 回答 2

0

我相信你的问题和我的一样。问题是您在生产环境中有重复的 Ninject.dll 或来自 Ninject 的任何 dll。在再次部署之前,您必须清理所有现有文件。在这里查看我的解决方案:

静态容器已经有一个与之关联的内核

于 2014-12-27T01:23:06.557 回答
0

这发生在我身上,我只是评论了这一行:bootstrapper.Initialize(CreateKernel)问题结束了。

于 2021-05-03T20:09:05.393 回答