XML serialization error in REST API when using generic types and inheritance

I get an error in my REST API when I serialize my return objects that use common types in XML. This error does not reproduce when using JSON.

Here is a simplified example demonstrating my problem:

using System;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Web.Http;

namespace Areas.API
{
    public abstract class AnimalController<TProperties> : ApiController
        where TProperties : new()
    {
        protected abstract TProperties makeNew( Int32 Id );

        [Route( "{id}" )]
        public AnimalReturn<TProperties> Get( Int32 id )
        {
            TProperties p = makeNew( id );
            return new AnimalReturn<TProperties>()
            {
                Properties = p,
                Request = Request,
                StatusCode = HttpStatusCode.OK
            };
        }
    }


    public class AnimalReturn<TProperties> : AnimalResult
    {
        public TProperties Properties;
    }

    public class AnimalResult : IHttpActionResult
    {
        [IgnoreDataMember]
        public HttpStatusCode StatusCode = HttpStatusCode.OK;

        [IgnoreDataMember]
        public HttpRequestMessage Request = null;

        public Task<HttpResponseMessage> ExecuteAsync( System.Threading.CancellationToken cancellationToken )
        {
            HttpResponseMessage response = null;
            if( null != Request )
            {
                response = Request.CreateResponse( StatusCode, this );
            }
            return Task.FromResult( response );
        } // ExecuteAsync()
    }

    [RoutePrefix( "api/v3/Fish" )]
    public class FishController : AnimalController<FishController.FishProperties>
    {
        public class FishProperties
        {
            public bool IsShark;
        }

        protected override FishProperties makeNew( Int32 Id )
        {
            return new FishProperties()
            {
                IsShark = true
            };
        }
    }

    [RoutePrefix( "api/v3/Dog" )]
    public class DogController : AnimalController<DogController.DogProperties>
    {
        public class DogProperties
        {
            public string Breed;
            public Int32 TagNo;
        }

        protected override DogProperties makeNew( Int32 Id )
        {
            return new DogProperties()
            {
                Breed = "Labrador",
                TagNo = 12345
            };
        }

    }
}

When I run this API call:

localhost/api/v3/Dog/1

with title

Accept: text/xml

I get this error:

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</ExceptionMessage>
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <StackTrace />
    <InnerException>
        <Message>An error has occurred.</Message>
        <ExceptionMessage>Type 'Areas.API.AnimalReturn`1[[Areas.API.DogController+DogProperties, NbtWebApp, Version=2012.2.4.1, Culture=neutral, PublicKeyToken=null]]' with data contract name 'AnimalReturnOfDogController.DogPropertiesS2PP9ThI:http://schemas.datacontract.org/2004/07/Areas.API' 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.</ExceptionMessage>
        <ExceptionType>System.Runtime.Serialization.SerializationException</ExceptionType>
        <StackTrace>   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph)
   at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)
   at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.WebHost.HttpControllerHandler.&lt;WriteBufferedResponseContentAsync&gt;d__1b.MoveNext()</StackTrace>
    </InnerException>
</Error>

However, when I remove the inheritance (pay attention to AnimalReturn2), the problem disappears:

using System;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Web.Http;

namespace Areas.API
{
    public abstract class AnimalController<TProperties> : ApiController
        where TProperties : new()
    {
        protected abstract TProperties makeNew( Int32 Id );

        [Route( "{id}" )]
        public AnimalReturn2<TProperties> Get( Int32 id )
        {
            TProperties p = makeNew( id );
            return new AnimalReturn2<TProperties>()
            {
                Properties = p,
                Request = Request,
                StatusCode = HttpStatusCode.OK
            };
        }
    }

    public class AnimalReturn2<TProperties> : IHttpActionResult
    {
        public TProperties Properties;

        [IgnoreDataMember]
        public HttpStatusCode StatusCode = HttpStatusCode.OK;

        [IgnoreDataMember]
        public HttpRequestMessage Request = null;

        public Task<HttpResponseMessage> ExecuteAsync( System.Threading.CancellationToken cancellationToken )
        {
            HttpResponseMessage response = null;
            if( null != Request )
            {
                response = Request.CreateResponse( StatusCode, this );
            }
            return Task.FromResult( response );
        } // ExecuteAsync()
    }

