Wcf serialization

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
keeper
Posts: 17
Joined: Mon 27 Dec 2010 09:20

Wcf serialization

Post by keeper » Mon 21 Feb 2011 13:43

I'm using LinqConnect to fetch data from an Oracle database and then serialize the results through wcf services.

I'm using SerializationMode=Unidirectional because I don't need to trace the object in any way (actually I just need the columns/properties values) and I'm returning a single entity or an array of the same entity type.
My DataContext maps different tables with many relations between them.

I can see that the serialized object still have the EntitySet/EntityRef properties while being serialized and they're always NULL (this is right becuase I don't need the relations): is there a way to tell the designer to not add the DataMember attribute to them?

The serialization works almost all the time: sometimes (and I can't still figure out why) it doesn't work and I get the error "An existing connection was forcibly closed by the remote host". I can't find any problem because the call works from a point in my code but from another it doesn't.
I believe the problem lies between the serialization from my wcf service to my client.
I tried also to explicitily close and reopen the wcf client connection and it seems this solves the problem (but it's more expensive and slower).

What can I do to troubleshoot this problem? Any suggestion is really appreciated!

EDIT: I found the problem with my serialization error using Service Tracing (which I recommend to anyone using wcf):
The error is:
Object graph for type 'CiprosDataContext.Gruppo' contains cycles and cannot be serialized if reference tracking is disabled.'
It seems that the server is trying to serialize the entity relations using the objects retrieved from previous call to my service. So if I could remove the relations from serializations completely may be this problem will be solved. Am I correct?

keeper
Posts: 17
Joined: Mon 27 Dec 2010 09:20

Post by keeper » Tue 22 Feb 2011 09:23

I partially solved my problem by changing the InstanceContextMode on my wcf service like this:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
In this mode my object is recreated every time so cached objects in datacontext are not used. This solution is very inefficient (my service need to recreate the object every time).

I almost certain the problem is really about cached object and subsequent queries to the datacontext.

I can only think of three way of solving this problem:
- force to not cache entity on datacontext
- force to not serialize entityset (even if it should be the default behavior)
- remove completely entityset from serialization

Any advice?

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Wed 23 Feb 2011 14:37

As for the 'connection lost' problem, it specifies that the connection to the server was broken for some reason. It may be caused, e.g., by unstable physical connection, or can occur when invalid connections are retrieved from the pool. To fix the latter possibility, please try setting the 'Validate Connection' connection string parameter to true; in this case, invalid connections will be filtered when they are being retrieved from the pool.

To cancel setting the DataMember attribute for EntitySets/EntityRefs, you can change the code generation template. For example, you can remove the following entries from the GenerateRelationProperty() method in the LinqConnect C# template:

Code: Select all

      if (model.SerializationMode != LinqSerializationMode.None) {
#>
        [DataMember(Order=, EmitDefaultValue=false)]

        [DataMember(Order=, EmitDefaultValue=false)]
 'Working with Templates' -> 'Creating and Editing Templates' topic of the Entity Developer documentation.

Please tell us if this helps.

Post Reply