Why am I getting the exception "Consider using a DataContractResolver or add any types not known statically to the list of known types"

StackOverflow https://stackoverflow.com/questions/22121392

Question

I'm trying to serialise a object to Xml using the DataContractSerializer. I have the following classes;

[ActiveRecord(Lazy = true)]
[KnownType(typeof(RoomType))]
[DataContract]
public class Room : ActiveRecordBase<Room>
{
    [PrimaryKey]
    [DataMember]
    public virtual Int64 RoomId { get; protected set; }

    [BelongsTo("RoomTypeId")]
    [DataMember]
    public virtual RoomType RoomType { get; set; }

    [Property]
    [DataMember]
    public virtual Int64 HotelId { get; set; }

    [Property]
    [DataMember]
    public virtual string Name { get; set; }

    [Property]
    [DataMember]
    public virtual string Description { get; set; }

    public static Room[] FindByHotelId(Int64 HotelId)
    {
        return (Room[])FindAllByProperty(typeof(Room), "HotelId", HotelId);
    }
}

The RoomType class is

[ActiveRecord(Lazy = true)]
[DataContract]
public class RoomType : ActiveRecordBase<RoomType>
{
    [PrimaryKey]
    [DataMember]
    public virtual int RoomTypeId { get; protected set; }

    [Property]
    [DataMember]
    public virtual string Name { get; set; }

}

I use the following method to serialise the object

    internal static XElement ObjectToXElement<T>(T source)
    {
        XDocument oXDocument = new XDocument();

        try
        {
            using (var writer = oXDocument.CreateWriter())
            {
                // write xml into the writer
                var serializer = new DataContractSerializer(source.GetType());
                serializer.WriteObject(writer, source);
            }
        }
        catch(Exception e)
        {
            using (var writer = oXDocument.CreateWriter())
            {
                // write xml into the writer
                var serializer = new DataContractSerializer(oError.GetType());
                serializer.WriteObject(writer, oError);
            }
        }

        return oXDocument.Root;
    }

The actual object I'm serialising is;

[KnownType(typeof(List<Room>))]
[KnownType(typeof(RoomType))]
[DataContract]
public class RoomTypeResponse
{
    [DataMember]
    public int Code { get; set; }

    [DataMember]
    public string Message { get; set; }

    [DataMember]
    public List<Room> Rooms { get; set; }

    public RoomTypeResponse()
    {
        this.Rooms = new List<Room>();
    }
}

But for some reason when I call the method to serialise the object I get the following exception;

Type 'Castle.Proxies.RoomTypeProxy' with data contract name 'RoomTypeProxy:http://schemas.datacontract.org/2004/07/Castle.Proxies' 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.

If I comment out the property in Room class, it works fine

[BelongsTo("RoomTypeId")]
[DataMember]
public virtual RoomType RoomType { get; set; }

I'm not sure why I am getting the exception, because I've added the knowtype attribute for RoomType ? What am I missing, that is causing this problem.

Was it helpful?

Solution

The problem is that one type (Castle.Proxies.RoomTypeProxy) is generated at runtime, so .NET knows nothing about it. This is not an NHibernate-specific problem. If you disable lazy loading and proxy generation, the problem will go away, but I understand it might be difficult. Other option would be to use another serializer, like BinaryFormatter, but I don't know if that will work for you.

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