System.IO.FileLoadException on System.ServiceModel.Primitives - c#

I am currently working on a Xamarin project, and I required something that Xamarin on it's own doesn't support, so I looked for solutions. After messing around with some NuGets, my project ended up with a bunch of weird and unexplainable compile errors, popping up and disappearing randomly.
Eventually I realized that I removed a NuGet from the shared project, but not the UWP one. After removing it from UWP, the program compiles and runs. But seems that there is some more damage done...
However, when I try to call a method of a class defined in a different file, it throws a System.IO.FileLoadException, which it previously wasn't doing. The complete Exception message is:
System.IO.FileLoadException: 'Could not load file or assembly 'System.ServiceModel.Primitives, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'
After a bit of digging, it concluded that it is probably caused by a missing nugget, which I must've deleted by a mistake. So I installed a 4.5.3 version. Didn't help. Restarted Visual Studio, no effect.
I can't install any earlier versions, as it is considered being a downgrade by something. Any clues how to deal with this?

I had the same problem in my UWP project.
My solution was to remove checking if the exception is a FaultException. It seems like FaultException is not supported. After removing the check my method works fine, I could step in etc. Hopefully this helps you too and solves your problem.

My case it solved by update the System.ServiceModel.Primitives for .NET Standard to 4.4.4. version as mentioned below,
https://github.com/dotnet/wcf/issues/3088#issuecomment-411603736

Related

Using a reference which references a 3rd-party 32-bit C dll on a Nancy Projects causes System.BadImageFormatException

I have a Nancy project on .NET 4.8 in C# which was giving the error below on this line:
host = new NancyHost(new HttpBootstrapper(), hostConfigs, uri);
System.BadImageFormatException: Could not load file or assembly [name of assembly] The
module was expected to contain an assembly manifest
The assembly referenced is a 3rd-party 32-bit C DLL which one of my C# DLL's was referencing. All other projects using this DLL worked fine. Just the Nancy Project was not happy.
Now this has happened before and I had forgotten how to solve it. Thus, I'm writing and answering my own post on this. It's a hard error to solve because BadImageFormatException happens all over the place for many very different reasons.
Here is the solution that apparently I've been using for years, which only crops up when I reset my Exception settings in Visual Studio: simply Ignore those exceptions!
Just clear the checkbox "Break when this exception type is thrown". Everything will work perfectly. I have no idea what the underlying issue is, no idea if the problem is important or not. But this solution works perfectly find and lets me get to the 10,000 other issues that I need to solve on a daily basis.
I think it's related to old C DLLS being called by C#. It would be interesting to understand how to fix it, but that is not mission critical.

Dependency error (System.Net.Http) when using Azure deployment options - works when publishing in Visual Studio

I have a very weird situation I hope someone can help with!
I have a normal ASP.NET MVC website. I've set up "Deployment Options" to push to a domain on every Develop commit. However, this has started giving the dependency error:
Could not load file or assembly 'System.Net.Http' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Now, I thought this was some caching since solution worked locally. So I have been publishing the app inside Visual Studio a couple of times (with option of deleting all remote files), and then the solution works.
However, after doing this, and I go back to a normal commit with Deployment Options, I go back to the same error:
From your description it seems like it has deployed successfully in past.
Have you upgraded project recently? If yes, look at this thread Strange issue with System.Net.Http 4.2.0.0 not found
Also, check if you are referencing DLL from NuGet and not GAC.
First thing that comes to mind is that you added a new DLL in your solution that depends on another version of System.Net.Http assembly. If you know that you added a new DLL, check for its assembly dependency.
Another solution might be to manually specify the version of the assembly in code. And select SpecificVersion. (but I'm guessing it's the first thing that causes the issue).
I have came across similar issue once. There should be a mismatch of the versions between System.Net.Http package.
Try verifying the version of System.Net.Http for the entire solution and refer the same version in all the projects.
This happens if a package is referenced in a project and different version of the same package is referenced in another project. Here, this error occurs when there is a reference between these two projects.

Assembly binding error when building Office add-in: "FindRibbons" task failed unexpectedly

