Skip to content

Proposal: AppContext.SetData #47922

Closed

Description

@Sergio0694

Background and Motivation

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?

public static void SetData(string name, object? data)

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);
    }
}

Activity

  1. added
    api-suggestion
    on Feb 5, 2021
  2. ghost added
    untriaged
    on Feb 5, 2021
  3. tannergooding commented on Jun 26, 2021

    @tannergooding
    Member

    CC. @jkotas, just wanting to confirm that there isn't an intentional reason behind this not being exposed.

  4. jkotas commented on Jun 26, 2021

    @jkotas
    Member

    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.

  5. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on Jun 26, 2021
  6. tannergooding commented on Jun 26, 2021

    @tannergooding
    Member

    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.

  7. added this to the Future milestone on Jul 12, 2021
  8. bartonjs commented on Aug 17, 2021

    @bartonjs
    Member

    Video

    Looks good as proposed, for symmetry.

    namespace System
    {
        public static partial class AppContext
        {
            public static void SetData(string name, object? data);
        }
    }
  9. added
    api-approved
    and removed
    api-ready-for-review
    on Aug 17, 2021
  10. GrabYourPitchforks commented on Aug 17, 2021

    @GrabYourPitchforks
    Member

    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.

  11. deeprobin commented on Dec 19, 2021

    @deeprobin
    Contributor

    Feel free to assign me 😄

  12. ghost locked as resolved and limited conversation to collaborators on Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Runtime

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions