Link to home
Start Free TrialLog in
Avatar of badpilot
badpilot

asked on

Linq to SQL serialization?

Hello

I'm looking to move my session state store from InProc to SQL Server which I've managed to set up successfully. I'm receiving an error unable to serialize session state as I'm storing objects in there.

All of the objects are from Linq to SQL generated code, for instance customer.

I've changed the serialization mode on the data context to 'unidirectional' but this hasn't resolved the problem.

One of the more simple pages i get the error on is the login page, example of what it does:


Username and email is passed to a method which looks up the DB, if it matches then the customer object created by the linq to sql code is stored in session so it can be accessed later. It falls over at this part.

Any suggesstions on how I can get this working?

Many thanks!
Avatar of Gary Davis
Gary Davis
Flag of United States of America image

An example of your code and the save to session variable may help.

Do you end the Linq with ToList() to make sure it executes and provides the actual list?

Gary Davis
Avatar of badpilot
badpilot

ASKER

I'm not returning it to list, below is a quick example of the code, please ignore any errors, the actual code is on my work machine.

Public Sub Login(byval email as string, byval passw as string)

dim db as new linqDBDataContext

dim q = (From c in db.Customers
            Where c.emailAddress = email and c.password = passw
            Select c).first

session.add("oCustomer", q)

db.dispose

End Sub

Open in new window


And then I would retreive it by using

dim oCust as Customer = DirectCast(Session("oCustomer"), Customer)

Open in new window


Thanks
I suspect that Linq deferred execution means that the result of the query has not actually executed at the time you store the variable in the Session object (see http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx). That's why I mentioned the ToList() which forces query execution at that point. Perhaps use firstordefault.

Gary
ASKER CERTIFIED SOLUTION
Avatar of badpilot
badpilot

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your perfectly correct, I wish I had known about this before starting the project.

The project is quite large and I'm resigned to the fact that I'm going to have to rewrite any part where something is added to or retrieved from session. I'm using Jonas' technique in the article above opposed to making all of the classes myself as there are a few (and it seems silly to do this when all of the code is already there).

I should really have a couple of methods that store and retrieve info from session and then I would have only had to change the code in one place. Everyday's a school day.

Points for pointing out deferred execution with Linq and for confirming there was no quick fix for this, thanks!
The linked article explains how to resolve the issue, points to gardavis though.