MSBuild cannot find a reference - ReactJs.NET - msbuild

After upgrading Newtonsoft.Json to version 9.0.0 and ReactJS.Net packages to 2.5.0, the TransformBabel.proj stopped working:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="TransformBabel">
<!-- ReactJS.NET - Transpile JavaScript via Babel -->
<UsingTask AssemblyFile="$(OutputPath)\React.MSBuild.dll" TaskName="TransformBabel" />
<Target Name="TransformBabel">
<TransformBabel SourceDir="$(MSBuildProjectDirectory)" />
</Target>
</Project>
Returning the following:
TransformBabel.proj(6, 3): error MSB4018: The "TransformBabel" task failed unexpectedly.
[Exec] TransformBabel.proj(6, 3): error MSB4018: React.TinyIoC.TinyIoCResolutionException: Unable to resolve type: React.IReactSiteConfiguration ---> System.TypeInitializationException: The type initializer for 'React.ReactSiteConfiguration' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
It seems that it cannot load the Newtonsoft 6.0.0.0 version. The web.config has an assembly redirection:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
But I am not sure that, as it is starting a new msbuild process, if it is being ignored. I would like to hint msbuild the location of the assembly, but no success until now.

A bit late to the party here, but hopefully my experience will help to anyone else having the same problem.
I have recently experienced the same issue with React.MSBuild 3.1.0. It seems that it has hardcoded a specific version as I have updated my Newtonsoft.Json to the latest (10.0.3) using NuGet and set redirection correctly, but the build kept failing on the same error as you have mentioned.
What I did is simply uninstall all the React packages (MSBuild and Core) and also Newtonsoft.Json (using -force as there were other dependencies) and then let NuGet install the React.MSBuild again. It has installed it with all dependencies what resulted in obtaining Newtonsoft.Json 9.0.1.
Not sure why do they restrict Newtonsoft.Json library to a specific version, but it is more a question to React developers. Unless you need a latest (or other specific version of it), this should fix the problem.

I know this is an old post...but here is my workaround:
I put Newtonsoft.Json.dll v6.0.0.0 into Tools directory relative to the project directory and let msbuild copy it to $(OutputPath) to satisfy TransformBabel task condition.
My TransformBabel.proj looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="TransformBabel">
<!-- ReactJS.NET - Transpile JavaScript via Babel -->
<UsingTask AssemblyFile="$(OutputPath)\React.MSBuild.dll" TaskName="TransformBabel" />
<Target Name="TransformBabel">
<Copy SourceFiles="$(MSBuildProjectDirectory)\Tools\Newtonsoft.Json.dll" DestinationFolder="$(OutputPath)" />
<TransformBabel SourceDir="$(MSBuildProjectDirectory)" />
</Target>
</Project>
After this TransformBabel task is finished, then let msbuild overwrite Newtonsoft.Json.dll v6.0.0.0 in $(OutputPath) with whatever Newtonsoft.Json.dll version my project is actually using, ex: v8.0.3.
So, in the main project .csproj, I have something like this:
<ItemGroup>
...
<Reference Include="React.MSBuild, Version=2.3.0.0, Culture=neutral, PublicKeyToken=9aed67b161f7db78, processorArchitecture=MSIL">
<HintPath>Tools\React.MSBuild.dll</HintPath>
<Private>True</Private>
</Reference>
...
</ItemGroup>
...
<ItemGroup>
...
<Content Include="Tools\Newtonsoft.Json.dll" />
<Content Include="Tools\React.MSBuild.dll" />
...
</ItemGroup>
...
<Target Name="TransformBabel" AfterTargets="Build">
<Exec Command=""$(msbuildtoolspath)\msbuild.exe" $(ProjectDirectory)TransformBabel.proj /p:OutputPath=$(OutputPath) /nr:false" />
</Target>
<Target Name="AfterTransformBabel" AfterTargets="TransformBabel">
<Copy SourceFiles="..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll" DestinationFolder="$(OutputPath)" />
</Target>
Replace the path Newtonsoft.Json.8.0.3 inside AfterTransformBabel task to your need.

Related

Nuget targets, how to send a renamed content file to solution output folder?

