HttpContext.Current and HttpContext.Current.Items errors

24 views
Skip to first unread message

Junior Lapuñete

unread,
Nov 1, 2020, 5:38:56 AM11/1/20
to nhusers

I running this code using .net core and it produces these errors: HttpContext.Current and HttpContext.Current.Items errors:

Anybody can help me how to fix these errors in order we can compile and run this using .net core?
And here's the code:

namespace NHibernateUnitOfWork
{
    public static class Local
    {
        static readonly ILocalData _data = new LocalData();

        public static ILocalData Data
        {
            get { return _data; }
        }

        private class LocalData : ILocalData
        {
            [ThreadStatic]
            private static Hashtable _localData;
            private static readonly object LocalDataHashtableKey = new object();

            private static Hashtable LocalHashtable
            {
                get
                {
                    if (!RunningInWeb)
                    {
                        if (_localData == null)
                            _localData = new Hashtable();
                        return _localData;
                    }
                    else
                    {
                        var web_hashtable = HttpContext.Current.Items[LocalDataHashtableKey] as Hashtable;
                        if (web_hashtable == null)
                        {
                            web_hashtable = new Hashtable();
                            HttpContext.Current.Items[LocalDataHashtableKey] = web_hashtable;
                        }
                        return web_hashtable;
                    }
                }
            }

            public object this[object key]
            {
                get { return LocalHashtable[key]; }
                set { LocalHashtable[key] = value; }
            }

            public int Count
            {
                get { return LocalHashtable.Count; }
            }

            public void Clear()
            {
                LocalHashtable.Clear();
            }

            public static bool RunningInWeb
            {
                get { return HttpContext.Current != null; }
            }
        }
    }
}

Thanks.

Carlos Alberto Costa Beppler

unread,
Nov 1, 2020, 4:06:51 PM11/1/20
to nhusers
There is no way to this code run on .NET Core, on ASP.NET Core the static HttpContext.Current property of System.Web is not used and always be null.

For example, the best way to manage sessions on ASP.NET Core is to configure ISessionFactory (as singleton) and ISession (as scoped) on DI on ConfigureServices method of the Startup class like bellow.

services.AddSingleton<NHibernate.ISessionFactory>(factory =>
{
    return new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
}); 

services.AddScoped<NHibernate.ISession>(factory => factory.GetRequiredService<NHibernate.ISessionFactory>().OpenSession());

After that you must change the code that uses the old static class to be dependent on ISession interface (add an ISession parameter to the constructor).

I don't know if you are using this hashtable to manage sessions, but I assuming it because you posted your message on an NHibernate group.
Reply all
Reply to author
Forward
0 new messages