Error System.BadImageFormatException service fabric - azure

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
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>

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
From source here
Edit 1:
Service febric targets x64 bit then Click the Platform dropdown and choose New, then create an x64 platform configuration.

Related

Azure DevOps Invalid token '=>' in class, struct, or interface member declaration

Oh, the most annoying thing ever :( So I spent some time trying to get my old application to compile using c# 7 and managed it, now that is finally working I was ready to setup devops.
I created my pipeline as usual, but the first issue was when I pushed a build, it moaned about not supporting .net 4.7.2. Awesome.
So I downgraded the project to 4.6.2 and then ran my pipeline again and now I get this:
Invalid token '=>' in class, struct, or interface member declaration
I have checked the nuget restore in devops and it is using .net compilers 2.10.0.
I am at a loss. Does anyone know how I can fix this?
Your Agent pool may need to be Hosted VS2017.
As #NicoD mentioned, we also are building c# 7 projects with no issues by targeting this host.
For other users who have custom/hosted agents, you have to install this in server where agent runs:
Be sure you have correct Build Tools for Visual Studio installed: How can I install the VS2017 version of msbuild on a build server without installing the IDE?
And don't forget install this option too: MS-Build 2017 "Microsoft.WebApplication.targets " is missing

Azure Mobile Services - System.PlatformNotSupportedException

i'm working on a xamarin forms app with azure however when I load my initial page it crashes on the line:
public static MobileServiceClient MobileService =
new MobileServiceClient(
"https://myapp.azurewebsites.net");
with exception:
System.PlatformNotSupportedException "The empty PCL implementation for
Microsoft Azure Mobile Services was loaded. Ensure you have added
nuget package to each of your platform projects."
How do I fix this?
It might mean that the NuGet packages did not install correctly in one of your platforms. I would suggest doing a force-reinstall of all NuGet packages using the Package Manager Console (Tools/NuGet Package Manager/Package Manager Console).
Update-Package -reinstall
Do this for all projects in your solution. Once done, close and reopen Visual Studio, open your solution and click Build in the top menu and do Clean Solution.
Now look into each of your platform projects in turn to see if you have a Microsoft.Azure.Mobile.Client reference there:
If it is not there, install the Microsoft.Azure.Mobile.Client package again in that given project.
Then you should proceed as described in any tutorial on Azure Mobile Services. Even if you use the Shared Project strategy in you Xamarin.Forms app, it should work as expected once the platform project has Microsoft.Azure.Mobile.Client installed.
If it does not help, as the last resort I would suggest trying to update all NuGet packages to their last version (right-click solution, Manage NuGet Packages for Solution...)
Make sure that you add the initialization code in the respective AppDelegate/MainActivity
CurrentPlatform.Init();
And that in the iOS, Android and PCL projects there is the reference to the Microsoft.Azure.Mobile.Client package.
Example
It instantiates the MobileServiceClient in the TodoItemManager class.

How to create a x64 (instead of AnyCPU) build of .Net based Azure Cloud Service?

I'm using Azure 2.7.
I created a C# cloud service that only contains a single worker role. After it's done, the solution contains
A ccproj
A csproj
Both are using the setting of AnyCPU.
I then added the PropertyGroup "Debug|x64" and "Release|x64" into both projects. Also changed the default to x64 for both. As a result, an x64 build.
However, whenever I select "publish" on the "ccproj", I notice that it always builds AnyCPU instead of x64. I cannot find how to force it to build x64 for packaging and deployment.
I then edited both proj file again, and removed the PropertyGroup for "Debug|AnyCPU" and "Release|AnyCPU". After I did this, and when I tried to build x64 again, I got error as:
C:\Program Files
(x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(723,5):
error : The OutputPath property is not set for project
'AzureWorkerRoleExample.ccproj'. Please check to make sure that you
have specified a valid combination of Configuration and Platform for
this project. Configuration='Release' Platform='AnyCPU'. This error
may also appear if some other project is trying to follow a
project-to-project reference to this project, this project has been
unloaded or is not included in the solution, and the referencing
project does not build using the same or an equivalent Configuration
or Platform.
I could not figure out how to work around this error.
Any idea on how to force VS to make a x64 build of cloud service and deploy it?
One workaround I found is that: keep AnyCPU property groups, but also put
<PlatformTarget>x64</PlatformTarget>
in the group. Thus the AnyCPU assembly are actually targetting to x64. But this sounds hacky.
Any idea?
Thanks a lot!

From where do I reference a missing assembly (System.Net.Http.Formatting)?

In my Visual Studio 2013 RC project, I'm getting this err msg:
"The type 'System.Net.Http.Formatting.MediaTypeFormatter' is defined
in an assembly that is not referenced. You must add a reference to
assembly 'System.Net.Http.Formatting, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35'"
...on the "var response = " line of code in a Controller:
public HttpResponseMessage PostDelivery(Delivery delivery)
{
delivery = repository.Add(delivery);
var response = Request.CreateResponse<Delivery>(HttpStatusCode.Created, delivery);
string uri = Url.Link("DefaultApi", new { id = delivery.Id });
response.Headers.Location = new Uri(uri);
return response;
}
Searching my hard drive for the required assembly ("System.Net.Http.Formatting"), it shows me several locations where it exists, but they are all within existing projects, as is seen here:
Surely there is another location from which System.Net.Http.Formatting.dll can be referenced! But where?
UPDATE
Trying to follow the clue here: http://www.nuget.org/packages/System.Net.Http.Formatting
IOW, by selecting Tools > Library Package Manager > Package Manager Console, and entering:
Install-Package System.Net.Http.Formatting -Version 4.0.20710
...I see the red bulls of Pamplona (not the beverage) descending wild-eyed upon me:
UPDATE 2
I added the System.Net.Http.Formatting.dll from here:
C:\Users\clay\My Documents\Visual Studio 2013\Projects\MvcApplication1\MvcApplication1\bin
...and it now compiles and runs and works as expected, but that is obviously "not the preferred method"
I had the same problem. I used Microsoft Asp Net WebApi Client 4.0.20710 and it worked for me.
Type the following command into Tools > Library Package Manager > Package Manager Console:
Install-Package Microsoft.AspNet.WebApi.Client -Version 4.0.20710
Adding to what Luiz answered above.
System.Net.Http.Formatting is a library and is used for media type formatting. For example:
http://msdn.microsoft.com/en-us/library/system.net.http.formatting.jsonmediatypeformatter(v=vs.118).aspx
When referencing it, when I tried, I was looking to add a reference in my project and was looking in the Assemblies/Framework folder in Reference Manager.
It is actually in the Assemblies/Extensions folder in Reference Manager
If you got an error trying to install System.Net.Http.Formatting -Version 4.0.20710, try this one:
Install-Package WebApiDoodle.Net.Http.Formatting -Pre
I just found on NuGet: https://www.nuget.org/packages/WebApiDoodle.Net.Http.Formatting/3.0.0-pre01
Right click on project and click Add a reference option, then go to Extensions from
Assemblies tab and choose System.Net.Http.Formatting.
If it is no in here you have to manually add in prgramme files-> Microsoft ASP.NET
One other simple option for resolving this is to right click on references, click 'Manage NuGet References', Browse for 'Microsoft.AspNet.WebApi.Client' and install it or perhaps reinstall it. Review the package information to learn more.
I ran into the same problem on vs2022 v16.11.5.
I was fortunate enough to have the project working on one machine but not another. On the suspect machine, under project References, System.Net.Http.Formatting could not be found. Running the visual studio installer app and modifying the vs2022 install to include the ASP.NET and web development package fixed me up.
ASP.NET and Web development Install image

create azure package with build

I was just upgrading my project from sdk 1.3 to 1.7 and I noticed that now when I build the application the package(cspkg) is not created with the build. I have go click on publish to create a package. Is there a way to tell VS to create a package everytime a build is triggered.
Also using msbuild is there a way to do the same thing. I have multiple projects under a solution, Most of which are just libraries and then there is this azure app. Is there a way to specify a single msbuild statement with params to tell the azure app to create the package as well as build the other projects. Also when I specify debug the debug package should be created and when I specify release switch the release package should be created.
How could I do the same thing on my build server as well where I have a .proj file which specifies the sln to build. How could I mention a switch to build the package there.
Thanks,
Kunal
You can configure CSPACK command (Be sure to have CSPACK.exe launched from SDK 1.7 Path otherwise you will get some schema related errors with SDK 1.6 project) as Post Build event in your Windows Azure Application Build settings. This way when you will build, after successful build CSPACK command will run and package your application. Same way you can configure your MSBuild configuration. I just tested and it worked for me.
Visit this MSDN article on packaging a cloud service to learn more.
You can do this using msbuild as well. See the Resolution section of this question.

Resources