[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Linq to SQL serialization?

Posted on 2011-05-06
7
Medium Priority
?
674 Views
Last Modified: 2012-05-11
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!
0
Comment
Question by:badpilot
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

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

Author Comment

by:badpilot
ID: 35717114
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
 
LVL 18

Expert Comment

by:Gary Davis
ID: 35719750
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Accepted Solution

by:
badpilot earned 0 total points
ID: 35721028
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
 
LVL 18

Assisted Solution

by:Gary Davis
Gary Davis earned 2000 total points
ID: 35721184
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
 

Author Comment

by:badpilot
ID: 35722134
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
 

Author Closing Comment

by:badpilot
ID: 35759445
The linked article explains how to resolve the issue, points to gardavis though.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

873 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