Developer forum

Forum » Dynamicweb 9.0 Upgrade issues » Dynamicweb.Context.Current.Session is sometimes null and thus throws exceptions.

Dynamicweb.Context.Current.Session is sometimes null and thus throws exceptions.

Simon Nordahl
Simon Nordahl
Reply

Hi guys

We're starting to sell Dynamicweb 9 ( 9.2.6) solutions and with that we're sometimes having issues with standard dynamicweb functionalities. 

We can no longer rely on getting the current user from Dynamicweb's api (Dynamicweb.Security.UserManagement.User.GetCurrentUser()) since the Dynamicweb.Context.Current.Session sometimes  (at random) can be null. The user is still there since we can get him through the regular System.Web.HttpContext:

var userSessionKey = $"{(object)"UserManagement.User.Current"}.{(object)PagePermissionLevels.Frontend}";

var user = (User)HttpContext.Current.Session[userSessionKey];

 

Futhermore the problem can also occur in Dynamicwebs own source code, where an exception can be thrown on a regular pageload, all because of the session being null.

 

[NullReferenceException]: Object reference not set to an instance of an object.

at Dynamicweb.Frontend.Devices.DeviceDetector.IsDeviceDetectionEnabled(IContext context)

   at Dynamicweb.Frontend.PageView.DetectDeviceAndPlatform()

   at Dynamicweb.Frontend.PageView.Load()

   at Dynamicweb.Frontend.DynamicwebHttpHandler.ProcessRequest(HttpContext context)

 

Have anybody else experienced this issue or found a workaround untill DW corrects this somewhat critical error?

Regards

Simon Nordahl

 

 

null.png

Replies

 
Morten Buhl Dynamicweb Employee
Morten Buhl
Reply

Hi Simon

Thanks for the bug report. Registered as 31463

/Morten Buhl, DW

 
Simon Nordahl
Simon Nordahl
Reply

That's great, any idea of when you'll have some progress on the matter?

 
Nicolai Pedersen
Reply

We are working on this as we speak - so as soon as we have a verified solution, we will release it. Will keep you posted.

 
Simon Nordahl
Simon Nordahl
Reply

Cool, nice to hear!

In the meantime the problem might extend past the session being null, sometimes when we call Dynamicweb.Base.GetGs("value"), we're also getting a nullreference exception in Base.NotifyTemplateCompatibilityPackCalled(), so it might be the entire context that is not always correct.

 

 
Nicolai Pedersen
Reply

Yes, you are spot on!

Can you reproduce it?

NP

 
Simon Nordahl
Simon Nordahl
Reply

It's not happening everytime but it happens quite often when you are accessing the context from a webservice or generic handler (which works flawless is in dw 8.).

For instance we have a favoritelist which the logged in users can update through clicking a button which makes an ajax call to the handler or webservices, that webservice checks to see if the user is logged in through calling User.GetCurrentUser(), that call will fail often. Its the same case for Base.GetGs(), which we use to look for custom settings in globalsettings.

 

 

 
Simon Nordahl
Simon Nordahl
Reply

A concrete example could be adding items to the cart async through ajax and a generic handler (attached a dummy handler).

As soon as we reach "Dynamicweb.Ecommerce.Common.Context.Cart", we're thrown an exception with the following stacktrace:

   at Dynamicweb.Environment.ExecutingContext.IsAdminLoggedIn()
   at Dynamicweb.Content.Management.EcomLicense.GetLicense()
   at Dynamicweb.Ecommerce.Frontend.Cart.CartCatch.LoadCart()
   at Dynamicweb.Ecommerce.Common.Context.GetCart()
   at Dynamicweb.Ecommerce.Common.Context.get_Cart()
   at StandardWebshop.Handlers.AddToBasket.ProcessRequest(HttpContext context) in xxxxxx\StandardWebshop\Handlers\AddToBasket.ashx.cs:line 20

 

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Simon

Thanks for the additional information. We have a fix ready for this in 9.2.8 which will be released tomorrow.

BR Nicolai

Votes for this answer: 1
 
Jonas Mersholm
Reply

Hi Nicolai,

We are having the same problem on a 9.2.12 solution.

Using : GetCurrentUser(); from a "ApiController", results in nulls, even though an ExtranetUser is actually logged in.

 

Best,

Jonas

 
Nicolai Pedersen
Reply

Hi Jonas

You need to make sure that you have sessionstate enabled for web api and then you have to access the user in the explicit User.Current(level) call instead.

GetCurrentUser() will return either a backend or frontend logged in user depending on the executing context (/Admin or in PageView context), so it can be 'risky' to use that in something not native Dynamicweb.

BR Nicolai

 

You must be logged in to post in the forum