1

I have a C# class library. Framework .NET 4.5.2. I've installed these NuGet packages:

<?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>

Here is my code:

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

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

Here's the exception I am getting when I run the test through Test Explorer:

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'.

If I do the same thing with a console app, I don't get the exception. If I comment out the first or the second line I don't get the exception; both lines are needed.

So this exception seems to require NUnit, AWSSDK and log4net.

I've tried googling, but none of the suggestions worked.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97
  • 2
    Why are you messing with the logical call context? – Aluan Haddad Apr 10 '18 at 23:35
  • @AluanHaddad: I have a multitenant web app I'm working on. There's one Appender for lots of clients. I'd like to include the client id for that client's log messages (which I'll set per request). I thought `LogicalThreadContext` was the way to do that. – Jesus is Lord Apr 11 '18 at 00:51

1 Answers1

2

To solve the issue I added CallContext.FreeNamedDataSlot("log4net.Util.LogicalThreadContextProperties"); per this answer.

This test passes:

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");
    }
}
Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97