2

I try to store my object to session in a asmx web service

 user = new MyUser(UserName);
 Session["user"] = user;

and read from a master page in page load section but I get null

user = (MyUser)Session["user"];

it work when I try with a simple string instead of MyUser object

Any idea?

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ngi
  • 378
  • 1
  • 4
  • 16
  • is your `MyUser` serializable? – balexandre Jul 04 '12 at 15:00
  • yes it is: '[Serializable] public class User' – ngi Jul 04 '12 at 15:10
  • if you're sure that it works with string, then I have no clue, for me, the logical part is not to work at all and they **will** probably have 2 different pools and `Sessions`'s are not shared like that. I would use SQL Sessions or a Cache system to change messages between. – balexandre Jul 04 '12 at 15:14
  • And when I try to get this object in the asmx page I can, but from another page I can't ... – ngi Jul 04 '12 at 15:20
  • 1
    ..I will not prefer putting session variables in webservices..though did you tried it like this : HttpContext.Current.Session["user"] = user? – Suave Nti Jul 04 '12 at 15:23
  • yes I've tried HttpContext.Current.Session Strange, because when I try get value from aspx file it work, but from the mester page not ... – ngi Jul 05 '12 at 08:43

3 Answers3

5

Assuming the process running your service is the same serving up your pages, you need to ensure the WebMethod has the session enabled by decorating the method with

[WebMethod(EnableSession = true)]

Here is another answer with more detail.

Community
  • 1
  • 1
Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
  • Thanks, but I already use this, and the session is work, but when I try to read an object from the session in the master page, get null – ngi Jul 05 '12 at 08:47
  • @Jaime Torres Ohhhh man, this is not a "simple answer"... this one saves me a complete rollback of few days of work until I got stuck on an issue where this single attribute solves it... Thanks man. Like your name meaning in french; «`J'aime` vraiment ta réponse!» (I really like your answer). – Simon Dugré Jun 10 '14 at 19:16
0

Who's the client of the service and who is the client of the page? Are they sharing cookies?

Answers to these questions are fundamental because session sharing involves sharing a session cookie with the same session id.

From what you write it is not clear but most probably you just have to share (for example copy) cookies between two clients - the web service client and the page client.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
0

I found the solution! In the master page I tried to read session on the Page_Load event, but it was wrong. When I put in Page_Init event it worked :)

ngi
  • 378
  • 1
  • 4
  • 16