ken2k
ken2k

Reputation: 48985

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.

Upvotes: 9

Views: 4401

Answers (3)

user3314553
user3314553

Reputation: 61

I was getting this error too on SmarterASP.NET hosting. I couldn't try all the file updates suggest here, but found another solution, prior to which I tried compiling to several targets without success. Finally, it all just started working when I targeted for Portable. On Smarter it is now running nicely as a Framework Dependent Deployment, ASP 2.2 Core, Portable. Sometimes I find getting Core running is like having to get out the ol' trial-and-error chops. As always, your mileage may vary.

Upvotes: 0

Prince Owen
Prince Owen

Reputation: 1505

There's a chance that the dependency is corrupted. You can use a tool like Everything (https://filehippo.com/download_everything/) to search and find the corrupt dependency. There's a good chance it will be somewhere in this folder (C:\Program Files\dotnet\shared). Once you find it, try to google and redownload it(there's a good chance, you'll find a nuget for it) and then replace the old\corrupt version (be sure to back up).

If you're having trouble finding a replacement for the dependency online, simply publish your project on to a folder, all the dependencies would be copied to that folder. If you publish your project, and you run dotnet PROJECTNAME.dll and you still get the same error, you might have to run a clean install or repair on Visual Studio.

I hope this saves someone a lot of time, this error can take a while to debug.

Upvotes: 0

David Urting
David Urting

Reputation: 403

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)

Upvotes: 7

Related Questions