We're trying to set up a Jenkins (build server) job to build our Office add-in based on VSTO. However, I keep getting a strange error that fails the build process after the DLL is copied to the bin directory of the project:
Error 11 The "FindRibbons" task failed unexpectedly.
System.IO.FileNotFoundException:
Could not load file or assembly 'MyAddIn, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' or one of its dependencies.
The system cannot find the file specified.
File name: 'MyAddIn, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
So the problem is that the "FindRibbons" task, triggered by the Office add-in build target, has successfully identified the MyAddIn DLL as being an Office Add-In, but is not able to locate and load it!
Any ideas? I'd love to be able to debug the FindRibbons task directly but hooking into and debugging the compile process seems a little extreme...
Here are some observations:
In our build server's Fusion logs for binding the MyAddIn assembly it looks like it's looking in the folder where MSBuild.exe lives (C:\Windows\Microsoft.NET\Framework\v4.0.30319\) and nowhere else.
On my dev machine, there is no Fusion log entry for MyAddIn! But the build process succeeds and Kivo works fine.
On both my dev and build machines I also have Fusion log entries for WhereRefBind!Host=(LocalMachine)!FileName=(PresentationCore.dll) and ExplicitBind!FileName=(MyAddIn.dll) which show the binding succeeding.
This error comes up on the build server whether I use Visual Studio or MSBuild from the command line to build the project.
I've ensured that the .NET/MSBuild/VS2012 versions are identical on both my dev machine and the build server and the error still occurs. The only difference seems to be that the build server is running Windows Server 2012 (since it's Azure, and we can't spin up a Windows 7 image).
This has worked for me every time I upgrade Visual Studio - I don't use ribbons.
This worked for my solution, but use at your own risk:
Open the following file in an XML editor (make a backup first): C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets (the v10.0 part may be different for you, e.g., it might be v14.0)
Remove the following section:
<FindRibbons AssemblyName="$(AbsolutePathToCustomization)" TargetFramework="$(TargetFrameworkVersion)">
<Output TaskParameter="RibbonTypes" ItemName="RibbonTypesCollection"/>
</FindRibbons>
Replace all occurrences of "#(RibbonTypesCollection)" with the empty string ""
Save the file and restart the visual studio
If you migrated the project from a previous version of Visual Studio, be sure to remove the ExcelLocale1033 and SecurityTransparent attributes from the AssemblyInfo.cs file (as answered by Swati in this other question)
If the project still fails to build, it may be because your .csproj file has some references to msbuild's tasks of previous versions of Visual Studio. I suggest you to create a new empty Excel AddIn project, and uses the msbuild structure of the new project file as base for your project.
I had this problem. It was apparently caused because I changed the "Copy Local" setting on reference "Microsoft.Office.Tools.Common.v4.0.Utilities" from True to False. ISYN. (I sh*t you not)
I had upgraded a project from VS2012 to VS2013 and noticed that that reference was the only one set to "Copy Local = True". So I set it to false, because it was different. This caused the error. Changing it back to True solved it.
I had the same error message and finally found a fix. The problem stemmed from the VSTO project being targeted for .NET 4.0 (it seems this is the minimum for VSTO4), while also referencing an assembly built for .NET 3.5. The real culprit was that I had a class in the VSTO project deriving from an interface defined in the .NET 3.5 assembly that in turn derived from a .NET 3.5 library interface. i.e.,
using System.Xml;
class MyVSTOClass : IMy35AssembyInterface // This caused the error
class MyVSTOClass : IXmlSerializable // This compiled OK
using System.Xml;
interface IMy35AssembyInterface : IXmlSerializable
The fix was to update the .csproj to explicitly reference the older version of System.Xml.dll and System.Data.dll which would otherwise default to 4.0 and conflict with the 3.5 assembly references.
<Reference Include="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <!--<Aliases>Data2</Aliases>-->
      <HintPath>C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll</HintPath>
      <SpecificVersion>True</SpecificVersion>
      <Private>False</Private>
    </Reference>
 <Reference Include="System.XML, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <!--<Aliases>Xml2</Aliases>-->
      <HintPath>C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll</HintPath>
      <SpecificVersion>True</SpecificVersion>
      <Private>False</Private>
    </Reference>
