Search code examples
c#.netasp.net-coreodata.net-6.0

OData 8.0.10 Could not load type 'Microsoft.AspNet.Odata.MetadataController' upon startup


I recently tried upgrading my OData project from .NET 5.0 to 6.0, requiring an upgrade to OData 8. I went through the docs to make the breaking changes build, but am getting this error at startup. It appears to not know where to obtain the $metadata controller.

At endpoints.MapControllers()

System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types. Could not load type 'Microsoft.AspNet.OData.MetadataController' from assembly 'Microsoft.AspNetCore.OData, Version=8.0.10.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not load type 'Microsoft.AspNet.OData.Routing.Conventions.IODataRoutingConvention' from assembly 'Microsoft.AspNetCore.OData, Version=8.0.10.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not load type 'Microsoft.AspNet.OData.Routing.Conventions.IODataRoutingConvention' from assembly 'Microsoft.AspNetCore.OData, Version=8.0.10.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not load type 'Microsoft.AspNet.OData.Routing.Conventions.SelectControllerResult' from assembly 'Microsoft.AspNetCore.OData, Version=8.0.10.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not load type 'Microsoft.AspNet.OData.Routing.Template.ODataPathTemplate' from assembly 'Microsoft.AspNetCore.OData, Version=8.0.10.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.'

From startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            // Use odata route debug, /$odata
            app.UseODataRouteDebug();

            // Add OData /$query middleware
            app.UseODataQueryRequest();

            // Add the OData Batch middleware to support OData $Batch
            app.UseODataBatching();

            // Test middleware
            app.Use(next => context =>
            {
                var endpoint = context.GetEndpoint();
                if (endpoint == null)
                {
                    return next(context);
                }

                return next(context);
            });

            //app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

        }

        private static IEdmModel GetEdmModel()
        {
            var odataBuilder = new ODataConventionModelBuilder();

            // add models here
            odataBuilder.EntitySet<PERS>("Persons").EntityType.HasKey(x=> x.PERSNBR);
            
// more models omitted
            return odataBuilder.GetEdmModel();
        }

        private void AddFormatters(IServiceCollection services)
        {
            services.AddMvcCore(options =>
            {
             
                foreach(var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_=> _.SupportedMediaTypes.Count() == 0))
                {
                    outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }

                foreach(var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_=> _.SupportedMediaTypes.Count() == 0))
                {
                    inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }

            });

        }
    }`

Solution

  • I was able to get it working by removing the outdated package Microsoft.AspNetCore.OData.Versioning.ApiExplorer