我正试图让ninject在生产环境中工作。
我的解决方案包含以下项目
所有东西都被部署为天蓝色的webrole。
我的api被设置为mvc网站下面的虚拟应用程序。我的应用程序是一个多租户应用程序,因此我希望用户能够以与应用程序相同的方式访问api。
对于我的本地开发,我使用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, 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站点作为根站点。这对这个问题没有帮助。
在我的上述设置中出现了什么问题?
答案 0 :(得分:0)
我相信你的问题跟我的一样。问题是您在生产环境中有Ninject.dll或Ninject中的任何dll。在重新部署之前,必须清除所有现有文件。请在此处查看我的解决方案:
The static container already has a kernel associated with it
答案 1 :(得分:0)
这发生在我身上,我只是在这一行评论:bootstrapper.Initialize(CreateKernel)
和问题结束了。