Webapi: [feature/netcore] - Error: The requested service 'Microsoft.AspNet.OData.IPerRouteContainer' has not been registered

Created on 7 Feb 2018  路  7Comments  路  Source: OData/WebApi

I have an interface as follows:

public interface IODataRegistrar
{
    void Register(IRouteBuilder routes, IServiceProvider services);
}

I need this as I have a plugin architecture. I use it as follows in my Startup file:

app.UseMvc(routes =>
{
    foreach (var registrar in registrars)
    {
        registrar.Register(routes, app.ApplicationServices);
    }
    //etc...

Getting the following error:

The requested service 'Microsoft.AspNet.OData.IPerRouteContainer' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

If you haven't guessed it, I am using Autofac for DI, though I don't think that matters here. How can I ensure this IPerRouteContainer is registered?

featurnetcore in-progress question

Most helpful comment

@gordon-matt - #1237 improves the error messages in this scenario. Specifically, you should see an exception that says:

Cannot find '{0}'. The OData services have not been configured. Are you missing a call to AddOData()?

All 7 comments

Note, this actually seems to be happening regardless of my scenario above.. I have another app where I am just using it as follows and still getting the exact same error:

var edmModel = GetEdmModel(app.ApplicationServices);
app.UseMvc(routes =>
{
    routes.MapODataServiceRoute("OData", "odata", edmModel);
    //etc

and GetEdmModel in this case is just as simple as the following:

private static IEdmModel GetEdmModel(IServiceProvider serviceProvider)
{
    var builder = new ODataConventionModelBuilder(serviceProvider);

    builder.EntitySet<Report>("ReportApi");
    builder.EntitySet<ReportGroup>("ReportGroupApi");
    builder.EntitySet<ReportRole>("ReportRoleApi");
    builder.EntitySet<ReportSorting>("ReportSortingApi");
    builder.EntitySet<ReportTable>("ReportTableApi");
    builder.EntitySet<ReportTableColumn>("ReportTableColumnApi");

    return builder.GetEdmModel();
}

@gordon-matt - Do you have this? AddOData() will add the IPerRouteContainer class to the global dependency injection container.

        public void ConfigureServices(IServiceCollection services)
        {
            ....
            services.AddOData();
            ....
        }

Wow, don't know how I missed that. My mistake. Thanks!

No worries. I once heard a story about a tech support person who fielded a call from NASA about a computer issue, which turned out to be the computer was unplugged. Sometimes, it's the simple things we overlook.

Haha, that's hilarious. Thanks for everything. Keep up the great work.

@gordon-matt - #1237 improves the error messages in this scenario. Specifically, you should see an exception that says:

Cannot find '{0}'. The OData services have not been configured. Are you missing a call to AddOData()?

@gordon-matt - #1237 improves the error messages in this scenario. Specifically, you should see an exception that says:

Cannot find '{0}'. The OData services have not been configured. Are you missing a call to AddOData()?

@robward-ms I know this issue is old by now, but I just added latest version of OData for .NetCore and got the more generic registration exception when trying to add a ODataQueryOptions parameter to one of my routes.

I did not know about the AddOData registration extension at the time.

After adding the registration call, I got a new message asking me to "call EnableDependencyInjection() to enable OData components on non-OData routes.".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MDzyga picture MDzyga  路  5Comments

NetTecture picture NetTecture  路  4Comments

kpko picture kpko  路  3Comments

suadev picture suadev  路  3Comments

johnhzhu picture johnhzhu  路  4Comments