I have created a Nuget package with a default json configuration file, 'config.json.pp'. On installation it is transformed and added as content; 'config.json'. I have also added a targets file in the build folder of the package, it renames the config file based on the assembly root namespace the package is installed in:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ConfigFile Include="config.json"/>
</ItemGroup>
<Target Name="CopyConfigFile" AfterTargets="Build;Rebuild">
<Copy SourceFiles="#(ConfigFile)" DestinationFiles ="#(ConfigFile->'$(OutDir)\$(RootNameSpace)%(Extension)')" />
</Target>
</Project>
When the assembly is build in a solution with more assemblies, it is not added to the output directory, only to the bin\$(configuration) folder of the assembly where the package is installed.
How can I add the file to the output directory of the main application of the solution?
You can use the link metadata to do this:
<ItemGroup>
<Content Include="config.json"
Link="$(RootNameSpace)%(Extension)"
CopyToOutputDirectory="PreserveNewest"
Visible="False"
Condition="Exists('config.json')" />
</ItemGroup>

Add .dll reference to nuget that is generated before build

I'm deploying a MsBuild task using nuget, that generates a .dll before each build.
I fail to get the generated dll referenced in the References node in the Visual Studio project of consumers.
I'm also using MSBuild to build the .nupkg file. Generating and compiling works just fine, I deploy the following target in the build/directory
<Project>
<!-- this will automatically run before the 'Build' target -->
<Target Name="GenerateAndBuild" BeforeTargets="Build">
<!--the deployed task that generates the code-->
<Utils.CreateUtilResourceTask/>
<ItemGroup>
<CompileGeneratedSources Include="GeneratedClass.cs" />
</ItemGroup>
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
<Csc Sources="#(CompileGeneratedSources )" OutputAssembly="$(OutputPath)Util.dll" TargetType="library" EmitDebugInformation="true" />
<Delete Files="#(CompileGeneratedSources )" />
</Target>
</Project>
That generates the util.dll in the project output folder but I fail to get it referenced in the consuming project.
I thought that this would work in the .csproj file:
<PropertyGroup>
<TargetFrameworks>netstandard1.6;net46</TargetFrameworks>
<BuildOutputTargetFolder>tasks</BuildOutputTargetFolder>
<VersionPrefix>0.1.1</VersionPrefix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageOutputPath>$(MSBuildThisFileDirectory)</PackageOutputPath>
<PackageId>BuildUtil</PackageId>
<!-- that does not add a refenrece in consuming projects -->
<references>
<reference file="$(OutputPath)Util.dll"/>
</references>
<files>
<file src="$(OutputPath)Util.dll"/>
</files>
</PropertyGroup>
Perhaps someone has a hint on that?
You need to declare the assembly reference in an ItemGroup, not a PropertyGroup. For example:
<ItemGroup>
<Reference Include="$(OutputPath)Util.dll" />
</ItemGroup>

How to install a NuGet package based on major version

We use myget.org as NuGet server and here I upload a package named e.g. mypackage with two different major versions e.g. 6.0.1 and 7.0.1, and there can be several versions for each major version but I always want to have the latest version though the correct specified major version. So in one branch of my code I want to have 6.0.X (X = latest) and from another branch I want to have 7.0.X
Currently I do below in my msbuild, where MyPackage id have major version in the name, but that is not scalable.
<ItemGroup>
<Packages Include="MyPackageX">
<Source>https://xxx.myget.org/myfeed/index.json</Source>
</Packages>
</ItemGroup>
<Exec Command="$(NugetExe) install %(Packages.Identity) -Source %(Source)
-OutputDirectory $(PackagesFolder)\Package -ExcludeVersion -noninteractive
-prerelease -verbosity detail" />
I would perfer to do it from a msbuild script. Is it possible?
Simplified example
Build.msbuild file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.6.3" />
</ItemGroup>
<Target Name="Build" />
</Project>
In the same folder I have a nuget.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositorypath" value="packages" />
</config>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
From my developer command I type: msbuild build.msbuild
But no NuGet is installed so it doesn't make much sense to use wildcard before this simple scenario works.

NANT CSC Build Failure: References Missing?

I have the following NANT CSC target for building winexe:
<csc target="winexe" output="${Deploy.dir}\VMIS.exe" debug="${debug}">
<sources>
<include name="${App.dir}\**\*.cs" />
<include name="${Build.dir}\AssemblyInfo.cs" />
<exclude name="${App.dir}\**\AssemblyInfo.cs" />
</sources>
<references refid="Lib.fileset">
</references>
...
</csc>
The following is the failure message:
D:\..\myClass.cs(9,17): error CS0234: The type or namespace name 'Reporting'
does not exist in the namespace 'Microsoft' (are you missing an assembly
reference?)
In myClass.cs, I have this using reference:
using Microsoft.ReportViewer.WinForms;
There is no problem to build my app in VS, but I could not build from NANT. I think that I may miss reference to Microsoft.ReportViewer.WinForms.dll in NANT build. Not sure how I can include this dll in my bin for NANT?
I have tried to modify csc target's references:
<csc ...>
...
<references refid="Lib.fileset">
<include name="Microsoft.ReportViewer.Common.dll" />
<include name="Microsoft.ReportViewer.WinForms.dll" />
</references>
...
</csc>
Still not working. Should I use COPY target to copy all the dll files from bin to $(build.dir)?
Updates: I found that those Microsoft.ReportViewer.xx.dll files in project references are not copy to local. How can I simulate copy to local in NANT for those two dll files? I guess that may resolve the issue since NANT is a build app in console and does not have knowledge about references in global cache.
NAnt is configured with the default set of DLLs for the .NET framework, and knows where those DLLs reside (e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319). When you include non-framework assemblies, whether they are yours or a 3rd party, you can include them, but use the full path of the DLL:
<include name="C:\Common\ThirdParty.dll" />
You can also use variables:
<property name="common.directory" value="C:\Common" />
...
<csc ...>
...
<references>
<inclde name="${common.directory}\ThirdParty.dll" />
</references>
</csc>
Recommended:
Use MSBuild in your NAnt script(s) to build your application.
FYI: Visual Studio uses MSBuild to compile and build your solution and projects.
<!-- Verify the right target framework -->
<property name="MSBuildPath" value="C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" />
<target name="Build">
<exec program="${MSBuildPath}">
<arg line='"${SolutionFile}"' />
<arg value="/target:Rebuild" />
<arg value="/verbosity:normal" />
<arg value="/nologo" />
</exec>
</target>
Possibility:
Copy references/files locally (i.e. using copy task). Or similarly use full paths in the include name.
Not recommended:
Use NAnt's "solution" task, or NAntContrib's "msbuild" task.
This would simplify the msbuild call but would tie you into older versions of msbuild/VS solution/project files. Newer VS solution/project files would not be supported readily.
Hope it helps.

Retrieving Assembly Version information for multiple DLLs in MSbuild

I am fairly comfortable with writing assembly version information for a given project that will output as a DLL.
However what I am looking to do now is to go through each DLL in a \Bin folder, retrieve the Assembly information and write it to a versions.txt file.
Has anyone managed to acheive a similar goal?
You can to that using WriteVersionToFile MSBuild target defined below :
<PropertyGroup>
<VersionsFile>versions.txt</VersionsFile>
</PropertyGroup>
<ItemGroup>
<!-- Assemblies for which you want versions-->
<AssemblyFiles Include="Bin\**\*.dll"/>
</ItemGroup>
<Target Name="ExtractVersions">
<GetAssemblyIdentity AssemblyFiles="#(AssemblyFiles)">
<Output TaskParameter="Assemblies" ItemName="MyAssemblyIdentities"/>
</GetAssemblyIdentity>
<Message Text="%(MyAssemblyIdentities.Name) - Version %(Version)"/>
</Target>
<Target Name="WriteVersionToFile" DependsOnTargets="ExtractVersions">
<Delete Files="$(VersionsFile)" />
<WriteLinesToFile File="$(VersionsFile)"
Lines="%(MyAssemblyIdentities.Name) - %(Version)"
Overwrite="false"
Encoding="Unicode"/>
</Target>
Output file versions.txt will look like that :
ICSharpCode.SharpZipLib - 0.85.4.369
nunit.framework - 2.4.7.0
Test.Rules - 0.2.0.0