File not Found Exception was unhandled - dll

I created a Project A class library that reference azure.identity and built A.dll.
Then I created a Project B and imported in A.dll, but error comes out that could not load assembly:

In your project B add NuGet packages Azure.Identity and Microsoft.Graph in version 4.45.
Or if your project A and project B are part of the same solution then add project reference to project A in project B instead of importing A.dll.

Related

System.Management.dll not being copied to output directory .net 6 c#

There is an error System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'.I figured when the System.Management.dll is in the output directory, the error will be gone.
It is reference using NuGet but I couldn't get libraries in NuGet to be copied to the output folder. So I thought of adding the physical System.Management.dll file into my project and and reference it as assemblies (right-click project -> Add Reference -> browse to library) thenset 'Copy Local' to True. However, the dll still won't get copied over to the output folder.
The application is on .Net 6. Is there a reason behind why I can't copy this library like other?
A .NET6 project would normally use System.Management version 6.0.0.
The error suggests that there is another package in your project that explicitly depends on 4.0.0.
Without knowing the specifics of you projects, I suggest:
inspecting the build output for warnings about package version,
upgrading 3rd party packages in your solution.
Figured out I could copy nuget package references to output directory by setting this attribute in the project file
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
Then the nuget referenced packages will be copied to the output directory.

How to make .nuproj projects work with NCrunch

I have a solution with two nuproj projects, say A and B.
Both projects contain only props and targets files (so NO assemblies or PDB files) and they both generate NuGet packages.
Now, A has no reference and it is an internal dependency, whereas B references A plus other external NuGets.
Then I have an MSTest project that dynamically invokes MSBuild on a project which references B.
All works fine in Visual Studio and the tests pass. However, NCrunch compiles with warnings for the project B.
WARNING - ........\program files (x86)\microsoft visual studio\2017\professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets (2110, 5): MSB3246: Resolved file has a bad image, no metadata, or is otherwise inaccessible. Could not load file or assembly 'A.nupckg' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Infact, no NuGet package is generated for the A project in the NCrunch solution directory (the package is correctly generated in Visual Studio).
I have also tried to set the following properties for the A and B projects:
Instrument assembly = false
Prevent signing of output assembly = false
Implicit project dependencies = true
nothing changes and the warning is still there and no NuGet is generated for the A project (all the source files and dependent assemblies are in the NCrunch solution folder).
And Copy referenced assemblies to workspace is set to true.
Is there any known issue with using NCrunch with .nuproj?

Your project is not referencing the ".NETPortable,Version=v4.5,Profile=Profile78"

When i build my PCL project i received the following error:
Microsoft.NuGet.targets(186, 5): [null] Your project is not referencing the ".NETPortable,Version=v4.5,Profile=Profile78" framework. Add a reference to ".NETPortable,Version=v4.5,Profile=Profile78" in the "frameworks" section of your project.json, and then re-run NuGet restore.
Following error appears when you have .netstandard project in same directory and build it once.
It leaves .json files in Obj directory and it breaks PCL project build.
Move .netstandard .csproj out of this directory and remove Bin and Obj directories.

JetBrains Rider - The task factory "CodeTaskFactory" could not be loaded from the assembly

I am trying to build a former Windows project in JetBrains Rider on a Mac. I am getting the following build error:
NuGet.targets(100, 9): [MSB4175] The task factory "CodeTaskFactory"
could not be loaded from the assembly
"/Library/Frameworks/Mono.framework/Versions/5.2.0/lib/mono/msbuild/15.0/bin/Microsoft.Build.Tasks.v4.0.dll".
Method 'Microsoft.Build.Utilities.TaskLoggingHelper..ctor' not found.
Can anyone help?
This happens because custom msbuild code or NuGet packages you use are using a pre-MSBuild 15 way of referencing MSBuild's CodeTaskFactory.
Check all of your project / build files for occurrences of UsingTask elements referencing Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll or Microsoft.Build.Tasks.v4.0.dll (or similar dll reference) and change the dll to Microsoft.Build.Tasks.Core.dll.
If the problematic code comes from a NuGet package, see if an update to the package is available or contact its authors.

Method not found: 'Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)'

