Linq to SQL serialization?

Posted on 2011-05-06
Last Modified: 2012-05-11

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!
Question by:badpilot
    LVL 18

    Expert Comment

    by:Gary Davis
    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

    Author Comment

    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)
    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

    LVL 18

    Expert Comment

    by:Gary Davis
    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 That's why I mentioned the ToList() which forces query execution at that point. Perhaps use firstordefault.


    Accepted Solution

    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.

    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.
    LVL 18

    Assisted Solution

    by:Gary Davis
    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.

    Author Comment

    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!

    Author Closing Comment

    The linked article explains how to resolve the issue, points to gardavis though.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now