8

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.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • Curious, what happens if you cut out the config and just do `services.AddMvc();`? – DavidG Aug 21 '17 at 15:45
  • Also, in your csproj file, which version of `Microsoft.VisualStudio.Web.CodeGeneration.Design` do you have in there? (hopefully 2.0.0) – DavidG Aug 21 '17 at 15:49
  • @DavidG I have the same exception with the default config (`services.AddMvc();`). I do have the 2.0.0 version of `Microsoft.VisualStudio.Web.CodeGeneration.Design` in my csproj. – ken2k Aug 21 '17 at 16:00
  • Oh well, was worth a try. Perhaps you could reinstall that package then, or check it's dependencies to make sure they are up to date too. – DavidG Aug 21 '17 at 16:02
  • @DavidG Yes, was worth a try! I tried reinstalling the package too, no luck either :/ – ken2k Aug 21 '17 at 16:08
  • Cleared nuget cache & bin/obj folders? Also make sure you don't have `x86` in your .csproj (had that once, the other was wrong `` – Tseng Aug 21 '17 at 19:17
  • @Tseng No x86 reference anywhere in my csproj :/ Clearing the cache/rebuilding didn't help :/ – ken2k Aug 22 '17 at 12:13

3 Answers3

6

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)

David Urting
  • 383
  • 1
  • 7
  • I removed the reference too, this is the only workaround that works for now. That said my project has always targeted AnyCPU, so I'm not sure where this comes from. Looks indeed like a bug. – ken2k Aug 22 '17 at 15:25
0

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.

Prince Owen
  • 1,225
  • 12
  • 20
0

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.