SessionAs returns old state after PopulateWithNonDefaultValues but works fine if assigned by hand Version 4.0.51.0

Hi,

I have a strange behavior when I populate my own typed session with session.PopulateWithNonDefaultValues(userModel)". I get the old session back when I use SessionAs but everything works fine if I simply assigne the new values with session.FirstName = "MyVal"

var session = SessionAs<MySession>();
session.PopulateWithNonDefaultValues(userModel);
// works fine if I do: 
// session.FirstName = "MyVal";
this.SaveSession(session);
var session2 = SessionAs<MySession>();

Seems like a reference issue to me - I would like to use some kind of populating instead of assigning all properties by hand.

Thanks for help or hints

Do the property names match? i.e. you have FirstName property on MySession and userModel?

Yes,

public class MySession: AuthUserSession {}
public class MyModel : UserAuth {} 

Both instances looks absolutely the same after populating e.g. if I output to JSON or set a breakpoint at this.SaveSession(session) - but this.SaveSession(session); seems not to save the session or SessionAs do something different. Sorry, maybe it helps - I forgot to say that I’m using Reids as CacheClient.

.

Something seems wrong, are you saying that if you manually populate the property with:

session.FirstName = "MyVal";

And Save and retrieve the session it works, but if you use PopulateWithNonDefaultValues() the property is still populated but it no longer saves the property? can you confirm this in a debugger because that sounds wrong, it shouldn’t matter how the property is populated.

Yes exactly,

did some more testing:

        //session.PopulateWithNonDefaultValues(userEntity);
        userEntity.FirstName = "John";

        session.FirstName = userEntity.FirstName;
        session.LastName = userEntity.LastName;

        this.SaveSession(session);

        var sessionToCheck = SessionAs<PortalUserSession>();

this works sessionToCheck.FirstName == John.

But this leads to the error:

        userEntity.FirstName = "John";

        session.PopulateWithNonDefaultValues(userEntity);

        session.FirstName = userEntity.FirstName;
        session.LastName = userEntity.LastName;

        this.SaveSession(session);

        var sessionToCheck = SessionAs<PortalUserSession>();

now sessionToCheck.FirstName is null. It doesn’t even matter where I put PopulateWithNonDefaultValues at. If I do a call to the method SessionAs returns the old data.

The only one difference is: session.PopulateWithNonDefaultValues(userEntity);

I could hardly believe it :flushed:

In that case you’re probably overwriting the session id? can you try, preserving the session.Id, e,g:

var sessionId = session.Id;
session.PopulateWithNonDefaultValues(userEntity);
session.Id = sessionId;
this.SaveSession(session);

Omg, thats it - thats why it looks equal. Didn’t recognized because Id seems the be correct (User.Id == Session.Id). Confirm also in Redis - UserId was used as SessionId.

Thanks a lot!

1 Like