title: How to Add an Instance Annotation to an Asp.Net ODataFeed (V4)
body: <p>I am using ODataLib 6.6.0 and Microsoft.AspNet.OData 5.2.1 Nuget packages.</p>
  
  <p>I have the following code which creates a custom serializer to add a custom annotation to a paged OData feed.  The code compiles and is called correctly, all the way through the point of adding the custom instance annotation.</p>
  
  <pre><code>namespace WebApiTest.App_Start
  {
      using Microsoft.OData.Core;
      using System.Collections.Generic;
      using System.Web.Http;
      using System.Web.OData.Formatter;
      using System.Web.OData.Formatter.Deserialization;
      using System.Web.OData.Formatter.Serialization;
      using WebApiTest.Controllers;
  
      public class FormatterConfiguration
      {
          public static void Register(HttpConfiguration config)
          {
              var formatters = ODataMediaTypeFormatters.Create(
                  new CustomODataSerializerProvider(),
                  new DefaultODataDeserializerProvider());
              config.Formatters.InsertRange(0, formatters);
          }
      }
  
      public class CustomODataSerializerProvider : DefaultODataSerializerProvider
      {
          private readonly CustomODataFeedSerializer _feedSerializer;
  
          public CustomODataSerializerProvider()
          {
              _feedSerializer = new CustomODataFeedSerializer(this);
          }
  
          public override ODataEdmTypeSerializer GetEdmTypeSerializer(Microsoft.OData.Edm.IEdmTypeReference edmType)
          {
              var serializer = base.GetEdmTypeSerializer(edmType);
              var feedSerializer = serializer as ODataFeedSerializer;
              if (feedSerializer != null)
              {
                  return _feedSerializer;
              }
              return serializer;
          }
      }
  
      public class CustomODataFeedSerializer : ODataFeedSerializer
      {
          public CustomODataFeedSerializer(ODataSerializerProvider serializerProvider)
              : base(serializerProvider)
          {
          }
  
          public override Microsoft.OData.Core.ODataFeed CreateODataFeed(System.Collections.IEnumerable feedInstance, Microsoft.OData.Edm.IEdmCollectionTypeReference feedType, ODataSerializerContext writeContext)
          {
              ODataFeed feed = base.CreateODataFeed(feedInstance, feedType, writeContext);
              var result = feedInstance as IPageResultCustom;
              if (result != null)
              {
                  var value = new ODataComplexValue();
                  var properties = new List&lt;ODataProperty&gt;();
                  properties.Add(new ODataProperty() { Name = "CustomProperty", Value = result.CustomProperty });
                  value.Properties = properties;
                  feed.InstanceAnnotations.Add(new ODataInstanceAnnotation("org.CustomComplexValue", value));
              }
              return feed;
          }
      }
  }
  </code></pre>
  
  <p>I issue a request to a feed including the HTTP headers <code>Prefer: odata.include-annotations="*"</code> and <code>Content-Type=application/json</code> in the request. Yet, I get no annotations in the response.</p>
  
  <p>Tracing through the OData framework code, the internal property <code>Func&lt;string,bool&gt; Microsoft.OData.Core.MessageWriterSettings.ShouldInludeAnnotations</code> is never set when creating the <code>ODataMessageWriters</code> and therefore, the <code>ODataJsonLightValueSerializer.ShouldSkipAnnotation(string name)</code> method call always returns true.</p>
  
  <p>It seems like a bug in the framework.  Am I missing something in my configuration or request?</p>
  
  <p><strong>Edit Aug 7, 2014</strong></p>
  
  <p>The issue appears to be related to the bug <a href="https://aspnetwebstack.codeplex.com/workitem/1880" rel="nofollow">https://aspnetwebstack.codeplex.com/workitem/1880</a>.</p>
  
  <p>A workaround was suggested that involved creating a custom <code>ODataMediaTypeFormatter</code>.  However, <code>ODataMediaTypeFormatters.Create</code> is not generic and recreating it involves too many dependencies that are internally scoped.  This workaround is a daunting task at a minimum.   It appears that the realistic limit of extensibility on <code>ODataMediaTypeFormatter</code> is confined to implementing custom serializers and/or deserializers.</p>