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.
Related
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
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
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.
I installed the folowing assembly with NuGet Package Manager in Visual Studio 2017
Microsoft.Build;
Microsoft.Build.Framework.
Microsoft.Build.Utilities.Core
All is in version 15
I want to build a c++ project with Msbuild
public static void Build(string namePerozhe)
{
Logger l = new Logger();
ProjectCollection pc = new ProjectCollection();
pc.DefaultToolsVersion = "15.0";
pc.Loggers.Add(l);
Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();
GlobalProperty.Add("Configuration", "Release");
GlobalProperty.Add("Platform", "Win32");
BuildRequestData buildRequest = new BuildRequestData(namePerozhe, GlobalProperty, null, new[] { "Build" }, null);
BuildParameters buildParameters = new BuildParameters(pc)
{
OnlyLogCriticalEvents = false,
DetailedSummary = true,
Loggers = new List<Microsoft.Build.Framework.ILogger> { l }.AsEnumerable()
};
var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);
}
But i get the below error :
The "SetEnv" task could not be loaded from the assembly
C:\Program Files\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\
Microsoft.Build.CppTasks.Common.dll.
Could not load file or assembly 'Microsoft.Build.Utilities.Core, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
The system cannot find the file specified.
Confirm that the declaration is correct, that the assembly and all its dependencies are available,
and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
when i build this project in visual studio it will build with no error . but when i want to build it programmatically this error will throw up.
there is another question that don't have any answer that help me .
I had to do three main steps:
Download the version 15+ Microsoft.Build packages from NuGet. The ones that aren't from NuGet are out of date.
Download the Microsoft.Build.Runtime package. If you don't download that package, your project might still build, but it'll crash at runtime on LoadProject.
Hooked up bindingRedirects in my app.config. See: https://developercommunity.visualstudio.com/content/problem/24545/microsoftbuildcpptaskscommon-references-incorrect.html
I had similar problem and solved it by adding bindingRedirect to app.config for every msbuild assembly.
Just look at C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config and copy necessary blocks to your app.config.
make sure you have below blocks in your C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\msbuild.exe.config
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Tasks.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.0.0.0"/>
</dependentAssembly>
Or you may use your own msbuild.exe, make sure you point your env PATH to the right one.
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.