7

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();
                    }))
            };
        }
Community
  • 1
  • 1
Malik Kashmiri
  • 5,741
  • 11
  • 44
  • 80
  • 2
    Make sure all dependencies are 64 bit, as Service Fabric only supports 64 bits applications. – Peter Bons Feb 13 '18 at 07:22
  • 2
    Possible duplicate of [Service Fabric System.BadImageFormatException](https://stackoverflow.com/questions/42307850/service-fabric-system-badimageformatexception) – Peter Bons Feb 13 '18 at 07:23
  • @PeterBons all projects are x64, as for making sure that the nuget packages are x64, i woudlnt know how to do – Alex Gordon Feb 20 '18 at 15:45

4 Answers4

4

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

Diego Mendes
  • 10,631
  • 2
  • 32
  • 36
  • how do i ensure that the dotnet core tools are x64? – Alex Gordon Feb 20 '18 at 15:45
  • Probably the easiest way to check is verifying the path on Environment variables. Go to your command promp(cmd) and type path, it will list the paths for installed softwares, dotnet will be one of them. if it is x86, it will be installed on "c:\program files (x86)\dotnet". if it is x64, it will be installed on "c:\program files\dotnet". If you have both, Go to environment variables and reorder them to have x64 first. Environement variable is located on: Right Click on "This Pc" > Properties > Advanced System settings > Environment Variables > System Variables. – Diego Mendes Feb 20 '18 at 16:24
  • neither one is installed – Alex Gordon Feb 21 '18 at 15:17
3

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

Satya Tanwar
  • 1,118
  • 6
  • 11
1

To fix it, simply follow the steps below:

  • open project properties window.
  • select Build tab
  • Change it to ‘Any CPU’
  • Save your changes.
  • Compile your project and run

    enter image description here

Edit 1:

Service febric targets x64 bit then Click the Platform dropdown and choose New, then create an x64 platform configuration.

1

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>
José Alonso
  • 103
  • 1
  • 1
  • 9