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

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.

Related

.net core msbuild nuget with additional assemblies

This sample shows a .NET Core project which can be packaged into a nuget package just using dotnet pack, and when restored in another project, it integrates in the msbuild pipeline. One of the great things about this sample is it creates a nuget package that integrates with msbuild on linux, mac and Windows. However, the custom build code doesn't have dependencies on any other assemblies.
How can I adapt this sample to use code that uses a dependency?
Here are my failed attempts:
Attempt 1
I added a package reference to Newtonsoft.Json and changed the code to do some JSON serialisation. However, in the project that uses the build nuget, when I do a dotnet publish, I get the following error:
error MSB4018: The "Zip" task failed unexpectedly. [C:\git\MSBuild-Features-With-Nate-McMaster\Video-2\1-NuGet\Web.csproj]
error MSB4018: System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. The system cannot find the file specified. [C:\git\MSBuild-Features-With-Nate-McMaster\Video-2\1-NuGet\Web.csproj]
Additionally, if my project didn't already have a dependency on JSON.NET, adding the build nuget would unnecessarily add it.
Attempt 2
I used nuget.exe spec to create a .nuspec file. At the end of the file, I added:
<files>
<file src="bin\Release\**" target="build" />
<file src="build\**" target="build" />
</files>
However, both "dotnet pack" and "msbuild /t:pack" ignore the file, and nuget.exe pack fails with the error Unable to find 'bin\Release\0-WriteATask\bin\Release\'. Make sure the project has been built..
If I try nuget.exe pack Zipper.nuspec or msbuild /t:pack /p:NuspecFiles=Zipper.nuspec, they both fail with the message Value cannot be null or an empty string..
Attempt 3
I edited the nuspec to remove all of the placeholders that are normally calculated from the project (any string starting and ending with a $). Then, doing a nuget.exe pack Zipper.nuspec created a nupkg file, and the net46 folder contains Newtsonsoft.Json.dll, but the netstandard1.3 folder does not.
The way MSBuild loads a task assembly can make it tricky to load additional assemblies that you may depend on.
Typically, the easiest way to solve this is to ship a copy of your dependencies inside your NuGet package. But your dependencies alongside your task assembly file in the package. There may be some additional complications that require you to use AssemblyLoadContext or the AppDomain.AssemblyResolve event.
You can do this without a nuspec file by forcing MSBuild to copy your assemblies into the local build output, and then copying them into your package. Set CopyLocalLockFileAssemblies=true, and add the items to _PackageFiles
Here's an example of how to do that: https://github.com/madskristensen/BundlerMinifier/blob/3333b5c38289a247391966443370ee6f4a29bf26/src/BundlerMinifier/BundlerMinifier.csproj#L35-L47
Hopefully, this will be addressed in the future, https://github.com/Microsoft/msbuild/issues/1312, and the task assembly resolution will use the NuGet cache.
Try it with the 9.0.1 version of Newtonsoft.Json, it worked for me, all these dll load problems went away, and it still targets .NET Standard. Although I did copy all the dependencies next to the task dll, but with the 10.x version even that didn't help.

Why Msbuild does not copy Newtonsoft.Json.dll referenced by Microsoft.AspNet.WebApi.Client?

My web projectA references my projectB that references Microsoft.AspNet.WebApi.Client that references Newtonsoft.Json assembly. It is not referenced anywhere else. The problem was that on my deployment server Newtonsoft.Json was not copied to bin directory once run as
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe .\src\path.to.Web.projectA.csproj /t:Rebuild /p:Configuration=Release
And it eventually broke with this exception:
FileNotFoundException: Could not load file or assembly
'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The
system cannot find the file specified.
I've resolved it by adding a stub code to projectB:
string json = JsonConvert.SerializeObject(new Something());
Can someone describe why it was not copied before?
Note. These questions do not seem to answer the question or out of date or I miss something?
MSBuild does not copy directly referenced dlls to bin folder
msbuild not copying referenced assembly
MSBuild doesn't copy references (DLL files) if using project dependencies in solution
http://blog.alexyakunin.com/2009/09/making-msbuild-visual-studio-to.html
Copying a DLL's dependencies in Visual Studio
I made a comment under Alex's blog post,
It does not work for me when the indirect dependency comes from NuGet packages (Microsoft.AspNet.WebApi.Client and Newtonsoft.Json). But the tip described in http://www.paraesthesia.com/archive/2014/05/09/recursively-copying-indirect-project-dependencies-in-msbuild.aspx/ solves the issue. I think it uses a more suitable indirect dependency detection approach.
If you follow that solution, then there is no need to use the stub at all.
About why the dll is not copied over, it has been narrowed down to an MSBuild issue (or more as there are too many reports related), which Microsoft claims won't fix on Connect,
https://connect.microsoft.com/VisualStudio/feedback/details/797034/msbuild-handles-direct-and-transitive-references-differently-for-unused-assembly-references