For those who need to simultaneously reference both the newer and older versions of a DLL, note that it is in theory possible using:
extern alias XmlDll1
using XmlDll1::System.Xml
See http://geekswithblogs.net/narent/archive/2008/11/11/126940.aspx for more info.
This problem can also be caused by adding a reference to an unsigned assembly to a signed/strong named add-in project. In my case I added the RestSharp Nuget package and started receiving this error on build as soon as I referenced RestSharp in code. After some digging I noticed that RestSharp was the only unsigned assembly in the project references. If you have this problem, there are 3 possible solutions:
In the case of RestSharp, I found that there was a signed version available on Nuget - searched for "restsharp signed" and installing that solved the problem.
If you have access to the source code, you can configure Visual Studio to build a signed version of the assembly in the Project Properties page.
If you do not have access to the source code, you can sign the assembly with your own key by following these instructions.
I had the same error and none of the answers from the internet help me fix this problem. The reason why I was getting that error is because I was referencing an assembly of type Console Application. I changed that assembly to be of type ClassLibrary and I did not got that exception any more.
Also I would only get that exception when inheriting from a class that was located on my ConsoleApplication. It took me forever to figure it out.
May be a little late here, but I just resolved this for myself - after following numerous suggestions (via google) all of which did not solve my problem I manually went down the line. Turns out I had compiled a set of libraries with a dependent assembly with a lower version (not the latest). In my main project I also had a reference to this dependency but it was pulled via nuget and was at the latest & greatest version. For some reason VS.NET couldn't figure that out and would completely trip out and drop the error you posted. Once I updated the set of libraries to the latest version of the dependency all worked as normal.
The crazy part is - it worked fine initially and then out of nowhere the issue came about. Hope this helps someone along the way.
After enabling Fusion the output showed that it was looking for the assembly in the msbuild/ folder.
I just encountered this same situation today, futzing around for a bit, restarting VS and then rebooting my machine without any success. Than one warning popped out at me - One of my dependent assemblies was not strong named. Setting that assembly to be strong-named solved the problem.
I had the same issue, and even after reading KKG's answer I could not resolve mine.
It turned out to be much simpler for me, but not less frustrating and time consuming. I was working in a Win8.1 VM which does not ship with .net3.5 by default. My .net4 VSTO4 project was referencing an assembly that requires 3.5 somewhere. The same project compiled find on my other VM which was Server2008 and had 3.5 enabled.
In my case, the cause for this error was the mere existence of a field of a generic value type in the assembly (not kidding), e.g.:
class Foo
{
ImmutableArray<int> foo;
}
Workaround (if the additional indirection is acceptable performance-wise):
Wrap the value type in a reference type. This can be done generically with something like
public sealed class Box<T>
{
public readonly T value;
public Box(T value)
{
this.value = value;
}
}
then foo can be of type Box<ImmutableArray<int>>.
I have experienced this same issue with an add-in for Outlook.
The solution for me was to set Embed Interop Types to True on my reference to Office.dll.
This however caused the add-in to crash during startup with an Access Denied on Microsoft.Office.Interop.Outlook. I fixed that issue by setting Embed Interop Types to True on all references to Microsoft.Office.Interop.Outlook.dll as well.
This error can be caused by a clash of dependency versions. For example:
YourAddIn
-- OtherLibrary v1.3
-- BaseLibrary v1.0
-- BaseLibrary v2.0
If a newer version of BaseLibrary v2.0 is released and updated in your project, however this version introduce a breaking change in your other dependency OtherLibrary, you will see this exception because OtherLibrary is still trying to find the old methods that doesn't exist in newer assembly.
Update OtherLibrary with the latest packages will resolve this clash of dependency versions.
This can also happen if the Microsoft.Office.Tools.Outlook.v4.0.Utilities reference is set to <Private>False</Private>.
<Reference Include="Microsoft.Office.Tools.Outlook.v4.0.Utilities">
<!-- Required for FindRibbons task -->
<Private>True</Private>
</Reference>

Visiblox invalid assembly reference

I am struggling with a problem regarding to visiblox. I am quite new to the WPF concept, but I am getting the hang of it. I have quite some expirence with the classic forms.
My problem is that the designer keeps telling me that the XAML contains errors. And I get the folling error in my error list:
Unable to load the metadata for assembly 'Visiblox.Charts'.
This assembly may have been downloaded from the web.
See http://go.microsoft.com/fwlink/?LinkId=179545.
The following error was encountered during load: Could not load file or assembly
'Visiblox.Charts, Version=2.1.4.31043, Culture=neutral, publicKeyToken=1543c03f04c4461b' or one of its dependencies.
I have added the reference to the project, and I have added the line:
xmlns:charts="clr-namespace:Visiblox.Charts;assembly=Visiblox.Charts"
I can build and run the project, but the designer keeps telling me that my assemblies are not correct. I tried to clean and rebuilt the project. Restarted Visual Studio and the computer, but nothing seems to work. Anyone any clue? Thank you in advance.
I'd suggest following the link it provides: http://go.microsoft.com/fwlink/?LinkId=179545
The problem is that the dll is "locked" by windows security and you need to unlock it before you can reference it. The link specified tells you how.

Could not load System.Data

I am a little baffled as to why this is occurring. I have been cruising along just fine -- made a couple changes / rebuilt (builds just fine) and all of a sudden no matter what I do I get the following exception when I try to debug..
Could not load file or assembly
'System.Data, Version=2.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089' or
one of its dependencies. The system
cannot find the file specified.
Any ideas how to get this back to how it was before? I even undid the changes I made (that really shouldn't have had anything to do with this) to no avail.
If a "Windows" update recently patched the .NET framework version you're referencing, the compiler may think there's a versioning difference.
If this were the case, try rebooting, and possibly re-checking updates. Failing that, re-installing the .net framework version, and letting the updates get re-applied. I had a similar issue with another System component after once installing a trial version of XNA 4.0, and then uninstalling it.
Ensure that you have System.Data still added as a project reference. If so, be sure to then exist Visual Studio and go back in. Then do a "clean" and "Build" and you should be set to go.

Categories

Resources