如何向AspNet.OData v6注册OData序列化程序提供程序 [英] How to register an OData Serializer Provider with AspNet.OData v6
问题描述
Microsoft.AspNet.Odata更改了注册序列化程序等服务的方式.新格式应该是什么样?
Microsoft.AspNet.Odata changed the way you register services like serializers. What should the new format look like?
推荐答案
您的新配置应如下所示.您需要至少添加前两个服务(IEdmModel和IEnumerable IODataRoutingConvention),然后可以添加自己的服务.例如,自定义的odata序列化程序提供程序.
Your new config should look something like this. You need to add at least the first 2 services (IEdmModel and IEnumerable IODataRoutingConvention), and then you can add your own. For example, a custom odata serializer provider.
using Boomerang.OData;
using Boomerang.Models;
using Microsoft.OData;
using Microsoft.OData.Edm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
using System.Web.OData.Formatter;
using System.Web.OData.Formatter.Serialization;
using System.Web.OData.Routing.Conventions;
namespace Boomerang
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.MapODataServiceRoute(
"ODataRoute",
"api",
containerBuilder => containerBuilder
.AddService<IEdmModel>(ServiceLifetime.Singleton, s => BuildEdmModelForOData())
.AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp =>
ODataRoutingConventions.CreateDefaultWithAttributeRouting("ODataRoute", config))
.AddService<ODataSerializerProvider>(ServiceLifetime.Singleton, s => new CustomODataSerializerProvider(s))
);
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
}
public static IEdmModel BuildEdmModelForOData()
{
ODataConventionModelBuilder
oDataConventionModelBuilder = new ODataConventionModelBuilder();
// Build model here
oDataConventionModelBuilder.EntitySet<Post>("Posts");
oDataConventionModelBuilder.EntitySet<Feeling>("Feelings");
return oDataConventionModelBuilder.GetEdmModel();
}
}
}
我添加此答案是因为大多数在线指南都提供了6之前版本的说明.可以在以下位置找到此信息:
I'm adding this answer because most of the guides online give instructions for versions before 6. This information can be found here: http://odata.github.io/WebApi/#13-04-DependencyInjection
这篇关于如何向AspNet.OData v6注册OData序列化程序提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!