Could not load file or assembly 'Microsoft.Web.Design.Server

Help please, I am having an error on the TFS. it complains that it cannot load the assembly below.
the project works fine on my computer. I searched for the dll by there is no such dll called: Microsoft.Web.Design.Server.
Any help pelase?
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Web.Design.Server, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
The build server does not have the same software installed as your local machine. Thus when it goes to resolve dependencies it cannot find them.
This is compounded when your project has GAC references.
I recommend that anything which isn't a system assembly be copied to a folder and put into source control. The projects should then reference this assembly as a file reference instead of a GAC reference.
Since the file is now in source control it'll be available to the build server for it to resolve the assembly at compile time.

NUnit unable to load dll with String Key Name

I have a project in which I have some C# classes which I am building using csc in my ant script. I have also have written some unit tests which I want to test with NUnit2.6, My test class and actual class refer to one DLL which is compiled with String Key Name (.snk), if I install thi DLL in my GAC (gacutil /i myDLL.dll) everything works fine. As soon as I uninstall this DLL from my gac I get following error for all the test during the build when I try to run my test using NUnit;
Test Error : fromJDynTest1
[exec] System.IO.FileNotFoundException : Could not load file or assembly 'myDLL, Version=5.1.0.0, Culture=neutral, PublicKeyToken=c0409b584f86b2d6' or one of its dependencies. The system cannot find the file specified.
[exec] at fromJDynTest1()
...
...
Does anyone know how to resolve this. Let me know if you need any other information.
Thanks
--
SJunejo
The code you are testing cannot see the assembly in question when you place it in the gac it can see the assembly. You need to ensure that the code you are testing has the assembly in the bin directory when compiled (perhaps copy local is set to false in your project on the assembly reference).
The reason I am sure it is not a dependency (which is mentioned in the message) is because the situation is resolved when the assembly is placed in the gac. This error is not likely caused because the assembly is strongly named.
Failing that the test code refers to the assembly and that test code either doesn't have a reference or has the same problem (not in its compiled directory).
Would help if you posted the actual test or structure of the project.
.net locates assemblies as follows:
Determines the correct assembly version by examining applicable configuration files, including the application configuration file, publisher policy file, and machine configuration file. If the configuration file is located on a remote machine, the runtime must locate and download the application configuration file first.
Checks whether the assembly name has been bound to before and, if so, uses the previously loaded assembly. If it failed before it will fail again now.
Checks the gac. If the assembly is found there, the runtime uses this assembly.
Probes for the assembly (if you open up a .csproj file in a text editor you will see hintpaths, which try and help .net find the assembly).

Mono - Could not load file or assembly

i just exported my project to my new imac an loaded my solution into monodevelop.
After the successfully compilation i started the project, and i always getting the same error.
** (/Library/Frameworks/Mono.framework/Versions/2.6.1/lib/mono/2.0/xsp2.exe:1576): WARNING **: The following assembly referenced from /private/var/folders/9y/9ypfv24bEwieHw+Yy+PUsU+++TI/-Tmp-/nhu-temp-aspnet-0/ebc9e2c6/assembly/shadow/7f5f6265/859daf16_fac2cd73_00000001/Centrism.Administration.UI.dll could not be loaded:
Assembly: Centrism.Core (assemblyref_index=3)
Version: 0.0.3688.40278
Public Key: (none)
The assembly was not found in the Global Assembly Cache, a path listed in the MONO_PATH environment variable, or in the location of the executing assembly (/var/folders/9y/9ypfv24bEwieHw+Yy+PUsU+++TI/-Tmp-/nhu-temp-aspnet-0/ebc9e2c6/assembly/shadow/7f5f6265/859daf16_fac2cd73_00000001/).
** (/Library/Frameworks/Mono.framework/Versions/2.6.1/lib/mono/2.0/xsp2.exe:1576): WARNING **: Could not load file or assembly 'Centrism.Core, Version=0.0.3688.40278, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
In other projects the assembly works correct and can be loaded.
I had the same problem with a dll that was in the same path as the application assembly, the problem was that the dll name had a different case than the expected one. So the Abc.dll was missing, in the folder it was named abc.dll, after renaming, it worked.
I guess I'm going to ask the obvious:
Do you have Centrism.Core.dll somewhere that Mono knows about and can get to?
If your project is referencing the Centrism.Administration.UI.dll, and it is not in the GAC on your iMac, the easiest solution would probably be to set the reference to that assembly to "Local Copy". For web projects, this will copy the assembly into the application's Bin directory, where xsp should be able to pick it up.
I had this error when adding a new assembly to an existing project (which itself was referenced by a unit test project).
Because the assembly wasn't included in both projects, the unit test would error out. The solution was to add a Copy tag to the project file:
<Copy SourceFiles="$(ProjectDir)\dlls\NewAssembly.dll" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true" />
Presumably it would also work to add the reference directly to the unit test (this reference is not used directly, so was able to avoid using above).