MSBuild using BuildManager cannot find bootstrapper path or SignTool.exe - c#

I'm trying to automate builds using the MSBuild API - "BuildManager".
The following code works fine for building solutions/projects but fails when it comes to publishing.
The project publishes just fine when using the Visual Studio Publish page.
var collection = new ProjectCollection();
var parameters = new BuildParameters(collection);
parameters.Loggers = new List<ILogger> { Logger };
parameters.MaxNodeCount = Environment.ProcessorCount; //maxcpucount
var globalProperties = new Dictionary<string, string>();
globalProperties.Add("Configuration", "Debug");
globalProperties.Add("Platform", "AnyCPU");
globalProperties.Add("OutDir", binPath);
globalProperties.Add("OutputPath", publishPath);
globalProperties.Add("ApplicationVersion", version.ToString());
globalProperties.Add("GenerateBootstrapperSdkPath", #"C:\Program Files (x86)\Microsoft SDKs\ClickOnce Bootstrapper\");
//Doesn't work!
globalProperties.Add("SignToolPath", #"C:\Program Files (x86)\Microsoft SDKs\ClickOnce\SignTool\signtool.exe");
BuildManager.DefaultBuildManager.ResetCaches();
var buildRequest = new BuildRequestData(projectFile.FileInfo.FullName, globalProperties, "4.0", new[] { "Rebuild", "Publish" }, null);
var buildResult = BuildManager.DefaultBuildManager.Build(parameters, buildRequest);
If I run this code, it fails with the following error:
An error occurred while signing: SignTool.exe not found.
As you can see I'm adding the global property called "GenerateBootstrapperSdkPath", which if its not there leads to this error:
Could not find required file 'setup.bin' in
'C:\PathToProject\Engine'.
This seems to be a vicious cycle, as soon as I specify a path for the bootstrapper, it can't find the SignTool.exe, if I don't it can't find the setup.bin.
Unfortunately the global property "SignToolPath" doesn't seem to do anything.
Any Ideas?

There was an old problem of locating bootstrapper by MSBuild which could be solved by creating registry key:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\GenericBootstrapper\4.0]
#="0"
"Path"="C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Bootstrapper\\"
Just set it to your SDK version and try again. Or perhaps use the path to your bootstrapper in BuildManager code based on the above one instead of:
C:\Program Files (x86)\Microsoft SDKs\ClickOnce Bootstrapper\
According to one of old blog entries the following registry values are resolved in search for the bootstrapper path and finally your local project folder under the Engine sub folder is checked and when not found MSBuild throws MSB3147 error:
HKLM\Software\Microsoft\GenericBootstrapper\<.NET Tools Version>\
HKLM \Software\Microsoft.NetFramework\SDKInstallRoot\Bootstrapper
HKLM \Software\Microsoft\VisualStudio\\InstallDir\Bootstrapper
Team build for ClickOnce application with bootstrapper
I understand from your description that finding solution to bootstrapper location would solve your problem since in that configuration you did not observe any SignTool.exe problems.

Related

JsReport using C# .NET core and Vs

I am trying to learn JsReport using c# and .NET core and visual studio. I have installed the required nuget packages (jsreport.binary and jsreport.Local). I am trying to replicate the code found Here. The problem is that I keep getting the following error.
***Severity Code Description Project File Line Suppression State
Error MSB3021 Unable to copy file "C:\Users\jmodiba\source\repos\js\js\obj\Debug\net6.0\apphost.exe" to "bin\Debug\net6.0\js.exe". Access to the path 'bin\Debug\net6.0\js.exe' is denied. js C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 5097***
here is the code,
using jsreport.Binary;
using jsreport.Local;
using jsreport.Types;
var rs = new LocalReporting().UseBinary(JsReportBinary.GetBinary()).AsUtility().Create();
await rs.RenderAsync(new RenderRequest()
{
Template = new Template()
{
Recipe = Recipe.ChromePdf,
Engine = Engine.None,
Content = "Hello from pdf"
}
});
I managed to find the problem. My antivirus kept on moving the .exe file created to quarantine. Once I created an exception in the antivirus setting my code ran correctly.

Selenium C# Mstest - chromedriver.exe does not exist. Getdirectory() referring some-other directory while executing vis mstest commands

when i'm running the selenium test from VS2017 it is able to pick the drivers successfully BUT when i run the same test using mstest command - internally it is referring some other directory!
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\mstest.exe/
testcontainer:..\Test\Sun.TestAutomation.dll /test:"myfristtest"
/resultsfile:..\Test\TestResultLog.trx //Mstest commands
OpenQA.Selenium.DriverServiceNotFoundException: OpenQA.Selenium.DriverServiceNotFoundException: The file
C:\Test\xsed_2018-12-07 10_55_51\Out\chromedriver.exe does not exist.
The driver can be downloaded at
http://chromedriver.storage.googleapis.com/index.html.
code:
this.DriversPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory()));
additional information:
drivers are available in debug folder why my mstest is referring the drivers in "Out" folder ??
This post is a bit old but since it was brought back to the the front, this may help someone.
I would download the ChromeDriver Nuget package. This way you always get the latest version.
Right click on your project > properties. Click on Build tab.
set Conditional compilation symbols = _PUBLISH_CHROMEDRIVER
under output path set: bin\Debug\
Once installed, clean solution and rebuild and you should see the file in the bin dir.
for your chromedriver call it should look something like this:
Driver = new ChromeDriver(Path.Combine(GetBasePath, #"bin\debug"), options);
Then add the GetBasePath code:
public static string GetBasePath
{
get
{
var basePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
basePath = basePath?.Substring(0, basePath.Length - 10);
return basePath;
}
}
This PC -> Properties -> advanced system settings -> Environment variables -> system variables -> Varible PATH add folder, where you have chromedriver.exe

mscorlib not found when building Xamarin.Android project with CakeBuild

I'm in the process of integrating a Xamarin Android project into our CI pipeline. We already use CakeBuild for other .NET projects and so I wanted to use it here, as well.
The problem is that I always get the following error message when trying to build with Cake:
C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(406,2): error : Could not load assembly
'mscorlib, Version=0.0.0.0, Culture=neutral, PublicKeyToken='. Perhaps it doesn't exist in the Mono for Android profile? [C:\[myproject].csproj]
Building works in Visual Studio 2015 and using the Visual Studio Developer Command Prompt. Because of this, I was thinking it had something to do with the environment variables that are set in VS and via the VS command prompt. So what I did was make a small batch file:
call "%vs140comntools%vsvars32.bat"
Powershell.exe ./build.ps1 -Target Build
But I'm getting the exact same error. In my projects, there is no explicit reference to mscorlib.
The Cake build task looks like this:
Task("Build")
.IsDependentOn("Restore-NuGet-Packages")
.Does(() =>
{
var settings = new MSBuildSettings()
{
ArgumentCustomization = args =>
{
args = args.Append("/t:PackageForAndroid");
args = args.Append("/p:TargetFrameworkRootPath=\"C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\"");
return args;
},
Verbosity = Verbosity.Normal
};
settings.SetConfiguration(configuration);
MSBuild("../myproject.csproj", settings);
});
I had to add the TargetFrameworkRootPath because it won't find the reference assemblies if I do not set it explicitly.
I'm wondering what else to do to replicate the build environment of VS / VS command prompt.
Are you targeting .NET Standard or is this still using PCL?
What does the verbose output look like? It should give you the exact MSBuild command being executed.
call "%vs140comntools%vsvars32.bat"
Powershell.exe ./build.ps1 -Target Build -Verbosity Diagnostic

Building a .sln file with Microsoft.Build

I'm using VS2012 and I want to build a .sln file programatically using c#. I understand I need to use the Microsoft.Build api to do so. I've gotten as far as building the solution
string slnPath= #"C:\Path\To\solution.sln";
ProjectCollection pc = new ProjectCollection();
List<ILogger> loggers = new List<ILogger>();
loggers.Add(new ConsoleLogger());
Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();
GlobalProperty.Add("Configuration", "Debug");
GlobalProperty.Add("Platform", "Win32");
BuildRequestData BuildRequest = new BuildRequestData(projectFileName, GlobalProperty, "4.0", new string[] { "Build" }, null);
BuildParameters bp = new BuildParameters(pc);
bp.Loggers = loggers;
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, BuildRequest);
this executes the build command, but I get an error saying:
error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
Inside the .vcxproj the line that gives the error is:
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
I've tried setting the Environment Variable VCTargetsPath to be
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\
But I then get an error:
error MSB4127: The "SetEnv" task could not be instantiated from the assembly "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Build.CppTasks.Common.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 Micros
oft.Build.Framework.
Does anyone have any suggestions as to how I can fix this?
EDIT: I see this appears to be a bug in Visual studio, but I can't reproduce the issue mentioned in the comment. If run a Developer command prompt, I can build solutions using MSBuild.

Compiling Silverlight solution from C# fails with error "The Silverlight 4 SDK is not installed"

I am trying to compile a Silverlight project programatically. Initially I just wrapped MsBuild.exe, but had an error saying:
C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v5.0\Microsoft.Silverlight.Common.targets(104,9): The Silverlight 4 SDK is not installed.
I am using SilverLight 5 and have the SDK for both Silverlight 4 and Silverlight 5 installed. The solution compiles fine in Visual Studio. I followed the advice in this question:
MSBuild command-line error - Silverlight 4 SDK is not installed
I switched from running:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe
to:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
This solved the problem, but due to a change in requirements I now need to use Microsoft.Build without wrapping the MsBuild exe. I have the following code:
var globalProperty = new Dictionary<string, string> { { "Configuration", "Debug"}, { "Platform", "Any CPU" } };
var buildParameters = new BuildParameters(new ProjectCollection()) { Loggers = new List<ILogger> { this.logger } };
var buildRequest = new BuildRequestData(#"C:\example\solution.sln", globalProperty, "4.0", new[] {"Build" }, null);
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);
But again, I am now seeing:
ValidateSilverlightFrameworkPaths: ERROR C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v5.0\Microsoft.Silverlight.Common.targets(104,9): The Silverlight 4 SDK is not installed..
Is there a way to compile using x86 MsBuild from C# code without wrapping MsBuild.exe?
You have to set the target platform to "x86" in the project options of your "Build" project.
I think you have to create a specific Process that will launch that specific MSBuild for you.
var p = new Process();
p.StartInfo = new ProcessStartInfo(#"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe")
p.StartInfo.Arguments = string.Format(#"{0}\{1}.csproj", _args.ProjectPath, _args.ProjectName)
p.Start();
Or whatever path for your solution.

Categories

Resources