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

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.

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.

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

TFS 2015 reference required to assemby

I've a problem with a build with TFS 2015.
I've a simple project that reference B.dll . B.dll use A.dll . The project don't reference A.dll as it don't use it itself.
A.dll & B.dll bothe are company Framework dll stored in the GAC.
[error]LogistiqueTest\Index.aspx.vb(7,14): Error BC30009: Reference required to assembly 'A.DII.Technique.Interfaces, Version=3.5.0.0, Culture=neutral, PublicKeyToken=e60618ca32d203a8' containing the implemented interface 'A.DII.Technique.Interfaces.IGestionException'. Add one to your project.
This is not a big deal, just a reference that's missing. But we don't want to add unecessary references in our projects
Moreover, this problem don't occurs with visual studio (2013 pro)
So I tracked the problem and I saw that :
-> Visual studio retrieve quickly the reference (A.dll) after the "call" of this line
Target "CoreBuild: (TargetId:61)" in file "C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets" from project "D:\AgentTFS_02_work\3\s\LogistiqueTest\LogistiqueTest.vbproj" (target "Build" depends on it):
But TFS don't use the same file; it use
"C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.targets"
The difference between those files is in Microsoft.Common.CurrentVersion.targets, there is some code for resolving assemblies.
So my questions are, is there a way to make TFS build my solution visual studio like ? Force the other target file to be used? Is there another trick to be able to resolve this assembly ?
I tried to add, like i saw in many post around the web, a folder that contains every .dll required, but TFS don't event try to resolve path for A.dll
More information : The exception occurs at the execution of Vbc.exe
because with VS execution the command line has A.dll in the /import section when with TFS it's not added. No problems with MSBuild.exe.
I finally found an anwser :
http://www.nsilverbullet.net/2012/02/17/automatically-resolving-secondary-assembly-references-with-msbuild-and-tfs-build/
I need to add this to the proj file that has problems.
<Target Name="AfterResolveReferences">
<!-- Redefine referencepath to add dependencies-->
<ItemGroup>
<ReferencePath Include="#(ReferenceDependencyPaths)"></ReferencePath>
</ItemGroup>
</Target>

How to add reference to an external dll file in asp.net core project

Everytime I try to add reference to any dll file from my non asp.net core projects to my new asp.net core project i get this error message:
.NET Core projects only support referencing .NET framework assemblies in this release. To reference
other assemblies, they need to be included in a NuGet package and
reference that package.
What should be happen here? is there a special way to do it?, seams there is something I am missing here which different than all previous asp.net version
As of now, you cannot directly add full .NET framework dll into ASP.NET core project (netcoreapp1.0) directly. You will have to create NuGet package.
If it is project specific dll then create local NuGet package. These are the steps we followed in our project to generate NuGet package-
1.Download Nuget.exe and place it in the folder where .csproj file exists.
2.Open cmd and type nuget spec. File with .nuspec extension will be created.
3.Open the created file and add tag:
<files> <file src="..\..\SomeRoot\**\*.*" target="libs\net461" /> </files>
4.Execute nuget pack A.csproj –IncludeReferencedProjects in cmd. File with .nupkg extension gets created.
5.Go to visual studio. In NuGet package manager settings, Add in “Package Sources” and provide path where your .nupkg and .nuspec file exists.
6.Close Nuget package manager and again open it. Now you can find it in your created package source under browse tab.
Note: Your .nuspec file should be like :
<?xml version="1.0"?>
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ABC.XYZ.FL</id>
<version>1.0.0.0</version>
<title>ABC.XYZ.FL</title>
<authors>ABC</authors>
<owners>ABC</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Framework that will be used to create objects in XYZ world</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>2016</copyright>
<tags>ABC.XYZ.FL</tags>
</metadata>
<files>
<file src="bin\Debug\*.dll" target="lib\net461" />
</files>
</package>
The following links contains more details about creating nuget package and hosting it locally:
https://docs.nuget.org/create/creating-and-publishing-a-package
https://docs.nuget.org/create/hosting-your-own-nuget-feeds
https://docs.nuget.org/consume/nuget-config-file
See if this helps.

Issue with WiX installer configuration and Newton.JSON nuget package

I'm making an Setup project using WiX and my project uses the Newton.JSON library. I've not referenced the Newton library in WiX however it is fully referenced in my application.
This is the error I'm receiving (Sorry for the image, unable to copy paste the code) :
Could someone please save me, I've been pulling my hair about this for a few hours now, nothing I try seems to work. Tried deleting all bin and ob files, deleted packing.config, deleting the bin and obj from both my project and the installer project. Then trying to reinstall Newton.JSON. Still no joy.
Here is my package.config file currently :
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
</packages>
Here is the link to my setup project xml - http://nopaste.info/bcbc2048a6.html (for some reason SO doesnt let me add it as code.
As you can see the packages property does indeed have the newton.json reference.
Anyone have the same issue at all? So it isn't just me?
Try to assemble all dependencies manually in the folder you wish to install to and try to run the application from there to see what files need to be installed. Register any components that need registration such as COM Interop files. Most .NET applications work if you copy all referenced assemblies to the installation directory. The EXE will use the local folder as default search path.
You need to include dependencies in your installer package. WiX doesn't automatically pull in project references.