Nancy.Bootstrapper.AppDomainAssemblyTypeScanner'抛出异常
本文关键字:抛出异常 Bootstrapper AppDomainAssemblyTypeScanner Nancy | 更新日期: 2023-09-27 18:16:10
当我在Nancy项目上执行单元测试时,我得到了一个异常。应用程序按预期运行,只有在运行单元测试时才出现问题。
我使用Nancy 0.23.2.0。我也使用Nancy. authentication . basic 0.23.2.0, Nancy. bootstrapers . unity 0.23.2.0和Nancy。在单元测试项目上测试0.23.2.0。
我得到了这个版本的南希src,可以跟踪问题到AppDomainAssemblyTypeScanner.UpdateTypes(),特别是linq表达式。
public static void UpdateTypes()
{
UpdateAssemblies();
types = (from assembly in assemblies
from type in assembly.SafeGetExportedTypes()
where !type.IsAbstract
select type).ToArray();
}
我得到异常的测试代码是这样的:
[TestFixture]
public class IndexModuleTests
{
private Browser _browser;
[SetUp]
public void SetUp()
{
var bootstrapper = new ConfigurableBootstrapper(bootstrapperConfigurator =>
{
bootstrapperConfigurator.RootPathProvider(new TestRootPathProvider());
bootstrapperConfigurator.Module<IndexModule>();
bootstrapperConfigurator.RequestStartup((container, pipelines, context) =>
{
context.CurrentUser = new UserIdentity {UserName = "demo"};
});
});
_browser = new Browser(bootstrapper);
}
[Test]
public void ShouldAllowAuthenticatedUsersToBrowseSecuredPage()
{
var result = _browser.Get("/index", with => with.HttpRequest());
result.StatusCode.Should().Be(HttpStatusCode.OK);
}
}
我要测试的模块:
public class IndexModule : NancyModule
{
public IndexModule()
{
this.RequiresAuthentication();
Get["/"] = parameters => View["index"];
}
}
应用程序引导程序看起来像这样:
public class Bootstrapper : UnityNancyBootstrapper
{
protected override void ConfigureApplicationContainer(IUnityContainer contrainer)
{
contrainer.RegisterType<IUserValidator, UserValidator>();
}
protected override void ApplicationStartup(IUnityContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
pipelines.EnableBasicAuthentication(new BasicAuthenticationConfiguration(
container.Resolve<IUserValidator>(),
"MyAppDomain"));
}
protected override void ConfigureConventions(NancyConventions nancyConventions)
{
nancyConventions
.StaticContentsConventions
.Add(StaticContentConventionBuilder.AddDirectory("js", @"js"));
nancyConventions
.StaticContentsConventions
.Add(StaticContentConventionBuilder.AddDirectory("css", @"css"));
nancyConventions
.StaticContentsConventions
.Add(StaticContentConventionBuilder.AddDirectory("img", @"img"));
nancyConventions
.StaticContentsConventions
.Add(StaticContentConventionBuilder.AddDirectory("fonts", @"fonts"));
base.ConfigureConventions(nancyConventions);
}
}
例外:
SetUp : System.TypeInitializationException : The type initializer for 'Nancy.Bootstrapper.AppDomainAssemblyTypeScanner' threw an exception.
----> System.TypeLoadException : Could not load type 'Nancy.Bootstrapper.ApplicationRegistrations' from assembly 'Nancy, Version=0.23.2.0, Culture=neutral, PublicKeyToken=null'.
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner.TypesOf(ScanMode mode)
at Nancy.Conventions.NancyConventions.BuildDefaultConventions()
at Nancy.Conventions.NancyConventions..ctor()
at Nancy.Bootstrapper.NancyBootstrapperBase`1..ctor()
at Nancy.Bootstrapper.NancyBootstrapperWithRequestContainerBase`1..ctor()
at Nancy.Testing.ConfigurableBootstrapper..ctor(Action`1 configuration)
at Pandora.Web.Backoffice.Lite.Tests.IndexModuleTests.SetUp() in IndexModuleTests.cs: line 17
--TypeLoadException
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at Nancy.Extensions.AssemblyExtensions.SafeGetExportedTypes(Assembly assembly)
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner.<UpdateTypes>b__16(Assembly assembly)
at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray(IEnumerable`1 source)
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner.UpdateTypes()
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner.LoadAssembliesWithNancyReferences()
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner..cctor()
类型ApplicationRegistrations
在commit 21c2f00
(v0.23)中被重命名为Registrations
,因为它被更改为除了应用程序范围的注册之外还包括请求范围的注册。
听起来你有一个过时的版本的程序集躺在某处,或旧的NuGet依赖版本。尝试清理bin/obj文件夹,并确保所有NuGet依赖项都已更新。