Patrick\’s Bytes

19, June 2008

Workflow Foundation and LINQ2SQL: Of BinaryFormatter and DataContractSerializer

Filed under: LINQ to SQL,Windows Workflow Foundation — patrickyong @ 12:44 am

There seem to be quite a lot of questions in forums and blog post about inability of have LINQ to SQL object in a Workflow Foundation’s Activity execution. Most of the time, you will end up getting a error message as below

Type ‘System.Data.Linq.ChangeTracker+StandardChangeTracker’ in Assembly ‘System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ is not marked as serializable.

After I did some research and found out about serialization method in LINQ to SQL here:

http://msdn.microsoft.com/en-us/library/bb386929.aspx

Code generation in LINQ to SQL supports DataContractSerializer serialization. It does not support XmlObjectSerializer or BinaryFormatter. For more information, see Serialization (LINQ to SQL).

However for Workflow Foundation, it is using BinaryFormatter to serialize objects into persistent layer (refer the MSDN document here)

When the workflow runtime encounters a persistence point during workflow instance execution, it calls the persistence service to do the work. The persistence service will then serialize the workflow state into a stream using BinaryFormatter serialization, optionally compress it, and save it into a durable store.

As such there is a crash here which renders LINQ to SQL entities can’t be used in Workflow Foundation. Fortunately Serena Yeoh found a solution and put it into her project at Layer Sample. You can found out more at this forum thread. Basically her code will take LINQ to SQL objects, clone it and serialize it using DataContractSerializer first before committing changes. Then after that it will return the cloned copy.

Advertisements

2 Comments »

  1. HI,

    but i think the issue is not exactly this only.
    i am facing a trouble while fetching data from database using linq classes. the scenario is something like this:
    1) i need to fetch object of class A
    2) class A has entitySet of class B object that are having EntityRef to class A object in them
    3) now, while fetching Class A object i am facing a problem that Wrokflow is not able to fetch that object because EntitySet has been derived from iList and that is not serializable.

    and if will not use IList i.e. EntitySet , then the real magic of LINQ vanishes; as i am using cascade update feature of LINQ classes where one has to commit only the base object and all the other objects ttached to it gets submitted to database automatically.

    Hope you can understand. please help me out.

    Regards,
    Ashish D. Sehajpal

    Comment by Ashish D. Sehajpal — 11, August 2008 @ 2:16 pm | Reply

  2. Hi Rick,

    Your blog is very useful. It really summarizes the issues nicely. I did find a better solution I think though. It appears that LINQ to Entities and ADO.NET Entity Framework are a very good solution. I pieced together information on LINQ to SQL Serialization and WF that you and your readers may find useful.

    http://justgeeks.blogspot.com/2009/04/linq-to-sql-cant-be-serialized-when.html

    I spent a lot of time looking for a good solution, I hope this helps others.

    Thank you for your very useful blog.

    Brent

    Comment by Brent — 4, April 2009 @ 5:33 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: