Ninject错误:Sequence不包含任何元素
本文关键字:元素 何元素 包含任 错误 Sequence Ninject | 更新日期: 2023-09-27 18:11:49
当我在Azure服务器上发布时出现此错误:
序列不包含元素
我不确定这个错误是因为Ninject。我尝试了所有类似错误的提示,但似乎这是一个通用错误。
我试图在几台机器上运行相同的代码,这个错误不会只发生在公共Azure上。如果你经历了什么,可以帮助,非常感谢。
堆栈跟踪:
[InvalidOperationException: Sequence contains no elements]
System.Linq.Enumerable.Single(IEnumerable`1 source) +311
Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin.Start() +44
Ninject.Web.Common.Bootstrapper.<Initialize>b__0(INinjectHttpApplicationPlugin c) +8
Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable`1 series, Action`1 action) +130
Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) +140
Splora.SploraV1.UI.Web.NinjectWebCommon.Start() +102
[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.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +192
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +136
WebActivatorEx.BaseActivationMethodAttribute.InvokeMethod() +73
WebActivatorEx.ActivationManager.RunActivationMethods(Boolean designerMode) +350
WebActivatorEx.ActivationManager.Run() +78
[InvalidOperationException: The pre-application start initialization method Run on type WebActivatorEx.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) +613
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +141
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +159
System.Web.Compilation.BuildManager.ExecutePreAppStart() +157
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +656
[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivatorEx.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) +4531288
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +94
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +191
NinjectWebCommon.cs:
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 = RegisterServices();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
private static StandardKernel RegisterServices()
{
return new Container().GetModules();
}
我发现了这个问题,在Azure上,当我发布时,我没有在发布屏幕的设置选项卡上选择"在目的地删除其他文件"选项。
因此,发布没有删除我不再使用的旧dll。我清除了Azure上的所有文件并再次发布,它成功了。
之后,我在发布时选择了"Remove additional files at destination"选项,这样的事情就不会再发生了
在我的情况下,这个问题发生在UAT,而不是在我的机器上。
这样做的原因是我改变了命名空间,结果在发布文件夹中有两个类相同的库。
我不得不清理发布文件夹并重新发布。现在一切顺利