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.
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.
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();
}))
};
}
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
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.
Just add a basic code file and copy the content from an earlier controller.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With