1

I am currently following the instructions at https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

I am on step "Configure the OData endpoint". I encountered erroneous lines when I added the new code to the endpoint register method.

public static void Register(HttpConfiguration config)
{
    // New code:
    ODataModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Product>("Products");
    config.MapODataServiceRoute(
        routeName: "ODataRoute",
        routePrefix: null,
        model: builder.GetEdmModel());
}

I have tried all instructions (at config.MapODataServiceRoute error) to fix this. Using the suggestions on this site, I found many different ways to eliminate the line errors and get a successful build.

However, no matter which solution I use to get a successful build, when I run the service, I get the following error:

Server Error in '/' Application. Could not load file or assembly 'Microsoft.OData.Core, Version=7.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.OData.Core, Version=7.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Source Error:

Line 12: protected void Application_Start() Line 13: { Line 14: GlobalConfiguration.Configure(WebApiConfig.Register); Line 15: }

Source File: C:\Users<username>\source\repos\ProductService\ProductService\Global.asax.cs Line: 14

Assembly Load Trace: The following information can be helpful to determine why the assembly 'Microsoft.OData.Core, Version=7.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.

WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Stack Trace:

[FileLoadException: Could not load file or assembly 'Microsoft.OData.Core, Version=7.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)] Microsoft.AspNet.OData.Formatter.ODataMediaTypeFormatters.CreateApplicationJson() +0 Microsoft.AspNet.OData.Formatter.ODataMediaTypeFormatters.Create() +28 Microsoft.AspNet.OData.ODataFormattingAttribute.CreateODataFormatters() +5 Microsoft.AspNet.OData.ODataFormattingAttribute.Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) +280 System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +154 System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) +54 System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType) +105 System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache() +400 System.Lazy1.CreateValue() +429 System.Lazy1.LazyInitValue() +158 System.Lazy1.get_Value() +79 System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping() +16 System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider) +48 System.Web.Http.Routing.<>c__DisplayClass1_1.<MapAttributeRoutes>b__1() +57 System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func1 initializer) +61 System.Web.Http.Routing.<>c__DisplayClass1_0.b__0(HttpConfiguration config) +91 System.Web.Http.HttpConfiguration.EnsureInitialized() +23 System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback) +43 ProductService.WebApiApplication.Application_Start() in C:\Users<username>\source\repos\ProductService\ProductService\Global.asax.cs:14

[HttpException (0x80004005): Could not load file or assembly 'Microsoft.OData.Core, Version=7.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +475 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +176 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +220 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +303

[HttpException (0x80004005): Could not load file or assembly 'Microsoft.OData.Core, Version=7.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +659 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +89 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +189

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4331.0

Reza Heidari
  • 1,192
  • 2
  • 18
  • 23

2 Answers2

1

In the process of upgrading a project from .NET 3 to .NET 6, I ran into the same error. First issue I ran into was a value cannot be null exception on EnableDependencyInjection line below.

app.UseEndpoints(routeBuilder =>
{
    routeBuilder.MapControllers();
    routeBuilder.Expand().Select().Count().OrderBy().Filter();
    routeBuilder.EnableDependencyInjection();
});

I resolved that by upgrading the Microsoft.AspNetCore.OData NuGet package from version 7.5.6 to 7.5.15 based on this GitHub issue. That got rid of the null exception but caused a new exception.

"Could not load file or assembly 'Microsoft.OData.Edm, Version=7.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified."

When I downgraded Microsoft.AspNetCore.OData to version 7.5.12 as specified in the GitHub issue, it resolved the error. Not sure why going with the last version 7 release introduced a new error.

John81
  • 3,726
  • 6
  • 38
  • 58
0

I had the same issue and fixed it just by updating OData dependent packages through the Nugget package manager window.

1. Open Package manager

enter image description here

2. Update these packages

enter image description here

3. Rebuild your application