自托管ASPNET WebAPI中的MediaTypeFormatter问题

时间:2013-06-24 00:16:53

标签: asp.net-web-api mediatypeformatter

我在SelfHosted AspNet WebAPI中实现了自定义MediaTypeFormatter。我使用Unity.WebApi进行依赖性解析。控制器类只知道模型类实现的接口,而存储库则根据操作给出具体的模型。

自定义MediaTypeFormatter继承自BufferedMediaTypeFormatter,如下所述:http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

问题是此媒体类型格式化程序无法正常工作。即使我用来调试代码,也永远不会遇到ReadFormStream方法。有人知道吗:

  1. 可能出现什么问题?
  2. 我是否需要告诉unity容器将接口映射到模型类?
  3. 如何在自定义媒体类型格式化程序中获取对依赖项解析程序的引用?
  4. 以下是添加格式化程序的代码:

    
    var config = new SelfHostConfiguration("https://xxx.xxx.xxx.xxx:xxxx/");
    config.Formatters.Add(new EntityTypeFormatter());
    
    

    以下是EntityController的代码:

        public class EntityController : ApiController
        {
            private readonly IEntitiesRepository repository = null;
    
            public EntityController(IEntitiesRepository repository)
            {
                if (repository == null)
                {
                    throw new ArgumentNullException("repository");
                }
    
                this.repository = repository;
            }
    
            public IEnumerable<IEntity> Get()
            {
                return (IEnumerable<IEntity>)repository.Get();
            }
        }
    

    以下是EntityRepository的代码:

        public class EntitiesRepository : IEntitiesRepository
        {
            public IEnumerable<IEntity> Get()
            {
                return new Entities[]
                {
                    new Entity
                    { 
                        Prop1 = "value for property 1",
                        Prop2 = "value for property 2",
                        Prop3 = "value for property 3"
                    },
                    new Entity
                    { 
                        Prop1 = "value for property 1",
                        Prop2 = "value for property 2",
                        Prop3 = "value for property 3"
                    }
                };
            }
            public IEntity Get(long id)
            {
                return new Entity
                    {
                        Prop1 = Convert.ToString(id),
                        Prop2 = "value for property 2",
                        Prop3 = "value for property 3"
                    };
            }
        }
    

    以下是EntityMediaTypeFormatter类的实现:

    public class EntityMediaTypeFormatter : BufferedMediaTypeFormatter 
        {
            public EntityMediaTypeFormatter()
                : base()
            {
                SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
                SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            }
    
            public override bool CanReadType(Type type)
            {
                if (type == null)
                {
                    throw new ArgumentNullException("type");
                }
    
                if (type is IEntity)
                {
                    return true;
                }
                else if (type.IsGenericType)
                {
                    return type.GenericTypeArguments.Single(a => a.GetType().Equals(typeof(IEntity))) != null;
                }
    
                return false;
            }
            public override bool CanWriteType(Type type)
            {
                if (type == null)
                {
                    throw new ArgumentNullException("type");
                }
    
                if (type is IEntity)
                {
                    return true;
                }
    
                return false;
            }
    
            public override object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
            {
                if (type.IsInterface)
                {
                    type = GetConcreteType(type);
                }
    
                return base.ReadFromStream(type, readStream, content, formatterLogger);
            }
            public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
            {
                //nothing special for now...
                base.WriteToStream(type, value, writeStream, content);
            }
    
            private Type GetConcreteType(Type type)
            {
                //TODO: Need to find a way to DependencyResolver to get the concrete type
                return typeof(Entity);
            }
        }
    

    提前感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

在我看来,现有格式化程序之一优先于您的格式化程序。在添加之前清除现有格式化程序集合,或在第0位插入您的格式化程序集合。