Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing a Dynamic Type in Web API

I'm trying to create a generic function that when given an Enum Type will return an object that when serialized by WebApi will provide nice looking output as XML/Json.

This method works perfectly fine when serialized as JSON, but I'm unable to get it working with XML. If I serialize the returned object manually with either an XmlSerializer or DataContractSerializer, I get results as expected. When WebApi itself tries to serialize it on the other hand from an HttpRequest, I get errors like the following:

System.Runtime.Serialization.SerializationException

Type 'Priority' with data contract name 'Priority:http://schemas.datacontract.org/2004/07/' 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've tried using GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer to set the serializer for the generated type that I know works from setting breakpoints, but it just seems to ignore it and throws the same exception. The enums will be backed by integers and are guaranteed to have unique values for each entry. Here's the code I'm using to generate the type and return an instance of it.

public object GetSerializableEnumProxy( Type enumType ) {

    if ( enumType == null ) {
        throw new ArgumentNullException( "enumType" );
    }

    if ( !enumType.IsEnum ) {
        throw new InvalidOperationException();
    }

    AssemblyName assemblyName = new AssemblyName("DataBuilderAssembly");
    AssemblyBuilder assemBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
    ModuleBuilder moduleBuilder = assemBuilder.DefineDynamicModule("DataBuilderModule");
    TypeBuilder typeBuilder = moduleBuilder.DefineType( enumType.Name, TypeAttributes.Class | TypeAttributes.Public );

    // Add the [DataContract] attribute to our generated type
    typeBuilder.SetCustomAttribute(
        new CustomAttributeBuilder( typeof(DataContractAttribute).GetConstructor( Type.EmptyTypes ), new object[] {} )
    );

    CustomAttributeBuilder dataMemberAttributeBuilder = new CustomAttributeBuilder(
        typeof(DataMemberAttribute).GetConstructor(Type.EmptyTypes), new object[] {}
    );

    // For each name in the enum, define a corresponding public int field
    // with the [DataMember] attribute
    foreach ( var value in Enum.GetValues(enumType).Cast<int>() ) {
        var name = Enum.GetName( enumType, value );

        var fb = typeBuilder.DefineField( name, typeof(int), FieldAttributes.Public );

        // Add the [DataMember] attribute to the field
        fb.SetCustomAttribute( dataMemberAttributeBuilder );

        // Set the value of our field to be the corresponding value from the Enum
        fb.SetConstant( value );
    }       

    // Return an instance of our generated type
    return Activator.CreateInstance( typeBuilder.CreateType() );
}

Web Api Controller Method:

private static IEnumerable<Type> RetrievableEnums = new Type[] {
    typeof(Priority), typeof(Status)
};

[GET("enum/{enumName}")]
public HttpResponseMessage GetEnumInformation( string enumName ) {

    Type enumType = RetrievableEnums.SingleOrDefault( type =>
        String.Equals( type.Name, enumName, StringComparison.InvariantCultureIgnoreCase));

    if ( enumType == null ) {
        return Request.CreateErrorResponse( HttpStatusCode.NotFound, "The requested enum could not be retrieved" );
    }

    return Request.CreateResponse( HttpStatusCode.OK, GetSerializableEnumProxy(enumType) );
}

Any ideas?

like image 714
dherman Avatar asked Nov 30 '12 05:11

dherman


1 Answers

I believe this is ultimately because you're sending the enum value as an object - and unlike the Json formatter, Web API's xml formatter, which uses DataContractSerializer, uses (in effect) the compile-time type of the value being serialized, not the runtime type.

As a result, you must always make sure that any derived types of a base that you're trying to serialize are added to the underlying serializer's known types. In this case you have the dynamic enum (which is an object, of course).

On the face of it, it seems that the SetSerializer(type, serializer) approach should work, however, I'll bet your calling it with the dynamic type as the first argument - which won't work if you are sending the enum out as object - because it's the object serializer that the XmlRequestFormatter will use.

This is a well-known issue - and one which I've reported as an issue on codeplex (there's a good example there which demonstrates the problem in a simpler scenario).

That issue also includes some C# code for an attribute and replacement for XmlMediaTypeFormatter (called XmlMediaTypeFormatterEx) that provides one solution to this problem - it uses a declarative per-operation approach. Replace the XmlMediaTypeFormatter with the one in the code - using something like this (note this code handles the case where there is no XML formatter already defined - perhaps somewhat pointlessly):

var configuration = GlobalConfiguration.Configuration;  
var origXmlFormatter = configuration.Formatters.OfType<XmlMediaTypeFormatter>()
                       .SingleOrDefault();

XmlMediaTypeFormatterEx exXmlFormatter = new XmlMediaTypeFormatterEx(origXmlFormatter);

if (origXmlFormatter != null)
{
    configuration.Formatters.Insert(
      configuration.Formatters.IndexOf(origXmlFormatter), exXmlFormatter);
    configuration.Formatters.Remove(origXmlFormatter);
}
else
    configuration.Formatters.Add(exXmlFormatter);

And now on the API method that you want to return this dynamic enum you'd decorate it with this:

[XmlUseReturnedUnstanceType]
public object Get()
{

}

Now, whatever type you return from the Get method, the custom formatter kicks in and uses a DataContractSerializer specifically for the runtime type, not object.

This doesn't handle enumerables or dictionaries of bases - it gets very complicated then - but for basic single-instance return values it works fine.

like image 165
Andras Zoltan Avatar answered Sep 20 '22 12:09

Andras Zoltan