【发布时间】:2011-09-12 07:28:26
【问题描述】:
当我尝试使用它来构建我的 VS2010 解决方案(包括网站项目)时,我遇到了 MSBuild 任务的奇怪行为。如果我在 Visual Studio 2010 中打开该解决方案并尝试编译它,它就可以工作。但是,如果我尝试从我的 MSBuild 脚本编译它,它会失败并出现错误。
我已经追查到,网站目录中有一个用户控件,它不使用后面的代码。
重现步骤:
1.) 假设您有简单的解决方案。只需一个网站和两个用户控件:
web\Controls\HelpButton.ascx:
<%@ Control Language="C#" ClassName="HelpButton" %>
<script runat="server">
private string _helpUrl;
public string HelpUrl
{
get { return _helpUrl; }
set { _helpUrl = value; }
}
</script>
web\AnotherFolder\TestUserControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TestUserControl.ascx.cs" Inherits="AnotherFolder_TestUserControl" %>
<%@ Register Src="~/Controls/HelpButton.ascx" TagPrefix="myCtrl" TagName="HelpButton" %>
<myCtrl:HelpButton runat="server" ID="HelpButton" />
web\AnotherFolder\TestUserControl.ascx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class AnotherFolder_TestUserControl : System.Web.UI.UserControl
{
protected override void OnLoad(EventArgs e)
{
this.HelpButton.HelpUrl = "trada";
base.OnLoad(e);
}
}
VS2010可以毫无问题地建立这个网站。
2.) 使用以下 MSBuild:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets="Build">
<PropertyGroup>
<SolutionProperties>Configuration=Release</SolutionProperties>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="">
<MSBuild Projects="BuildTest.sln" Targets="Build" Properties="$(SolutionProperties)" RebaseOutputs="true">
</MSBuild>
</Target>
</Project>
3.) 运行 MSBuild 脚本:msbuild build.msbuild
显示以下错误:
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.235]
Copyright (C) Microsoft Corporation 2007. All rights reserved.
Build started 9/9/2011 2:21:50 PM.
Project "d:\Src\MSBuildIssue\build.msbuild" on node 1 (default targets).
Project "d:\Src\MSBuildIssue\build.msbuild" (1) is building "d:\Src\MSBuildIssue\BuildTest.sln" (2) on node 1 (Build target(s)).
ValidateSolutionConfiguration:
Building solution configuration "Release|Any CPU".
Project "d:\Src\MSBuildIssue\BuildTest.sln" (2) is building "d:\Src\MSBuildIssue\WebSite.metaproj" (3) on node 1 (default targets).
Build:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -v /WebSite -p WebSite\ -u -f PrecompiledWeb\WebSite\
d:\Src\MSBuildIssue\WebSite\AnotherFolder\TestUserControl.ascx.cs(12): error CS1061: 'System.Web.UI.UserControl' does not contain a definition for 'HelpUrl' and no extension method 'HelpUrl' accepting a first argument of type 'System.Web.UI.UserControl' could be found (are you missing a using directive or an assembly reference?) [d:\Src\MSBuildIssue\WebSite.metaproj]
Done Building Project "d:\Src\MSBuildIssue\WebSite.metaproj" (default targets) -- FAILED.
Done Building Project "d:\Src\MSBuildIssue\BuildTest.sln" (Build target(s)) -- FAILED.
Done Building Project "d:\Src\MSBuildIssue\build.msbuild" (default targets) -- FAILED.
Build FAILED.
"d:\Src\MSBuildIssue\build.msbuild" (default target) (1) ->
"d:\Src\MSBuildIssue\BuildTest.sln" (Build target) (2) ->
"d:\Src\MSBuildIssue\WebSite.metaproj" (default target) (3) ->
(Build target) ->
d:\Src\MSBuildIssue\WebSite\AnotherFolder\TestUserControl.ascx.cs(12): error CS1061: 'System.Web.UI.UserControl' does not contain a definition for 'HelpUrl' and no extension method 'HelpUrl' accepting a first argument of type 'System.Web.UI.UserControl' could be found (are you missing a using directive or an assembly reference?) [d:\Src\MSBuildIssue\WebSite.metaproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:02.66
在我看来,如果它以错误的顺序编译用户控件,则将“HelpButton”放置在“TestUserControl”上,并且“TestUserControl”将值分配给“HelpButton”对象的属性。 这是已知问题吗?有什么建议可以解决这个问题吗?
对我不起作用的一种解决方法是从 MSBuild 脚本执行 devenv.exe 并让 devenv.exe 构建解决方案。它编译成功,但问题是我不能以这种方式发送额外的 MSBuild 属性。
谢谢
编辑:直接针对解决方案文件执行时 msbuild 命令的输出:
c:\Tmp\MSBuildIssue>msbuild.exe BuildTest.sln /target:Build /p:Configuration="Debug",Platform="Any CPU"
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.235]
Copyright (C) Microsoft Corporation 2007. All rights reserved.
Build started 9/12/2011 12:05:38 PM.
Project "c:\Tmp\MSBuildIssue\BuildTest.sln" on node 1 (Build target(s)).
ValidateSolutionConfiguration:
Building solution configuration "Debug|Any CPU".
Project "c:\Tmp\MSBuildIssue\BuildTest.sln" (1) is building "c:\Tmp\MSBuildIssue\WebSite.metaproj" (2) on node 1 (default targets).
Build:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -v /WebSite -p WebSite\ -u -f -d PrecompiledWeb\WebSite\
c:\Tmp\MSBuildIssue\WebSite\AnotherFolder\TestUserControl.ascx.cs(12): error CS1061: 'System.Web.UI.UserControl' does not contain a definition for 'HelpUrl' and no extension method 'HelpUrl' accepting a first argument of type 'System.Web.UI.UserControl' could be found (are you missing a using directive or an assembly reference?) [c:\Tmp\MSBuildIssue\WebSite.metaproj]
Done Building Project "c:\Tmp\MSBuildIssue\WebSite.metaproj" (default targets) -- FAILED.
Done Building Project "c:\Tmp\MSBuildIssue\BuildTest.sln" (Build target(s)) -- FAILED.
Build FAILED.
"c:\Tmp\MSBuildIssue\BuildTest.sln" (Build target) (1) ->
"c:\Tmp\MSBuildIssue\WebSite.metaproj" (default target) (2) ->
(Build target) ->
c:\Tmp\MSBuildIssue\WebSite\AnotherFolder\TestUserControl.ascx.cs(12): error CS1061: 'System.Web.UI.UserControl' does not contain a definition for 'HelpUrl' and no extension method 'HelpUrl' accepting a first argument of type 'System.Web.UI.UserControl' could be found (are you missing a using directive or an assembly reference?) [c:\Tmp\MSBuildIssue\WebSite.metaproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:01.77
【问题讨论】:
-
如果您尝试直接在命令行上执行 MsBuild,输出是什么:
msbuild.exe BuildTest.sln /target:Build /p:Configuration="Debug",Platform="AnyCPU"? -
我已将命令输出添加到原始帖子中。这是同样的错误。