Entity Framework 4.0 with WCF 4.0 DataContractSerializer Error

.net entity-framework


I'm trying to use WCF to get a list of items from Entity Framework, but I keep getting the following error:

The attempt to serialize the argument http://tempuri.org/:GetAllResult failed. Type 'System appeared in the InnerException message. Data.Entity. DynamicProxies. Unexpected test object 'TestObject 240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' with data contract name 'TestObject 240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B40 To add any kinds that are not known statically to the list of known types, use the KnownTypeAttribute attribute or add them to the list of known types that is supplied to the DataContractSerializer, for example. For further information, please refer to InnerException.

WCF is something I've used before, but never with Entity Framework. I use Entity Framework to create all of my entities, and they are each annotated with [DataContract] and [DataMember] properties. On any of my entities, there are no Navigation Properties.

The call to the GetAll() function comes from an abstract service class:

public interface IService<T>
    List<T> GetAll();

And I am calling my implementation using the ChannelFactory:

Binding binding = new NetTcpBinding();
EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8081/" + typeof(TestObjectService).Name);
using (ChannelFactory<ITestObjectService> channel = new ChannelFactory<ITestObjectService>(binding, endpointAddress))
    ITestObjectService testObjectService = channel.CreateChannel();
    testObjects = testObjectService.GetAll();

As such, I am hosting it:

Type type = typeof(TestObjectService);
ServiceHost host = new ServiceHost(type,
            new Uri("http://localhost:8080/" + type.Name),
            new Uri("net.tcp://localhost:8081/" + type.Name));

Although it successfully locates the objects from the database when utilizing debugging, it fails to return the objects.

Any suggestions as to where I could be making mistakes?

7/30/2010 3:20:26 PM

Accepted Answer

It took me a while to realize why, but EntityFramework builds a "proxy" of your class. The TestObject class I had was configured properly, but a class with the name TestObject 240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE was being created by it.

You must add the following to your Context constructor in order for the ChannelFactory, WCF, and Entity Framework to all function together:

ContextOptions.ProxyCreationEnabled = false;

I hope this is useful to someone else.

7/30/2010 3:49:32 PM

Popular Answer

I have to perform the following when using the DbContext API for Code First (EF 4.3):

public class MyClass : DbContext
    public MyClass()
        base.Configuration.ProxyCreationEnabled = false;

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow