Search code examples
c#azure-web-app-serviceasp.net-core-6.0

ASP.NET Core 6.0 project works locally, gives System.BadImageFormatException exception when deployed as Azure Web App


Update 1: Added screenshot of publish configuration

I created a new project, wrote out the code and debugged it locally (where it works), but when I deploy it to production, I keep getting the following error:

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.  Could not load file or assembly 'SDKServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.
     at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
     at System.Reflection.RuntimeAssembly.get_DefinedTypes()
     at Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPart.get_Types()
     at Microsoft.AspNetCore.Mvc.Controllers.ControllerFeatureProvider.PopulateFeature(IEnumerable`1 parts, ControllerFeature feature)
     at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateFeature[TFeature](TFeature feature)
     at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetControllerTypes()
     at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors()
     at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
     at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection()
     at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize()
     at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.<>c__DisplayClass11_0.<Subscribe>b__0()
     at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration`1..ctor(Func`1 changeTokenProducer, Action`1 changeTokenConsumer, TState state)     at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)     at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Subscribe()     at Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource..ctor(ControllerActionEndpointDataSourceIdProvider dataSourceIdProvider, IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory, OrderedEndpointsSequenceProvider orderSequence)     at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.GetOrCreateDataSource(IEndpointRouteBuilder endpoints)     at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapControllers(IEndpointRouteBuilder endpoints)
     at Project.Program.Main(String[] args) in C:\Users\user\path\Project\Program.cs:line 65  System.BadImageFormatException: Could not load file or assembly 'SDKServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.  File name: 'SDKServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

Per whatever I could find on SO and other sites, I've tried the following:

  • Full rebuild & redeploy.
  • Ensuring that the "bitness" is the same for the main project & all the other projects it depends on (all target Any CPU).
  • I'm publishing to win-x86 and the Web App's settings (WebApp | Configuration | General Settings) also shows Platform as 32-bit.
  • Tried to delete all files via Kudu console and then re-upload (didn't have permission to delete all, but could delete many of the *.dll files)

The configuration of the project is as follows:

  • ASP.NET Core 6.0

  • The included libraries are as follows:

    Microsoft.Data.SqlClient Version="5.1.2" NLog Version="5.2.5" NLog.Database Version="5.2.5" NLog.Extensions.Logging Version="5.2.5" NLog.Web.AspNetCore Version="5.2.3" Swashbuckle.AspNetCore Version="6.5.0"

Here is the publish configuration I use from Visual Studio:

enter image description here

Nothing I've tried so far has worked, the error remains the same. If it matters, the error happens when app.MapControllers() is invoked in the Main method of Program.cs.


Solution

  • Cleaning out the deployment (deleting any extra files) and changing the target runtime to win-x64 solved the issue (turns out SDKServices.dll was a 64-bit binary).