gpt4 book ai didi

c# - 来自 Mono 的 TypeLoadException

转载 作者:行者123 更新时间:2023-11-30 16:11:26 24 4
gpt4 key购买 nike

我正在尝试让 Mono 3.4.0 与某些 Microsoft OWIN 组件一起使用。我已经开始在 Windows 上进行测试,但是当我的应用程序处理 HTTP 请求时,出现以下异常:

System.TypeLoadException: Could not load type 'Microsoft.Owin.Security.AuthenticateResult' from assembly 'Microsoft.Owin, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

该应用程序正在使用 Visual Studio 作为标准 .NET 程序集进行编译,期望它应该在 Mono 运行时上运行。

我将 Microsoft.Owin NuGet 程序包添加到控制台应用程序并设法将问题重现到以下代码:

class Program
{
static void Main(string[] args)
{
var instance = new AuthenticateResult(
null,
new AuthenticationProperties(),
new AuthenticationDescription());

Console.WriteLine(instance.GetType().AssemblyQualifiedName);
}
}

在 .NET 中运行它,应用程序正常启动并打印出类型信息。如果它通过 Mono 运行,应用程序会打印出异常并失败。

此外,此代码适用于 .NET 和 Mono:

class Program
{
static void Main(string[] args)
{
var type = typeof(AuthenticateResult);
Console.WriteLine(type.AssemblyQualifiedName);
}
}

我已经用 Microsoft.Owin 程序集中的许多类型进行了测试,结果相似。如果我在需要类型之前使用 typeof 运算符加载类型,Mono 不会引发异常,而是抛出异常以尝试加载不同类型(可能只是代码所需的下一个类型)。

为什么我在 Mono 中运行时会出现这种奇怪的行为?是否有一些我必须在命令行上指定的内容才能加载程序集,正如我期望从 .NET CLR 中加载的那样?我能做些什么来获取有关类型加载失败原因的更多信息吗?

最佳答案

看起来类型加载错误是由您在代码中引用的类型所使用的类型之一引起的,而不是您实际使用的类型。

如果您在运行程序之前运行 set MONO_LOG_LEVEL=debug,您将在输出中看到以下内容:

Mono: The class System.Security.Claims.ClaimsIdentity could not be loaded, used in mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

对照 mono status page 进行检查我们可以看到在 Mono 中还没有实现任何 System.Security.Claims 命名空间。

关于c# - 来自 Mono 的 TypeLoadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24375532/

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