Search code examples
asp.netasp.net-coredependency-injectionodata

Passing parameters to OData service initialization


I got an OData service implemented using .NET core, which has this part in its initialization n the startup.cs file:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  ...
  try
  {
     app.UseMvc(routeBuilder =>
     {
        ...
        routeBuilder.MapODataServiceRoute(
                        routeName: "name",
                        routePrefix: ModelConstants.RoutePrefix,
                        configureAction: containerBuilder =>
                        {
                            ...                
                           containerBuilder.AddService<IODataQueryProcessor>(ServiceLifetime.Singleton, typeof(DefaultODataQueryProcessor));
                        ...
     }

How can I pass parameters here to DefaultODataQueryProcessor which has a constructor with parameters?


Solution

  • According to your description, I suggest you could try below codes :

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
    
                routes.MapODataServiceRoute(
                         routeName: "name",
                         routePrefix :"aaa",
                          configureAction: containerBuilder => {
                              containerBuilder.AddService<IODataQueryProcessor>(Microsoft.OData.ServiceLifetime.Singleton, serviceProvider => {
                                  return new DefaultODataQueryProcessor("someString");
                              } );
                          }
    
                     );
    
            });