gpt4 book ai didi

c# - 'Nancy.Bootstrapper.AppDomainAssemblyTypeScanner' 的类型初始值设定项抛出异常

转载 作者:行者123 更新时间:2023-11-30 20:48:07 25 4
gpt4 key购买 nike

我在对 Nancy 项目执行单元测试时遇到异常。应用程序按预期运行,我只在运行单元测试时遇到问题。

我使用的是 Nancy 0.23.2.0。我也在单元测试项目中使用 Nancy.Authentication.Basic 0.23.2.0、Nancy.Bootstrappers.Unity 0.23.2.0 和 Nancy.Testing 0.23.2.0。

我得到了这个版本的 Nancy 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"];
}
}

应用程序 Bootstrapper 如下所示:

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()

最佳答案

类型 ApplicationRegistrationscommit 21c2f00 中被重命名为 Registrations (v0.23) 因为它被更改为除了应用程序范围的注册之外还包括请求范围的注册。

听起来好像某个地方有一个陈旧版本的程序集,或者是一个旧的 NuGet 依赖项版本。尝试清理您的 bin/obj 文件夹并确保更新所有 NuGet 依赖项。

关于c# - 'Nancy.Bootstrapper.AppDomainAssemblyTypeScanner' 的类型初始值设定项抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25080544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com