Search code examples
c#.netmsbuildvisual-studio-extensionscsproj

DTE.SolutionBuild not building project


im trying to build a csproj in a Visual Studio Extension (.vsix) using the DTE.SolutionBuild interface:

private void BuildProject(Project vsProject)
{
        var sb = vsProject.DTE.Solution.SolutionBuild as SolutionBuild;
        sb.SolutionConfigurations.Item(1).Activate();
        sb.BuildProject(sb.SolutionConfigurations.Item(1).Name, vsProject.UniqueName, true);
}

But for some reason, the project did not compiled. No errors are showed, a folder with the name of the SolutionConfiguration is created, but the project assemblies are not generated. Any help is apreciated.

UPDATE1: I tried this too and the result are the same:

Microsoft.Build.Evaluation.Project p = new Microsoft.Build.Evaluation.Project(vsProject.FullName);
p.Build();

Solution

  • I got this way:

    private void Execute(object sender, EventArgs e)
    {
        ThreadHelper.ThrowIfNotOnUIThread();
    
        _SelectedObject = this.GetProjectItem();
        _VsProject = _SelectedObject.ContainingProject;
        this.BuildProject(_VsProject);
    }
    
    private void BuildProject(EnvDTE.Project vsProject)
    {
        DTE2 dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2;
        dte.Events.BuildEvents.OnBuildDone -= BuildEvents_OnBuildDone;
        dte.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone;
        dte.ExecuteCommand("Build.RebuildSolution");
    }
    
    private void BuildEvents_OnBuildDone(vsBuildScope Scope, vsBuildAction Action)
    {
        //Your action after build here
    }