Unable to serialize...

Unable to serialize...

Old forum URL: forums.lhotka.net/forums/t/11121.aspx


Hurty posted on Thursday, February 02, 2012

Hello nice people out there :)

I've run into very interesting problem.

So far I am using CSLA 4.1 and have built WCF Service (the standart CSLA built-in service) so I can use CommandBase to execute code on the server (n-tier appliation).

Most of the CommandBase objects are working fine.

The interesting part is...

I have LINQ 2 SQL classes which perform database updates. The program was working locally and was working just fine... the problem is ... when I put the LINQ code in CommandBase I had to make them serializable... so did I.

But the program rised exception about coulnd serialize some of the objects (although I have set the serialization mode to Unidirectional)!??

Any Ideas how I can use LINQ 2 SQL with Dataportal and CommandBase to execute code on server-side ?

 

RockfordLhotka replied on Thursday, February 02, 2012

Is this .NET or Silverlight?

I'll assume .NET, or you wouldn't have gotten anywhere at all.

In .NET, the entire object graph must be serializable. That means the classes that define all the objects in the entire graph must be serializable. Any object referenced from a field must be serializable, or that field must be marked as NotSerialized.

I'm not sure what you mean by "Unidirectional" - perhaps that's a relatively new parameter to [Serializable] I'm unaware of?

BUT, and this is the important part.

I strongly recommend against allowing data layer types or concepts to leak into the business or presentation layers of the application. That's just asking for trouble. Any types created by or composing LINQ to SQL (L2S) are data layer types, and shouldn't be used by business layer code (except maybe in the DataPortal_XYZ methods).

One reason for this strong recommendation is to avoid the complexity you are now facing. Another important reason is to avoid fragility in your application over time. L2S is generally considered to be deprecated, so it is a good bet that you'll eventually need to replace all use of L2S with something more supported. The less of your code that uses L2S types, the easier it will be to get rid of L2S at that time.

Hurty replied on Friday, February 03, 2012

Thank you Rocky... and yes.. I am working under .NET 4.0 and CSLA 4.1

I agree with you Rocky, about the maintenance complexity L2S adds along with CSLA, but the trouble is that I have a lot of L2S code inside the project and I am trying to move that code to work over the APP Server using the CommandBase and the DataPortal.Execute() method.

The "Unidirectinal" parameter is a parameter that sets the serialization mode of the L2S entities. Although I set it to that value it still gives me error?

It says:
ChangeTracker+StandardChangeTracker is not marked serializable...

I am totally lost... should I replace the L2S with standart SQL queries (as I am sure that standart SQL queries work 100% with Dataportal.XYZ() ) ?

Hurty replied on Friday, February 03, 2012

Actually I just found the answer myself... DataPortal.XYZ methods works just fine with L2SQL but one have to do some very UNCOMMENTED (from Microsoft side) devil settings:

1. If you want to send L2S over the wire you should made them serializable as setting the [Serialization] attribute to "Unidirectinal" (use the designer"

2. You have to manually put [Serializable] attribute on each of the generated classes the designer generates...

3. You have to mark all th EntitySet and EntittyRef fields of the generated class as [NonSerializable]

4. You have to comment all the events (such as NotifyPropertyChanging and NotifyPropertyChanged) to allow the serialization engine to do its work properly

5. You have to delete all corresponding interfaces to pt.4 to allow serialization engine to catch only generated classes (not to climb above the graph), so delete INotifiyPropetyCahnging and INotifyPropertyChanged events..

So these are the required steps if you want to use LINQ2SQL with CSLA built-in WCF Service and make the code run server-side..

I hope this post will help other in trouble :)

JonnyBee replied on Friday, February 03, 2012

Hi,

The only serialization supported by L2S is using DataContract and DataMember so the classes can be serialised by the WCF Data Contract Serializer.

CSLA requires a binary serialization and these are not compatible.

You should be able to create a separate WCF Portal for the Linq2Sql queries that use WCF and Data Contract Serializer or make the objects work with BinarySerializer.  I haven't done this myself but you might start with this article http://www.codeproject.com/Articles/22291/LINQ-to-SQL-Serialization that show how to make L2S objects serialize with BinaryFormatter.

Hurty replied on Friday, February 03, 2012

Yes.. that's the point. So following the steps in my answer will enable the CSLA to serialize the L2S entities, although you will face some limitations I had no time to think of potential workaround.

Copyright (c) Marimer LLC