6

How can I create a global variable in an ASP.NET Core Web API application? In ASP.NET MVC, I could do it like:

Application["<variableName>"] = <value>

I tried the same in my web API application, but was unable to find an equivalent for this. I saw some solutions which suggested me to store the data appsettings.json, but since the data I want to store in the global variable is not static, I cannot use that setup. I need to set different data during runtime. How can I do that? Please help me.

Arjun D Nair
  • 217
  • 1
  • 3
  • 14
  • You can use a static class with static property. You can access it and set it's value during runtime. – Chetan Feb 21 '22 at 05:05
  • Hi @Chetan, Thanks for your reply. I'll try it out and let you know. – Arjun D Nair Feb 21 '22 at 05:09
  • using `IOptions` doesn't require anything in `appsettings.json`. Instead you can populate constants with `services.Configure(o => { o.Prop = "value"; });`. Which would make it easier to change to json config later. – Jeremy Lakeman Feb 21 '22 at 05:57

3 Answers3

5

somewhere in project

public static class Config{
       public static Dictionary<string,string> Application = new Dictionary<string,string>();
}

elsewhere

Config.Application["froop"] = "noodle";

of course you will have to deal with race conditions, etc

pm100
  • 48,078
  • 23
  • 82
  • 145
3

We could use Singleton_pattern , creating an Application static object in .net core.

we can also try to use Depend Injection, register a singleton object as below in your code.

Writing a property ConcurrentDictionary in ApplicationInstance class.

public class ApplicationInstance {

    public ConcurrentDictionary<string,object> Application { get; } = new ConcurrentDictionary<string, object>();
}

public void ConfigureServices(IServiceCollection services){
    services.AddSingleton<ApplicationInstance>();
}

Then we might use this object

public class HomeController : ControllerBase
{
    private readonly ApplicationInstance _application;

    public HomeController(ApplicationInstance application)
    {
        this._application = application;
    }
    
    //use _application["test"] instance in your code
}

I would use ConcurrentDictionary to help us avoid racing-condition which will be happened on multiple-thread accessing the same object concurrently.

D-Shih
  • 44,943
  • 6
  • 31
  • 51
3

First of all thank you all for finding some time to help me. I took @pm100's solution and made a slight modification to suit my purpose. I could not use it directly as I am using SonarLint, and it had some issues, so I used a KeyValuePair instead of Dictionary like this:

public static class GlobalData
{
  public static KeyValuePair<string,object> Application { get; set; }
}

and I used it in my code like:

string value = GlobalData.Application.Value

and luckily it works without any issue.

Asons
  • 84,923
  • 12
  • 110
  • 165
Arjun D Nair
  • 217
  • 1
  • 3
  • 14
  • 1
    Should have been a comment " `public static KeyValuePair Application` suited my purpose " and marking @pm100 answer gives you a few points :) – Jeremy Thompson Feb 22 '22 at 04:24
  • Yes @JeremyThompson, but I thought adding it as an answer would make it easier for people who are looking for the solution for the same problem. I may get some points, but that benefits only for me. – Arjun D Nair Feb 22 '22 at 04:27
  • 1
    Though you make no reference that the Value of the Dictionary needed to be an Object or that KeyValuePairs were preferred. Also, trust me no one is coming back to this and taking your answer over pm100's especially when they see whats happened. Like I said better ettiqutte to award the answer that will help everyone else looking for "a global variable in ASP.NET Core Web API application?" – Jeremy Thompson Feb 22 '22 at 04:34
  • Using ```KeyValuePair``` will allow you to store only one variable at a time. Is this what you were looking for? If you want to store list of variables, consider using ```List>```, or as @pm100 suggested - ```Dictionary```, which has easier to use API than ```List```. (I'm not sure why SonarLint would allow you to use ```KeyValuePair```, but not ```Dictionary```) – Mitko Petrov Mar 22 '22 at 09:03
  • Hi @MitkoPetrov, I don't want a list. I just need to store the request to my APIs so that I can log it along with the exception. I can overwrite it on every request. So I don't need a list. SonarLint was showing multiple errors while using Dictionary, and I couldn't find solution for all. So I just used KeyValuePair. – Arjun D Nair Mar 22 '22 at 09:13