为什么我收到TypeInitializationException

杰瑞德

我有一个C#类库。框架.NET 4.5.2。我已经安装了以下NuGet软件包:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AWSSDK.Core" version="3.3.21.19" targetFramework="net452" />
  <package id="AWSSDK.S3" version="3.3.18" targetFramework="net452" />
  <package id="log4net" version="2.0.8" targetFramework="net452" />
  <package id="NUnit" version="3.10.1" targetFramework="net452" />
</packages>

这是我的代码:

using Amazon.S3;
using log4net;
using NUnit.Framework;

class Sandbox
{
    [Test]
    public void s()
    {
        LogicalThreadContext.Properties["a"] = "b";
        AmazonS3Client client = new AmazonS3Client("a", "b");
    }
}

通过测试资源管理器运行测试时,会遇到以下例外情况:

Result StackTrace:  
at Amazon.AWSConfigs.get_LoggingConfig()
   at Amazon.Runtime.ClientConfig..ctor()
   at Amazon.S3.AmazonS3Config..ctor()
   at Amazon.S3.AmazonS3Client..ctor(String awsAccessKeyId, String awsSecretAccessKey)
   at ICS.Logging.AwsVerboseLogs.Tests.Sandbox.s() in C:\Dev\RFSAutomation\Utils\ICS.Logging\ICS.Logging.AwsVerboseLogs.Tests\Sandbox.cs:line 18
--SerializationException
   at System.AppDomain.GetHostEvidence(Type type)
   at System.Security.Policy.AppDomainEvidenceFactory.GenerateEvidence(Type evidenceType)
   at System.Security.Policy.Evidence.GenerateHostEvidence(Type type, Boolean hostCanGenerate)
   at System.Security.Policy.Evidence.GetHostEvidenceNoLock(Type type)
   at System.Security.Policy.Evidence.RawEvidenceEnumerator.MoveNext()
   at System.Security.Policy.Evidence.EvidenceEnumerator.MoveNext()
   at System.Configuration.ClientConfigPaths.GetEvidenceInfo(AppDomain appDomain, String exePath, String& typeName)
   at System.Configuration.ClientConfigPaths.GetTypeAndHashSuffix(AppDomain appDomain, String exePath)
   at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigurationHost.RequireCompleteInit(IInternalConfigRecord record)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at Amazon.AWSConfigs.GetSection[T](String sectionName)
   at Amazon.Util.Internal.RootConfig..ctor()
   at Amazon.AWSConfigs..cctor()
Result Message: 
System.TypeInitializationException : The type initializer for 'Amazon.AWSConfigs' threw an exception.
  ----> System.Runtime.Serialization.SerializationException : Type is not resolved for member 'log4net.Util.PropertiesDictionary,log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'.

如果使用控制台应用程序执行相同的操作,则不会出现异常。如果我注释掉第一行或第二行,我不会例外。两条线都需要。

因此,此异常似乎需要NUnit,AWSSDK和log4net。

我曾尝试使用Google谷歌搜索功能,但所有建议均无效。

杰瑞德

为了解决这个问题,我说CallContext.FreeNamedDataSlot("log4net.Util.LogicalThreadContextProperties");这个答案

该测试通过:

using Amazon.S3;
using log4net;
using NUnit.Framework;
using System.Runtime.Remoting.Messaging;

class Sandbox
{
    [Test]
    public void s()
    {
        LogicalThreadContext.Properties["a"] = "b";
        CallContext.FreeNamedDataSlot("log4net.Util.LogicalThreadContextProperties");
        AmazonS3Client client = new AmazonS3Client("a", "b");
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章