自托管ASPNET WebAPI中的MediaTypeFormatter问题 [英] MediaTypeFormatter issue in Self-Hosted ASPNET WebAPI

查看:171
本文介绍了自托管ASPNET WebAPI中的MediaTypeFormatter问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在SelfHosted AspNet WebAPI中实现了自定义MediaTypeFormatter.我已经使用Unity.WebApi进行依赖关系解析.控制器类仅知道由模型类实现的接口,而存储库则根据操作提供了具体的模型.

I have implemented a custom MediaTypeFormatter in a SelfHosted AspNet WebAPI. I've used Unity.WebApi for dependency resolution. The controller classes only know about the interfaces implemented by the model classes, whereas, repositories give the concrete models as a result of actions.

自定义MediaTypeFormatter继承自BufferedMediaTypeFormatter,如下所述:

The custom MediaTypeFormatter is inherited from BufferedMediaTypeFormatter as discussed here: http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters.

问题是此媒体类型格式化程序不起作用.即使我用来调试代码,也永远不会碰到ReadFormStream方法.有人知道吗?

The problem is this media type formatter is not working. Even when I use to debug the code, the ReadFormStream method is never hit. Does someone know:

  1. 可能是什么问题?
  2. 我需要告诉统一容器以将接口映射到模型类吗?
  3. 如何在自定义媒体类型格式化程序中获取对依赖关系解析器的引用?

下面是添加格式化程序的代码:

Below is the code to add formatter:



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

下面是EntityController的代码:

Below is the code for the 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的代码:

Below is the code for the 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类的实现:

Below is the implementation of the EntityMediaTypeFormatter class:


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);
        }
    }

在此先感谢您的帮助.

Thanking in advance for any help.

推荐答案

在我看来,现有的格式化程序之一比您的格式化程序具有更高的优先级.在添加您的格式化程序集合之前,请先清除现有的格式化程序集合,或者将您的格式化程序集合插入位置0.

Looks to me like one of the existing formatters is taking priority over yours. Either clear the existing formatter collection before adding yours, or insert yours at position 0.

这篇关于自托管ASPNET WebAPI中的MediaTypeFormatter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