WCF + sending linq generated objects - wcf

i have entities generated from my .dbml. So some objects have relationship "one to many" and in entities it was generated as EntitySet.
Also, I have WCF Workflow Service Application which contains .xamlx with bussines logic.
So using this .xamlx i try to send objects which was genereted before to client, but i catch an exception "The underlying connection was closed: The connection was closed unexpectedly".
EntitySet<> was added to KnonwTypes. Serelzation mode in .dbml file has "None" value.
So, could you tell me how i can solve this problem?

is there an inner exception?
my guess is an circular reference caused by the navigation properties.
if that's that case you have a couple of options:
remove the virtual keyword from your navigation properties and eager load everything. Note: not sure if this applies to linq to sql.
map your entities to simple dto objects.
employ a circular reference aware serializer (the one built in to .net is not).

Related

Entity Framework T4 POCO objects raising exception in WCF

These objects have collections of type ICollection<>
If I pass an object graph from client to server it throws the following exception:
System.NotSupportedException was unhandled by user code
Message=Collection was of a fixed size.
Source=mscorlib
Which is occurs in the fixup code the T4 template has generated. It seems the collections are being deserialized on the server as arrays and so can't be modified. Is there a way to specify the type the serializer should use?
I would strongly recommend that you don't use the POCO classes on your service boundary. Create a separate set of classes to model the data you want to send and receive across the wire (Data Transfer Objects - DTOs) and use a tool like automapper to move data between the DTOs and your POCO classes
Essentially you end up tying the consumers of your service to your service's internal conceptual model which means you become constrained in changing your implementation because you need to avoid breaking your clients
Try using the following attribute
[ServiceKnownType(typeof(List<string>))]
If that doesn't work, perhaps try using IList<T> if that is possible in your situation

Problem with WCF/EF 4.1 Lazy Loading

I'm getting data for my application through WCF service. And on the server side the service is using EF4.1 as a data access.
Service method looks kind of like this :
public List<JobOffer> GetAllJobOffers()
{
var allJobOffers = _jobOffersRepository.GetAll().ToList();
return allJobOffers;
}
And the repository is done this way
public override IQueryable<JobOffer>GetAll()
{
return _context.JobOffers.Include(c => c.Company);
}
I am getting this strange error :
An error occurred while receiving the HTTP response to http://localhost:8080/JobsService/ws. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
In debug mode after hitting F5 it starts to loop forever.
In my case it should some problem with lazy loading on the server side. Because when I am calling the service to return objects with simple structure it works like a champ. What could be messed up? And what is the best practice to eager load objects with EF ?
When using EF over WCF lazy loading should be turned off because serialization will trigger lazy loading of every navigation property. Another problem are cyclic references which are not serializable by default. If your Company has also navigation property to its Jobs it makes cyclic reference.
I think your problem is more like to be related to the cyclic relationship. this all happens when serialization comes in scope. lets say u have e entity person which has property addresses which is marked as lazy. on the other side of relationship Addresses entity has a navigation property for Person entity. as serialization takes place it goes through each property of the person entity and as it access Addresses navigation property starts to load it. now it starts to serialize Address entity and as it access Person navigation property it goes back to that property and starts to serialize it and it creates situation when cyclic serialization starts and can not be completed. to overcome this problem u do not have to turn of lazy loading but all u have to do is to decorate you datacontract in (in this case Person and Addresse) with [DataContract(IsReference = true)] and everything works like an charm.
I know this is a bit of an old question, but based on your experiences in your question, I think it might help if you take a look through one of my recent posts
http://sanderstechnology.com/2013/more-with-the-entity-framework-v6-rc1/12423/
I walk through how to send a semi-complex EF object graph which is deserialized through WCF, and I handle lazy loading, proxy generation and cyclic reference issues. It might be worth a look.

How do lazily loaded POCO entities, entity framework and WCF work together?

If a project has used POCO for entities and uses entity framework and uses lazy loading then you have an "incomplete" object graph going back over the wire. So when a client uses the Entity is there some sort of proxy that will automagically load the remaining values? Do we have to create this proxy ourselves and wrap the original entity in it? Or is there an accepted pattern for identifying lazy loaded types which would then signal the client to make another call to the WCF?
Lazy loading with WCF usually doesn't work because your method looks like:
public List<MyPoco> GetData()
{
using (var context = new MyObjectContext())
{
return context.MyPocos.ToList();
}
}
As you see context is closed in method (you have to close context somewhere). But when the list is serialized it will try to lazy load dependent objects => exception because context is already closed. In WCF you should use eager loading.
Use flat DTO's, you probably don't want to expose your full domain to the client anyway. WCF is message-based, not Domain Driven.

Returning NHibernate mapping classes from WCF services

I have a server that handles the database access and a client that consumes the information. The communication from the client to the server is through a WCF service.
When the NHibernate POCO is returned from the service are all the objects in the object graph serialized? If so, is there a way to change it?
I'm also thinking of not returning the NHibernate POCO and instead return an object with only the essential information.
What do you do in these cases?
Use data-transfer objects to move the data from the server to the client. Your business (domain model) objects should not necessarily be exposed outside the core of the application, but should be considered a protected asset.
You can use AutoMapper to automate the translation from business objects to data-transfer objects.
Yeah, you probably want a DTO for this. It's usually considered better to not pass your data objects to the outside world, but also passing hibernate objects directly out of a service can give you some weird behavior, especially if you have lazily loaded collections.

ADO.NET Data Services with Linq-to-SQL

I am encountering a weird error when using linq-to-sql with ado.net data services. I have a simple silverlight application that connects to a remote database. I first added the linq-to-sql class and dragged a table onto the designer. Then I added a ADO.NET Data Service, updated the DataService reference to point to the L2S Data context.
Compiled with no problems.
When I open the service in IE I immediately get an error so I add the following attribute to the data service:
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
This brings up the detailed error message:
The exception message is 'On data context type 'DataClasses1DataContext', there is a top IQueryable property 'table1' whose element type is not an entity type. Make sure that the IQueryable property is of entity type or specify the IgnoreProperties attribute on the data context type to ignore this property
this happens to any database table I use in the L2S designer!
What is this error and why am I getting it?
You'll need to decorate your classes with the DataServiceKey attribute.
More details on Marc's blog here, and an MSDN blog here (the latter talks about many to many relationships, but it covers the DatServiceKey attribute).