Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BadImageFormatException when migrating from ASP.Net Core 1.1 to 2.0

I just migrated an ASP.Net Core 1.1 application to the new 2.0 version that was just released. Now I get the following exception:

System.BadImageFormatException: 'Could not load file or assembly 'dotnet-aspnet-codegenerator-design' or one of its dependencies. An attempt was made to load a program with an incorrect format.'

The exception is thrown on the following line (AddMvc):

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMvc(options =>
        {
            options.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline)));
        })
        .AddJsonOptions(options =>
        {
            // Maintain property names during serialization. See:
            // https://github.com/aspnet/Announcements/issues/194
            options.SerializerSettings.ContractResolver = new DefaultContractResolver();
        })
        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddDataAnnotationsLocalization();
}

nasty exception screenshot

I'm targeting the .Net Framework 4.7, using AnyCPU target platform. I'm using the win10-x64 RID if this is of any help. Every Nuget package is up to date etc.

Any idea? I've got no luck by googling.

like image 320
ken2k Avatar asked Aug 21 '17 15:08

ken2k


People also ask

Is ASP.NET Core still relevant 2021?

After a strong legacy of over two decades now, the net development services still remain relevant. As per a report by w3techs, ASP.NET is still used by 7.9% of all the websites whose server-side programming languages are known.

Should I migrate to ASP.NET Core?

Migrating existing ASP.NET apps to ASP.NET Core has many advantages, including better performance, cross-platform support (Windows, macOS, Linux), and access to all the latest improvements to the modern . NET web platform.

What is BadImageFormatException?

BadImageFormatException error often occurs when a 64-bit assembly is opened by a 32-bit application. In this case, Visual Studio is a 32-bit application and the Design view is attempting to load a 64-bit assembly. Visual Studio assemblies are located in the project references tree.


1 Answers

the same exception occurred when I switched from x86 to x64 (after upgrading from core 1.1 to 2.0).

Since dotnet-aspnet-codegenerator-design is not really needed at runtime, I removed that reference. But then an identical exception occurred with the Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv assembly.

Luckily, that problem was related to the fact I didn't change the .NET Core Runtime Identifier (RID). This was still set on win7-x86, so I changed it to win7-x64 and the Libuv devepency could load.

I hope there will be some fix for dotnet-aspnet-codegenerator-design since that dependency is probably necessary for scaffolding controllers and views...

Update: there is an issue for this on Github (https://github.com/aspnet/Scaffolding/issues/601)

like image 135
David Urting Avatar answered Oct 22 '22 14:10

David Urting