I have a problem regarding NewtonSoft.
I have a solution with 3 projects for example. Project A has reference points to Project B and Project C, Project B also has reference points to Project C. Both B and C has NewtonSoft assembly. Project C has function to get JsonMediaTypeFormatter:
new JsonMediaTypeFormatter()
the function is called by all three project. Both Project B and C can call the function without any problem. But when project A calls the function, it throws error:
Method not found: 'Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)'
Something I notice that even project A does not have NewtonSoft reference, when project is built, the Newtonsoft.json.dll is copied to its bin\Debug folder. I guess it is because of Newtonsoft assembly is set to true for Copy Local optoin in both project B and C. If I manually delete this dll from Project A's bin\Debug folder, problem solved.
My question is why project A can hit the exception, and is there any solution except manually delete Newtonsoft dll from project A's bin\Debug folder? Set Copy Local to false is not a option because it will prevent dll deploying to their own bin folder for project B and C too.
Any suggestion would be appreciated.
Update
Here is my code snippet
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Net.Http.Formatting;
var jsonSerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var dateConverter = new IsoDateTimeConverter
{
DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
};
jsonSerializerSettings.Converters.Add(dateConverter);
var formatter = new JsonMediaTypeFormatter
{
//SerializerSettings = jsonSerializerSettings
};
If I comment out SerializerSettings, it works fine.
If I uncomment this line, application will return such issue.
If I just pass in blank setting to it
var formatter = new JsonMediaTypeFormatter
{
//SerializerSettings = new JsonSerializerSettings{}
};
I got error:
Method not found: 'Void System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.set_SerializerSettings(Newtonsoft.Json.JsonSerializerSettings)'
I think it may be related to different System.Net.Http.Formatting references inside projects, but I checked reference setting, they are all point to file
packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll
This code resides in Project C. It only failed when called by Project A, project B and C can call it without any problem. Once I remove NewtonSoft.Json.dll from Project A bin\Debug folder, it works in project A calls too.
Do you know what happened? and how can I check if there still have different reference version conflicit in project?
Thanks,
Here I finally got problem solved.
The reaon for this issue is that in project A, there is a reference library points to System.Net.Http.Formatting (5.2.3.0), which depends on Newtonsoft Joson version 6.0.0.0. You can check this on Assembly Explorer. I updated Newtonsoft to 8.0.0.0 recently and project B and C are referencing this version. When project is built, the Newtonsoft 8.0 has been copy to project A's output folder based on prject B and C (Copy Local is set to true). when application is running, it will throw error
System.IO.FileLoadException : Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
or in my debug mode, it throws
{"The 'CreateJsonSerializer' method threw an exception when attempting to create a JSON serializer."}
Solution:
Add app.config file in project A, including setting below, to redirect system assembly binding to correct NewtonSoft version
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
</dependentAssembly>
</assemblyBinding>
The reason project B can call project C successfully is that it has this redirect on its web.config file.
Thanks,
I would clean up the way you do references...in the "new way".
You have the below setup, except for repositories.config and the 3 packages.config
\SolutionFolder\
\SolutionFolder\MySolution.sln
\SolutionFolder\packages\
\SolutionFolder\packages\repositories.config
\SolutionFolder\CSProjectA\
\SolutionFolder\CSProjectA\CSProjectA.csproj
\SolutionFolder\CSProjectA\packages.config
\SolutionFolder\CSProjectB\
\SolutionFolder\CSProjectB\CSProjectB.csproj
\SolutionFolder\CSProjectB\packages.config
\SolutionFolder\CSProjectC\
\SolutionFolder\CSProjectC\CSProjectC.csproj
\SolutionFolder\CSProjectC\packages.config
Each of the packages.config will look like this
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
</packages>
repositories.config will look like this
<?xml version="1.0" encoding="utf-8"?>
<repositories>
<repository path="..\CSProjectA\packages.config" />
<repository path="..\CSProjectB\packages.config" />
<repository path="..\CSProjectC\packages.config" />
</repositories>
When you do a nuget restore, the following will be created
\SolutionFolder\packages\Newtonsoft.Json.4.5.11\
\SolutionFolder\packages\Newtonsoft.Json.4.5.11\lib\
\SolutionFolder\packages\Newtonsoft.Json.4.5.11\lib\net40\
\SolutionFolder\packages\Newtonsoft.Json.4.5.11\lib\net40\
\SolutionFolder\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll
\SolutionFolder\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.xml
All your .csproj references (hintpath) will be
..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll
This is what nuget does for you (auto-voodoo-magically), but I'm explaining it here.
This is how you get consistency from your references.
On your local box, you can just do a normal VS "build" or "rebuild".
If you have missing packages, you can fix it (one time) with this command line call:
nuget.exe restore "\SolutionFolder\MySolution.sln"
When you build (on your build server), you will do a:
nuget.exe restore "\SolutionFolder\MySolution.sln"
msbuild.exe "\SolutionFolder\MySolution.sln"
APPEND
Try this:
"%WINDIR%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" "MySolution.sln" /p:Configuration=Debug;FavoriteFood=Popeyes /l:FileLogger,Microsoft.Build.Engine;logfile=MySolution.Debug.log
"%WINDIR%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" "MySolution.sln" /p:Configuration=Release;FavoriteFood=Popeyes /l:FileLogger,Microsoft.Build.Engine;logfile=MySolution.Release.log
And view the log to see the source of the Newtonsoft.Json.dll is coming from (that ends up in ProjectA\bin\Debug).
Now that I relook at your comments and OP, you do have a weird problem. I'm wondering if the ProjectA\bin\Debug\Newtonsoft.Json.dll might be coming from an alternate source.
I have the same problem.
Project A (web app) reference project B (PCL), both have the same newtonsoft nuget package, and A has the binding redirect.
Project C (test project) reference project B and E (PCL too), all having the same newtonsoft nuget package.
When i build project A, project A works ok. IF i build (not rebuild) project C, project E is build a,d project B is skipped (alreadu built). BUT project A is affected: something overwrites the newtonsoft json DLL in its bin folder with a CL version. And now project A won't run with this method not found error.
If i manually replace the newtonsoft json dll in project A with the same one that was in the bin folder before building project C, A works again ok !
If i then start debugging project C, no rebuild occurs, but project A stop working !
I suppose something is wrong with the newtonsoft json assembly between the PCL version and the net45 version, and the .NET engine picks the wrong one. I have added the net45 version to the test project, without success.
UPDATE ------------
The only workaround i found, is to separate projects in 2 solutions and open 2 visual studio.
In solution A, i add project A and B.
In solution B, i add project C and its dependancies B and E.
And now all works as expected. Project A can run, and running/compiling project B does not interfere with project A.