1

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 = new Microsoft.Build.Evaluation.Project(@"C:\Users\Documents\Visual Studio 2010\Projects\TestMvc\TestMvc\TestMvc.csproj");

            var logger = new OutputLogger(_panel);
            List<Microsoft.Build.Framework.ILogger> loggers = new List<Microsoft.Build.Framework.ILogger>();
            loggers.Add(logger);
            var targets = new string[] 
            { 
                "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. To see what's happening I enabled diagnostics output in both cases. What I've got: Console command produced the following log:

Target "CoreCompile: (TargetId:37)" in file "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.targets" from project "D:\Users\Documents\Visual Studio 2010\Projects\MvcTest2\MvcTest3\MvcTest3.csproj" (target "Compile" depends on it):
Building target "CoreCompile" completely.
Output file "obj\Debug\MvcTest3.dll" does not exist.
Using "Csc" task from assembly "Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Task "Csc" (TaskId:20)
  C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe .....
  Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1 (TaskId:20)
  Copyright (C) Microsoft Corporation. All rights reserved. (TaskId:20)
   (TaskId:20)
Done executing task "Csc". (TaskId:20)

When calling from API, I got the following log:

Target "CoreCompile" in file "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.targets":
Building target "CoreCompile" completely.
Output file "obj\Debug\MvcTest3.dll" does not exist.
Using "Csc" task from assembly "Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Task "Csc"
Done executing task "Csc".

So Csc task is launched in both cases, however, it does not produce any output files when launched by API (i checked this even in file monitor, no files are produced).

4

0 回答 0