[英]Mapping dynamic odata routes with ASP.NET Core OData 8.0
我有一个应用程序,其中 EDM 数据类型是在应用程序运行时生成的(它们甚至可以在运行时更改)。 松散地基于OData DynamicEDMModelCreation Sample - 重构为使用新的端点路由。 在那里,EDM model 在运行时动态生成,所有请求都转发到相同的 controller。
现在我想更新到最新的ASP.NET Core OData 8.0并且整个路由发生了变化,因此当前的解决方法不再起作用。
我已经阅读了更新Blog1 Blog2的两篇博客文章,似乎我不能再使用“旧”解决方法了,因为端点内的 function MapODataRoute()现在已经消失了。 似乎没有任何内置路由约定适用于我的用例,因为所有这些都要求 EDM model 在调试时出现。
也许我可以使用自定义IODataControllerActionConvention 。 我试图通过将约定添加到路由约定来激活约定,但似乎我仍然缺少如何激活它的部分。
services.TryAddEnumerable(
ServiceDescriptor.Transient<IODataControllerActionConvention, MyEntitySetRoutingConvention>());
这种方法是否有效? 甚至可以在新的 odata 预览中激活动态 model 吗? 或者有人知道如何为新的 odata 8.0 处理动态路由吗?
因此,经过 5 天的内部 OData 调试后,我设法让它工作。 以下是必要的步骤:
首先从您的 controller 中删除所有 OData 调用/属性,这可能会做一些时髦的事情( ODataRoutingAttribute
或AddOData()
)
创建一个简单的 asp.net controller 与您喜欢的路线和 map 它在端点
[ApiController]
[Route("odata/v{version}/{Path?}")]
public class HandleAllController : ControllerBase { ... }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory)
{
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
}
}
创建并注册您的 InputFormatWrapper 和 OutputFormatWrapper
public class ConfigureMvcOptionsFormatters : IConfigureOptions<MvcOptions>
{
private readonly IServiceProvider _services;
public ConfigureMvcOptionsFormatters(IServiceProvider services)
{
_services = services;
}
public void Configure(MvcOptions options)
{
options.InputFormatters.Insert(0, new ODataInputFormatWrapper(_services));
options.OutputFormatters.Insert(0, new OdataOutputFormatWrapper(_services));
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.ConfigureOptions<ConfigureMvcOptionsFormatters>();
...
}
public class ODataInputFormatWrapper : InputFormatter
{
private readonly IServiceProvider _serviceProvider;
private readonly ODataInputFormatter _oDataInputFormatter;
public ODataInputFormatWrapper(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
//JSON and default is first - see factory comments
_oDataInputFormatter = ODataInputFormatterFactory.Create().First();
}
public override bool CanRead(InputFormatterContext context)
{
if (!ODataWrapperHelper.IsRequestValid(context.HttpContext, _serviceProvider))
return false;
return _oDataInputFormatter.CanRead(context);
}
public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
return _oDataInputFormatter!.ReadRequestBodyAsync(context);
}
}
// The OutputFormatWrapper looks like the InputFormatWrapper
在ODataWrapperHelper
,您可以检查内容并获取/设置您的动态 edmModel。 最后有必要设置这些ODataFeature()
......这并不漂亮,但它完成了动态工作......
public static bool IsRequestValid(HttpContext context, IServiceProvider serviceProvider)
{
//... Do stuff, get datasource
var edmModel = dataSource!.GetModel();
var oSegment = new EntitySetSegment(new EdmEntitySet(edmModel.EntityContainer, targetEntity, edmModel.SchemaElements.First(x => targetEntity == x.Name) as EdmEntityType));
context.ODataFeature().Services = serviceProvider.CreateScope().ServiceProvider;
context.ODataFeature().Model = edmModel;
context.ODataFeature().Path = new ODataPath(oSegment);
return true;
}
现在到丑陋的东西:我们仍然需要在ConfigureServices(IServiceCollection services)
中注册一些 ODataService 添加一个AddCustomODataService(services)
并自己注册〜40个服务或做一些时髦的反射......
因此,如果 odata 团队的某个人读到此内容,请考虑打开Microsoft.AspNetCore.OData.Abstracts.ContainerBuilderExtensions
我创建了一个public class CustomODataServiceContainerBuilder: IContainerBuilder
这是内部Microsoft.AspNetCore.OData.Abstracts.DefaultContainerBuilder
的副本,我在那里添加了 function:
public void AddServices(IServiceCollection services)
{
foreach (var service in Services)
{
services.Add(service);
}
}
和丑陋的AddCustomODataServices(IServiceCollection services)
private void AddCustomODataService(IServiceCollection services)
{
var builder = new CustomODataServiceContainerBuilder();
builder.AddDefaultODataServices();
//AddDefaultWebApiServices in ContainerBuilderExtensions is internal...
var addDefaultWebApiServices = typeof(ODataFeature).Assembly.GetTypes()
.First(x => x.FullName == "Microsoft.AspNetCore.OData.Abstracts.ContainerBuilderExtensions")
.GetMethods(BindingFlags.Static|BindingFlags.Public)
.First(x => x.Name == "AddDefaultWebApiServices");
addDefaultWebApiServices.Invoke(null, new object?[]{builder});
builder.AddServices(services);
}
现在 controller 应该再次工作(使用 odataQueryContext 和序列化) - 示例:
[HttpGet]
public Task<IActionResult> Get(CancellationToken cancellationToken)
{
//... get model and entitytype
var queryContext = new ODataQueryContext(model, entityType, null);
var queryOptions = new ODataQueryOptions(queryContext, Request);
return (Collection<IEdmEntityObject>)myCollection;
}
[HttpPost]
public Task<IActionResult> Post([FromBody] IEdmEntityObject entityDataObject, CancellationToken cancellationToken)
{
//Do something with IEdmEntityObject
return Ok()
}
这里有一个动态路由和动态 model 的示例:
https://github.com/OData/AspNetCoreOData/blob/master/sample/ODataDynamicModel/请参阅MyODataRoutingApplicationModelProvider
和MyODataRoutingMatcherPolicy
,它们会将自定义 IEdmModel 传递给 controller。
HandleAllController
可以动态处理不同类型和 edm 模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.