    [RoutePrefix( "api/v3/Fish" )]
    public class FishController : AnimalController<FishController.FishProperties>
    {
        public class FishProperties
        {
            public bool IsShark;
        }

        protected override FishProperties makeNew( Int32 Id )
        {
            return new FishProperties()
            {
                IsShark = true
            };
        }
    }

    [RoutePrefix( "api/v3/Dog" )]
    public class DogController : AnimalController<DogController.DogProperties>
    {
        public class DogProperties
        {
            public string Breed;
            public Int32 TagNo;
        }

        protected override DogProperties makeNew( Int32 Id )
        {
            return new DogProperties()
            {
                Breed = "Labrador",
                TagNo = 12345
            };
        }

    }
}

Now, when I run the same query, I get:

<AnimalReturn2OfDogController.DogPropertiesS2PP9ThI 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://schemas.datacontract.org/2004/07/Areas.API">
    <Properties>
        <Breed>Labrador</Breed>
        <TagNo>12345</TagNo>
    </Properties>
</AnimalReturn2OfDogController.DogPropertiesS2PP9ThI>

Question: is there a way to solve the problem of XML serialization when using inheritance?

KnownTypes ( : - : " " , DataContractSerializer - , ?) ​​ , , - .

-, .

.

+4
3

AnimalResult.ExecuteAsync(), HttpRequestMessageExtensions.CreateResponse<T> Method (HttpRequestMessage, HttpStatusCode, T):

            response = Request.CreateResponse( StatusCode, this );

this AnimalResult, T, . , , , KnownType AnimalResult:

[KnownType(typeof(AnimalReturn<DogController.DogProperties>))]
[KnownType(typeof(AnimalReturn<FishController.FishProperties>))]
public class AnimalResult : IHttpActionResult
{
}

, MakeGenericMethod(), HttpRequestMessageExtensions.CreateResponse<T> this.GetType() , . , ?

, , AnimalResult AnimalResult :

public class AnimalResult : IHttpActionResult
{
    [IgnoreDataMember]
    public HttpStatusCode StatusCode = HttpStatusCode.OK;

    [IgnoreDataMember]
    public HttpRequestMessage Request = null;

    private static HttpResponseMessage StaticCreateResponse<TAnimalResult>(TAnimalResult animalResult) where TAnimalResult : AnimalResult
    {
        if (animalResult == null)
            throw new ArgumentNullException(); // Should have been checked outside.
        Debug.Assert(typeof(TAnimalResult) == animalResult.GetType());
        return animalResult.Request.CreateResponse(animalResult.StatusCode, animalResult);
    }

    HttpResponseMessage CreateResponse()
    {
        if (Request == null)
            return null;
        else
        {
            var method = typeof(AnimalResult).GetMethod("StaticCreateResponse", BindingFlags.NonPublic | BindingFlags.Static);
            var genericMethod = method.MakeGenericMethod(new[] { GetType() });
            return (HttpResponseMessage)genericMethod.Invoke(null, new object[] { this });
        }
    }

    public Task<HttpResponseMessage> ExecuteAsync( CancellationToken cancellationToken )
    {
        var response = CreateResponse();
        return Task.FromResult( response );
    } // ExecuteAsync()
}
+1

@dbc , KnownType() , , , .

, Type , , . :

    public class AnimalReturn<TProperties> : AnimalResult<AnimalReturn<TProperties>>
    {
        public TProperties Properties;
    }

    public class AnimalResult<T> : IHttpActionResult
        where T : class
    {
        [IgnoreDataMember]
        public HttpStatusCode StatusCode = HttpStatusCode.OK;

        [IgnoreDataMember]
        public HttpRequestMessage Request = null;

        public Task<HttpResponseMessage> ExecuteAsync( System.Threading.CancellationToken cancellationToken )
        {
            HttpResponseMessage response = null;
            if( null != Request )
            {
                response = Request.CreateResponse( StatusCode, this as T );
            }
            return Task.FromResult( response );
        } // ExecuteAsync()
    }

, , AnimalResult, , CreateResponse.

+1

Using the xml serializer will all work correctly. To change the use of the serializer, config.Formatters.XmlFormatter.UseXmlSerializer = true; in the Register method of the WebApiConfig class.

0
source

Source: https://habr.com/ru/post/1606621/


All Articles