0
votes

Ok, I am at my wits' end. We have a VB.Net application with many dependent assemblies (all contained in the solution), that builds well from with the Visual Studio IDE. Now I want to automate the build to produce three different versions (and copy the results to our installer package). After some research I came up with the following code, which I use from a C# console project:

var projectFileName = @"D:\NetCode\60-40\Applications\ControlViewer\BridgeIt.vbproj";
string target = "Build";
var properties = new Dictionary<string, string>();
properties.Add("Configuration","NBBR");
properties.Add("Platform", "AnyCPU");
var buildRequest = new BuildRequestData(projectFileName, properties, null, new[] { target }, null);
var buildParams= new BuildParameters(new ProjectCollection());
var logger = new BasicLogger();
buildParams.Loggers = new List<ILogger>() { logger};
BuildManager.DefaultBuildManager.ResetCaches();
var result = BuildManager.DefaultBuildManager.Build(buildParams, buildRequest);
Debug.Print(logger.GetLogString());

This builds all assemblies, only at the very last I get the following error:`

ERROR C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1657,5): The "GetReferenceNearestTargetFrameworkTask" task could not be instantiated from the assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.Build.Tasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Kan een object van het type NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask niet converteren naar het type Microsoft.Build.Framework.ITask. : The final remark is in Dutch and describes an `InvalidCastException'.

I tried:

  • Updating the NuGetPackages for Microsoft.Build.
  • Installing the NuGet Tools from the VS-installer
  • Registered the build dll's in the gac as explained in this link (Though I fear that this was unwise)

Well, nothing works. Could it be a C#/VB.Net compatibility issue?

Other thoughts: Most of the projects in the solution have come from VS 2015. Currently we are using VS 2017.

1
Why are you re-inventing the wheel by invoking your build from a c# console project? When MSBuild (XML syntax) is already there? By re-inventing this in c# you are bound to miss something that has already been solved somewhere.C Johnson
@CJohnson: I am not sure what solution you propose. I want to automate the build for three configurations, obfuscate, create an installer package and copy the executable and dependencies there. And then only when I need to publish. Is going the way of creating extra build configs and implementing all this in the project files the better option in your opinion?Dabblernl
Don't reinvent the wheel by writing C# code to build your code, when an msbuild xml file will do just fine. Everything you want to do can be done in an msbuild file, and with a lot less maintenance.C Johnson

1 Answers

0
votes

I applied two changes that made it work:

var buildParams = new BuildParameters(new ProjectCollection() { DefaultToolsVersion = "15.0" });

And I added this to the app.config:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>

And don't forget that I installed the NuGet Build dll's in the GAC (which I most probably should not have done).