I am using OData in an ASP.NET Core (.NET 6) web app
At first, I needed a way to output CSV (for data exporting feature) and found this great guide, it worked by doing so in Program.cs, the magic is adding the 3rd parameter service => service.AddSingleton<ODataMediaTypeResolver>(sp => new CustomizedMediaTypeResolver()))
for using the service configuration:
builder.Services
.AddControllers()
.AddOData(options => options
.EnableQueryFeatures()
.AddRouteComponents(
"odata/v1",
Startup.GetEdmModel(),
service => service.AddSingleton<ODataMediaTypeResolver>(sp => new CustomizedMediaTypeResolver()))
.Select()
.Filter()
.OrderBy()
.Count()
);
Then I need to activate the batching OData feature as described in many places for example here. It's also making use of an additional parameter in the AddRouteComponents
function, this time new DefaultODataBatchHandler()
. For example:
builder.Services
.AddControllers()
.AddOData(options => options
.EnableQueryFeatures()
.AddRouteComponents(
"odata/v1",
Startup.GetEdmModel(),
new DefaultODataBatchHandler())
.Select()
.Filter()
.OrderBy()
.Count()
);
I would like to call AddRouteComponents
and make use of both the DefaultODataBatchHandler
as well as the service configuration for registering the ODataMediaTypeResolver
.
One is using this method and the other that method, but I don't see a way to use a mix of the 2 :(
Am I missing something?
The source codes
public ODataOptions AddRouteComponents(string routePrefix, IEdmModel model, ODataBatchHandler batchHandler)
{
return AddRouteComponents(routePrefix, model, services => services.AddSingleton(sp => batchHandler));
}
has indicates:
.AddRouteComponents(
"odata/v1",
Startup.GetEdmModel(),
new DefaultODataBatchHandler())
is same with ( add DefaultODataBatchHandler() to IServiceCollection )
.AddRouteComponents(
"odata/v1",
edmModel,
services=> {
services.AddSingleton(sp => new DefaultODataBatchHandler());
})
If you want to mix them up,have you tried :
.AddRouteComponents(
"odata/v1",
edmModel,
services=> {
services.AddSingleton(sp => new DefaultODataBatchHandler());
services.AddSingleton<ODataMediaTypeResolver>(sp => new CsvMediaTypeResolver());
})