1

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

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

问题是这个媒体类型格式化程序不工作。即使我用来调试代码,ReadFormStream 方法也不会被命中。有人知道吗:

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

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

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

下面是 EntityController 的代码:

    公共类 EntityController : ApiController
    {
        私有只读 IEntitiesRepository 存储库 = null;

        公共实体控制器(IEntitiesRepository 存储库)
        {
            如果(存储库 == null)
            {
                throw new ArgumentNullException("repository");
            }

            this.repository = 存储库;
        }

        公共 IEnumerable<IEntity> Get()
        {
            return (IEnumerable<IEntity>)repository.Get();
        }
    }

下面是 EntityRepository 的代码:

    公共类 EntitiesRepository : IEntitiesRepository
    {
        公共 IEnumerable<IEntity> Get()
        {
            返回新实体[]
            {
                新实体
                {
                    Prop1 = "属性 1 的值",
                    Prop2 = "属性 2 的值",
                    Prop3 = "属性 3 的值"
                },
                新实体
                {
                    Prop1 = "属性 1 的值",
                    Prop2 = "属性 2 的值",
                    Prop3 = "属性 3 的值"
                }
            };
        }
        公共 IEntity 获取(长 id)
        {
            返回新实体
                {
                    Prop1 = Convert.ToString(id),
                    Prop2 = "属性 2 的值",
                    Prop3 = "属性 3 的值"
                };
        }
    }

下面是 EntityMediaTypeFormatter 类的实现:

公共类 EntityMediaTypeFormatter : BufferedMediaTypeFormatter
    {
        公共 EntityMediaTypeFormatter()
            : 根据()
        {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        }

        public override bool CanReadType(Type type)
        {
            如果(类型 == 空)
            {
                抛出新的 ArgumentNullException("type");
            }

            如果(类型是 IEntity)
            {
                返回真;
            }
            否则如果(类型。IsGenericType)
            {
                返回 type.GenericTypeArguments.Single(a => a.GetType().Equals(typeof(IEntity))) != null;
            }

            返回假;
        }
        public override bool CanWriteType(Type type)
        {
            如果(类型 == 空)
            {
                抛出新的 ArgumentNullException("type");
            }

            如果(类型是 IEntity)
            {
                返回真;
            }

            返回假;
        }

        public override object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            如果(类型。IsInterface)
            {
                类型 = 获取混凝土类型(类型);
            }

            return base.ReadFromStream(type, readStream, content, formatterLogger);
        }
        public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
        {
            //暂时没有什么特别的...
            base.WriteToStream(类型,值,writeStream,内容);
        }

        私有类型 GetConcreteType(类型类型)
        {
            //TODO: 需要想办法通过DependencyResolver获取具体类型
            返回类型(实体);
        }
    }

提前感谢您的帮助。

4

1 回答 1

1

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

于 2013-06-24T14:09:54.550 回答