【发布时间】:2020-10-13 00:41:14
【问题描述】:
所以我正在构建一个必须通过 OData 使用的服务,我很难弄清楚如何向它添加自定义格式化程序。我需要我的 OData 序列化程序在序列化数据时忽略空值。我创建了这两个来实现这一点:
public class SmartODataSerializerProvider : DefaultODataSerializerProvider
{
private readonly SmartODataEntityTypeSerializer _entityTypeSerializer;
public SmartODataSerializerProvider(IServiceProvider rootContainer)
: base(rootContainer)
{
_entityTypeSerializer = new SmartODataEntityTypeSerializer(this);
}
public override ODataEdmTypeSerializer GetEdmTypeSerializer(Microsoft.OData.Edm.IEdmTypeReference edmType)
{
// Support for Entity types AND Complex types
if (edmType.Definition.TypeKind == EdmTypeKind.Entity || edmType.Definition.TypeKind == EdmTypeKind.Complex)
return _entityTypeSerializer;
else
return base.GetEdmTypeSerializer(edmType);
}
}
和
public class SmartODataEntityTypeSerializer : ODataResourceSerializer
{
public SmartODataEntityTypeSerializer(ODataSerializerProvider provider)
: base(provider) { }
/// <summary>
/// Only return properties that are not null
/// </summary>
/// <param name="structuralProperty">The EDM structural property being written.</param>
/// <param name="resourceContext">The context for the entity instance being written.</param>
/// <returns>The property be written by the serilizer, a null response will effectively skip this property.</returns>
public override Microsoft.OData.ODataProperty CreateStructuralProperty(Microsoft.OData.Edm.IEdmStructuralProperty structuralProperty, ResourceContext resourceContext)
{
var property = base.CreateStructuralProperty(structuralProperty, resourceContext);
return property.Value != null ? property : null;
}
}
这些是在另一个堆栈溢出问题中提供的。但是,当我尝试使用此序列化程序时会出现问题。我已经有一个正在工作的 odata 端点(它只是用 null 序列化所有内容),当我对其应用以下配置时,我不断在没有它的情况下在同一个 EP 上获得“404 Not Found”。
app.UseEndpoints(endpoints =>
{
endpoints.MapODataRoute("odata", "odata", a =>
{
a.AddService(Microsoft.OData.ServiceLifetime.Singleton, typeof(IEdmModel), sp => GetEdmModel(app.ApplicationServices));
a.AddService(Microsoft.OData.ServiceLifetime.Singleton, typeof(ODataSerializerProvider), sp => new SmartODataSerializerProvider(sp));
});
endpoints.EnableDependencyInjection();
//endpoints.MapODataRoute("odata", "odata", GetEdmModel(app.ApplicationServices));
endpoints.MapControllers();
});
这是端点设置。我注释掉了使它工作但没有自定义格式化程序的行。这是设置中使用的 IEdmModel 函数:
private static IEdmModel GetEdmModel(IServiceProvider services)
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder(services);
builder.Namespace = "RS";
builder.EntitySet<PropertyIndexODataModel>("Properties");
builder.EntitySet<ReportResultModel>("Reports");
var function = builder.EntityType<ReportResultModel>().Collection.Function("Generate");
function.Parameter<int>("listId");
function.CollectionParameter<string>("functionsToRun");
function.ReturnsCollectionFromEntitySet<ReportResultModel>("Reports");
return builder.GetEdmModel();
}
因此,当我应用此 odataroute 时,我不断收到 404。当我删除它并返回到 'endpoints.MapODataRoute("odata", "odata", GetEdmModel(app.ApplicationServices));'它可以正常工作。
这似乎是一件非常微不足道的事情,但我到处搜索,但仍然无法正常工作。我正在使用 OData 7.4 和 netcore 3.1。提前致谢!
【问题讨论】:
-
同样的情况。你找到解决办法了吗?
-
@Max Nop。我什至已经在官方存储库上发布过,它已经“调查”了几个月了
标签: asp.net-core odata formatter serialization