Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error System.BadImageFormatException service fabric

Problem

I am building service fabric application. When I create a Project and run it its working fine. but when I inject a service in the Api controller it gives me this error I tried to resolved it but not succeeded yet.

Error

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.

Image

enter image description here

I add the service

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
        {
            return new ServiceInstanceListener[]
            {
                new ServiceInstanceListener(serviceContext =>
                    new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                    {
                        ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                        return new WebHostBuilder()
                                    .UseKestrel()
                                    .ConfigureServices(
                                        services => services
                                            .AddSingleton<StatelessServiceContext>(serviceContext)
                                            .AddScoped(typeof(ISendMessage), typeof(SendMessage))
                                            )
                                    .UseContentRoot(Directory.GetCurrentDirectory())
                                    .UseStartup<Startup>()
                                    .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                    .UseUrls(url)
                                    .Build();
                    }))
            };
        }
like image 432
Malik Kashmiri Avatar asked Feb 13 '18 06:02

Malik Kashmiri


3 Answers

It is likely that your service or any of their dependencies is targeting a x86 platform.

To fix that, you have to force your service running on x64 and/or replace any x86 dependencies for x64.

If your are running dotnet-core, make sure the x64 tools are installed as well.

You might also try removing the reference to Microsoft.VisualStudio.Web.CodeGeneration.Design from your project as mentioned here

These SO questions might give you more information:

service fabric system badimageformatexception

badimageformatexception when migrating from aspnet core 1.1 to 2.0

like image 130
Diego Mendes Avatar answered Nov 16 '22 01:11

Diego Mendes


This was an issue we faced sometime ago, It comes when you try to add asp.net core controller. For some reason visual studio adds the reference to "Microsoft.VisualStudio.Web.CodeGeneration.Design".

My sugegstion would be to remove the reference and also not add controller using the Project->Add->Controller. enter image description here

Just add a basic code file and copy the content from an earlier controller. enter image description here

enter image description here

like image 26
Satya Tanwar Avatar answered Nov 16 '22 01:11

Satya Tanwar


Add more binaries for the package, and do AnyCPU for the platform target.

<PackageId>Microsoft.VisualStudio.Web.CodeGeneration.Design</PackageId>
 <RuntimeIdentifier Condition=" '$(TargetFramework)' != 'netcoreapp2.0' ">win7-x86</RuntimeIdentifier>
 <RuntimeIdentifier Condition=" '$(TargetFramework)' != 'netcoreapp2.0' ">win7-x64</RuntimeIdentifier>
like image 22
José Alonso Avatar answered Nov 16 '22 02:11

José Alonso