سؤال

I am trying to access global.asax application variable from WCF, that's my goal at least. I've tried many type of solutions, but the one that I am trying now is using static variables.

I've created a StaticVariable.cs class like so:

  public static class StaticVariables
    {
        private static string _Key = "name1";

        public static Object someInfo
        {
            get
            {    
                return HttpContext.Current.Application[_Key];
            }           
        } 
    }

The Application["name1"] is initialized in the global.asax.cs. I can read it when I access my webservice but not in my WCF service.

In my WCF I call the StaticVariables someInfo to retrieve the data, but I get:

System.Web.HttpContext.Current is null error

My WCF is running asynchronously and its called from within a webservice using Task<int>.Factory.FromAsync. So I assume that the problem is that the WCF runs not on the main thread, but I am not sure.

So it seems that the Static class doesn't work in my case and I wanted to know how to solve this. Thanks

هل كانت مفيدة؟

المحلول

Why don't you simply use static variables ?

HttpContext is dependent on ASP.NET pipeline. In a host-agnostic model (OWIN or self-hosted) you don't have access to it.

Application storage in HttpApplicationState is only useful if you need to access the current HttpContext. If it's not necessary, you should simply use static properties.

Moreover, HttpApplicationState was initially created for backward compatibility with classic ASP.

public static class StaticVariables
{
    public static object SomeInfo { get; set; }
}

See also Singleton and HttpApplicationState and http://forums.asp.net/t/1574797.aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top