You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now AppContext only exposes the following APIs:
GetData
SetSwitch
TryGetSwitch
And the fact that a corresponding SetData method is missing is a bit confusing. You currently need to set data through AppDomain.SetData, even though you can then retrieve it through AppContext, as that data flows to AppContext anyway.
This proposal is to add the "missing" SetData API to AppContext to bring it on par with AppDomain.
This would also be more intuitive to use for consumers (eg. personally I was quite puzzled at first when I saw code in https://github.com/terrafx using AppContext.GetData in a few places, but with no AppContext.SetData in sight).
EDIT: looks like the API is actually... Already there?
But it's not listed in the API docs, and I can't access it from IntelliSense.
It seems it's not present in the ref assemblies - was this just missed or was it intentional?
Proposed API
namespace System
{
public static partial class AppContext
{
+ public static void SetData(string name, object? data);
}
}
AppContext.GetData and friends were introduced in .NET Core 1.0 as a replacement for AppDomain.GetData. When AppDomain type came back as part of .NET Standard 2.0, we lost the motivation to make this complete: #18887 (comment)
AppContext.SetData would be duplicate of AppDomain.SetData. If you would like to expose it for completeness, feel free to.
AppContext.SetData would be duplicate of AppDomain.SetData. If you would like to expose it for completeness, feel free to.
Yes, I think it will be good to expose for completeness however. Many things on AppDomain aren't exposed or don't work and its largely a "legacy" type on .NET Core. Having the method exposed where users can find it and the related GetData makes sense to me and avoids users needing to understand the relationship between AppContext and AppDomain.
We should doc both for this and for SetSwitch that they're intended to be set very early in the app's startup routine. Other components of the base libraries or the application might read and cache these values, and if you call SetData / SetSwitch after this caching has taken place your changes won't be honored.
Activity
tannergooding commentedon Jun 26, 2021
CC. @jkotas, just wanting to confirm that there isn't an intentional reason behind this not being exposed.
jkotas commentedon Jun 26, 2021
AppContext.GetData and friends were introduced in .NET Core 1.0 as a replacement for AppDomain.GetData. When AppDomain type came back as part of .NET Standard 2.0, we lost the motivation to make this complete: #18887 (comment)
AppContext.SetData would be duplicate of AppDomain.SetData. If you would like to expose it for completeness, feel free to.
tannergooding commentedon Jun 26, 2021
Yes, I think it will be good to expose for completeness however. Many things on AppDomain aren't exposed or don't work and its largely a "legacy" type on .NET Core. Having the method exposed where users can find it and the related
GetDatamakes sense to me and avoids users needing to understand the relationship between AppContext and AppDomain.bartonjs commentedon Aug 17, 2021
Video
Looks good as proposed, for symmetry.
GrabYourPitchforks commentedon Aug 17, 2021
We should doc both for this and for
SetSwitchthat they're intended to be set very early in the app's startup routine. Other components of the base libraries or the application might read and cache these values, and if you callSetData/SetSwitchafter this caching has taken place your changes won't be honored.deeprobin commentedon Dec 19, 2021
Feel free to assign me 😄