I am trying to package a Visual Studio .csproj with MSBuild programmatically (using namespace Microsoft.Build.Evaluation). I can correctly package this project with command like like
MSBuild"C:\Users\Documents\Visual Studio 2010\Projects\TestMvc\TestMvc\TestMvc.csproj"/T:Package
But when I use the following code (I implemented my own logger, but it does not matter here):
var project =newMicrosoft.Build.Evaluation.Project(@"C:\Users\Documents\Visual Studio 2010\Projects\TestMvc\TestMvc\TestMvc.csproj");var logger =newOutputLogger(_panel);List<Microsoft.Build.Framework.ILogger> loggers =newList<Microsoft.Build.Framework.ILogger>();
loggers.Add(logger);var targets =newstring[]{"Package"};var result = project.Build(targets, loggers);Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.UnloadProject(project);
Solution is not built. Logger gives me an error "Output file "obj\Debug\TestMvc.dll" does not exist."
After I build this project in Visual Studio manually (when all the binaries are successfully built), then this code packages the project succesfully. So there are all nesessary binaries available already.
The question is: why "Package" target launched programmatically is not equal to "Package" lanunched by command line and how to force the former do the same action as its commandlike counterpart?
UPD: If I use command line, I get the following result
Build started 17.12.201218:48:21.Project"TestMvc\TestMvc.csproj" on node 1(Package target(s)).ValidateGlobalPackageSetting:
$(PackageAsSingleFile)isTrue
$(PackageFileName)is obj\Debug\Package\TestMvc.zip.Validating...GenerateTargetFrameworkMonikerAttribute:Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.CoreCompile:
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Csc.exe /noconfig ......CopyFilesToOutputDirectory:Copying file from"obj\Debug\TestMvc.dll" to "C:\temp\build\TestMvc.dll".TestMvc-> C:\temp\build\TestMvc.dllCopying file from"obj\Debug\TestMvc.pdb" to "C:\temp\build\TestMvc.pdb".
If I use programmatic launch, I get the following log:
Project"TestMvc\TestMvc.csproj"(Package target(s)):TargetValidateGlobalPackageSetting:
$(PackageAsSingleFile)isTrue
$(PackageFileName)is obj\Debug\Package\TestMvc.zip.Validating...TargetGenerateTargetFrameworkMonikerAttribute:Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.TargetCopyFilesToOutputDirectory:Copying file from"obj\Debug\TestMvc.dll" to "C:\temp\build\TestMvc.dll".
c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(2868,9): error MSB3021:Unable to copy file "obj\Debug\TestMvc.dll" to "C:\temp\build\TestMvc.dll".Couldnot find file 'obj\Debug\TestMvc.dll'.Done building target "CopyFilesToOutputDirectory"in project "TestMvc.csproj"-- FAILED.Done building project "TestMvc.csproj"-- FAILED.
So CoreComplile target is not launched and I still don't understand why.
UPD2. If I save my project in code by calling project.Save("filename.xml"), resulting project file is the same as my TestMvc.csproj. So by this I've checked that projects are similar.
UPD3. Same errors occurs for Build target.