Baggage.Current accross AppDomains. (#1435)

On net452 Baggage.Current is stored in the CallContext which uses a
workaround to avoid marshalling Baggage instance accross AppDomains.
In a diffent AppDomain the private field value is null so we need to return
default Baggage in that case.
This commit is contained in:
mbakalov 2020-11-02 12:32:45 -05:00 committed by GitHub
parent 4ad73e4073
commit 772ed953ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 1 deletions

View File

@ -61,7 +61,13 @@ namespace OpenTelemetry.Context
return default(T);
}
return (T)WrapperField.GetValue(wrapper);
var value = WrapperField.GetValue(wrapper);
if (value is T)
{
return (T)value;
}
return default(T);
}
/// <inheritdoc/>

View File

@ -72,9 +72,67 @@ namespace OpenTelemetry.Context.Tests
Assert.Equal(100, expectedSlot.Get());
}
#if NETFRAMEWORK
[Fact]
public void NetFrameworkGetSlotInAnotherAppDomain()
{
const string slotName = "testSlot";
var slot = RuntimeContext.RegisterSlot<int>(slotName);
slot.Set(100);
// Create an object in another AppDomain and try to access the slot
// value from it.
var domainSetup = AppDomain.CurrentDomain.SetupInformation;
var ad = AppDomain.CreateDomain("other-domain", null, domainSetup);
var remoteObjectTypeName = typeof(RemoteObject).FullName;
Assert.NotNull(remoteObjectTypeName);
var obj = (RemoteObject)ad.CreateInstanceAndUnwrap(
typeof(RemoteObject).Assembly.FullName,
remoteObjectTypeName);
// Value we previously put into the slot ("100") would not be propagated
// across AppDomains. We should get default(int) = 0 back.
Assert.Equal(0, obj.GetValueFromContextSlot(slotName));
}
#endif
public void Dispose()
{
RuntimeContext.Clear();
}
#if NETFRAMEWORK
private class RemoteObject : ContextBoundObject
{
public int GetValueFromContextSlot(string slotName)
{
// Slot is not propagated across AppDomains, attempting to get
// an existing slot here should throw an ArgumentException.
try
{
RuntimeContext.GetSlot<int>(slotName);
throw new Exception("Should not have found an existing slot: " + slotName);
}
catch (ArgumentException)
{
// This is ok.
}
// Now re-register this slot.
RuntimeContext.RegisterSlot<int>(slotName);
var slot = RuntimeContext.GetSlot<int>(slotName);
// We didn't put any value into this slot, so default(int) should be returned.
// Previously an exception was thrown at this point.
return slot.Get();
}
}
#endif
}
}