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.

On Sunday, November 1, 2020 at 7:38:56 AM UTC-3, Junior Lapuñete wrote:
>
>
> 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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nhusers/e731f2df-8f8f-4765-9d9d-5a88507ee383o%40googlegroups.com.

Reply via email to