I'm new to WCF and today I have encountered a problem with DataContracts. I'm getting exception when objects are returned to client from WCF Service.
SvcTraceViewer shows the next exception:
Type 'System.Data.Entity.DynamicProxies.Person_7C797A477DD73534D4E8E743E1FCC1C75DAB75933D03B845A097A8B83F2DD748' with data contract name 'Person_7C797A477DD73534D4E8E743E1FCC1C75DAB75933D03B845A097A8B83F2DD748:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' 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.
I have several projects in solution.
- DataAccess (EntityFramework EDMX file is placed here)
- Model (Entities generated by DbContextGenerator are here)
- WCF Service (DataContracts are implemented in
Modelproject) - WPF Application (runs ServiceHost)
- Console Application (calls WCF project or service hosted in WPF Application)
Here is operation contract in WCF:
[OperationContract]
Person[] GetAllPersons(int version);
interface implementation:
public Person[] GetAllPersons(int version)
{
return StorageService.GetAllPersons(version);
}
The excepion is thrown when the data is recieved on the client side (ConsoleApp).
I guess the problem is related to generated entities, because they are partial classes
Here is Person class:
public partial class Person
{
public Person()
{
this.Project = new HashSet<Project>();
}
public int Id { get; set; }
public Nullable<long> AddressId { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string FirstName { get; set; }
public Nullable<long> GeoLocationId { get; set; }
public string FullGeoLocationName { get; set; }
public Nullable<long> SupervisorId { get; set; }
public Nullable<long> PositionId { get; set; }
public string Position { get; set; }
public string Office { get; set; }
public string NativeName { get; set; }
public string Location { get; set; }
public string FullName { get; set; }
public Nullable<long> PmcPersonId { get; set; }
public virtual ICollection<Project> Project { get; set; }
public virtual PersonDataVersion DataVersion { get; set; }
public virtual Workspace Workspace { get; set; }
}
I tried to mark class and it's members with [DataContract] and [DataMember] attributes, but error still happens. [KnownType(typeof(Person)] attribute also didn't help.
Is it possible to use generated entities as data contracts?