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!
badpilotAsked:
Who is Participating?
 
badpilotAuthor Commented:
FirstOrDefault and ToList still returns the same error although I wasn't aware of defferred execution with Linq so thanks for that.

I discovered this article which I think relates to my problem.

http://www.jstawski.com/archive/2009/11/13/1231.aspx

Applying this fix would require quite alot of re-writting though and as the article is a little old I was wondering if there is maybe an easier way around this now? The app is in .net 4, have there been any changes that might allow me to mark all of the linq generated objects as serializable automatically?

Thanks again.
0
 
Gary DavisDir Internet SvcsCommented:
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
0
 
badpilotAuthor Commented:
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
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Gary DavisDir Internet SvcsCommented:
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
0
 
Gary DavisDir Internet SvcsCommented:
Well if this is the only place you are having this issue, you could select the properties you need into a your own plain old class object and stick that into the Session.Mark it with the serializable if necessary. Change the references to use the same class when pulling it out of the Session.

Or just save the few values you need directly into cache instead of using the class.
0
 
badpilotAuthor Commented:
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!
0
 
badpilotAuthor Commented:
The linked article explains how to resolve the issue, points to gardavis though.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.