Question

I'm building a Silverlight MVVM template and and am getting stuck with the WCF Service returning and Entity Object.

Here's what I did:

  1. Using Entity Framework on the server side
  2. Created a small test database with a couple of tables.
  3. Created a WCF Service on the server side
  4. I then created a small test method returning an integer.
  5. On my client side, I added a service reference and I receive the integer result in my completed method successfully
  6. then changed my test service method to return a "Person" object (which is an Entity from Entity Framework)
  7. updated my service reference and then it doesn't work!
  8. I then the return type to any basic CLR Type and it works again

I checked Fiddler and I get the following 504 error in my service response:

HTTP/1.1 504 Fiddler - Receive Failure Content-Type: text/html; charset=UTF-8 Connection: close Timestamp: 08:56:23.783

[Fiddler] ReadResponse() failed: The server did not return a response for this request.

After trying to figure this out, I came across WCF Trace Logging and found this error:

    There was an error while trying to serialize parameter :BasicResult. The InnerException message was 'Type 'MVVMProject.Web.DataSource.Person' with data contract name 'Person:http://schemas.datacontract.org/2004/07/MVVMProject.Web.DataSource' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

I don't understand why this is so difficult? Must I set some property on my Entity to make it serializable? If I look at the Entity Framework's designer.cs file, I see a Serializable attribute on the Entity. Surely this means I can pass this via the WCF Service??? I don't understand this error, unfortunately...

Is it even possible to use Entity Framework with WCF Service?

Any help would be greatly appreciated.

Was it helpful?

Solution

I had same problem, it seams that the DataContractSerializer has a problem with the navigation properties of ef objects.

In my test project, I'm using the northwind database. I wanted to test the CodeFirst approach with the recommended DbContext.

The provided navigation properties are virtual and they are loading on demand, but the ef just return with the first level of the entity on navigation properties is filled.

On serializing the entity object the DataContractSerializer failed because the entity object is no longer bound to the DbContext and the serialization of the navigation properties failed.

This is happen when I try to consume a NW Employee object over my wcf service.

My soultion is to copy all data in a new object with the data contract attributes!

the service call:

public IEnumerable<EmployeeWcf> GetAll()
    {
        IEnumerable<EmployeeWcf> result = null;

        result = from e in context.Employees.OrderBy( e => e.LastName ) 
                         select new EmployeeWcf 
                         { 
                             EmployeeId = e.EmployeeID, 
                             Firstname = e.FirstName, 
                             Lastname = e.LastName 
                         };

        return result;
    }

the class:

    [DataContract]
public class EmployeeWcf
{
    [DataMember]
    public int EmployeeId { get; set; }
    [DataMember]
    public string Firstname { get; set; }
    [DataMember]
    public string Lastname { get; set; }
}

This work but it seems to me that this is not a perfect solution.

I hope this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top