简体   繁体   English

网站编译在MSBuild中失败,但在Visual Studio中有效

[英]Website compilation fails in MSBuild but works in Visual Studio

I am experiencing weird behavior of MSBuild tasks when I try to use it to build my VS2010 solution, which includes Web Site project. 当我尝试使用它构建我的VS2010解决方案(包括Web站点项目)时,我遇到了奇怪的MSBuild任务行为。 If I open that solution in Visual Studio 2010 and try to compile it, it works. 如果我在Visual Studio 2010中打开该解决方案并尝试编译它,它就可以工作。 However if I try to compile it from my MSBuild script, it fails with error. 但是,如果我尝试从我的MSBuild脚本编译它,它会失败并显示错误。

I have tracked it down to the fact, that there is a User Control in the WebSite directory, which does not use code behind. 我已经跟踪了这个事实,即WebSite目录中有一个User Control,它不使用后面的代码。

Steps to reproduce: 重现步骤:

1.) Imagine you have simple solution. 1.)想象一下你有简单的解决方案。 Just with a web site and two user controls: 只需一个网站和两个用户控件:

web\\Controls\\HelpButton.ascx: 网络\\控件\\ 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: 网络\\ 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: 网络\\ 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 can build this website without a problem. VS2010可以毫无问题地构建这个网站。

2.) Use following MSBuild: 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.) Run the MSBuild script: msbuild build.msbuild 3.)运行MSBuild脚本:msbuild build.msbuild

Following error is displayed: 显示以下错误:

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

It looks to me like if it compiled user controls in wrong order 'HelpButton' is placed on 'TestUserControl' and 'TestUserControl' assigns a value to property of 'HelpButton' object. 在我看来,如果它以错误的顺序编译用户控件,'HelpButton'放在'TestUserControl'上,'TestUserControl'为'HelpButton'对象的属性赋值。 Is this known issue? 这是已知的问题吗? Any suggestions how to workaround this? 有任何建议如何解决这个问题?

One workaround that didn't work for me was to execute devenv.exe from MSBuild script and let devenv.exe build solution. 一个对我不起作用的解决方法是从MSBuild脚本执行devenv.exe并让devenv.exe构建解决方案。 It compiles it sucessfully, but problem is that I can't send additional MSBuild properties this way. 它成功编译它,但问题是我无法以这种方式发送额外的MSBuild属性。

Thank you 谢谢

Edit: Output of msbuild command when executed directly against solution file: 编辑:直接针对解决方案文件执行时输出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

In the end I have end up using AspNetCompiler tasks instead of MSBuild. 最后,我最终使用AspNetCompiler任务而不是MSBuild。 Surprisingly AspNetCompiler works in situation when MSBuild tasks fails. 令人惊讶的是AspNetCompiler在MSBuild任务失败时工作。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="PrecompileWeb">
        <AspNetCompiler
            VirtualPath="/MyWebSite"
            PhysicalPath="c:\inetpub\wwwroot\MyWebSite\"
            TargetPath="c:\precompiledweb\MyWebSite\"
            Force="true"
            Debug="true"
        />
    </Target>

The only problem I had to resolve with our real production solution was, that our solution contains many other projects (assemblies, executables), not just the website. 我必须用我们真正的生产解决方案解决的唯一问题是,我们的解决方案包含许多其他项目(程序集,可执行文件),而不仅仅是网站。 For that reason I had to split build of the solution to two steps - at first only compile assemblies (using MSBuild tasks) and as second step call AspNetCompiler to compile the website. 出于这个原因,我不得不将解决方案的构建拆分为两个步骤 - 首先只编译程序集(使用MSBuild任务),第二步调用AspNetCompiler来编译网站。

Thanks for help 感谢帮助

What about this line <%@ Register Src="~/Controls/HelpButton.ascx" ... ? 那行<%@ Register Src="~/Controls/HelpButton.ascx" ... Your button seems to be located in a different directory. 您的按钮似乎位于不同的目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MSBuild与Visual Studio - 编译的差异? - MSBuild Vs Visual Studio - Differences in compilation? 解决方案重建在命令行中使用MSBuild失败,但Visual Studio重建工作正常 - Solution rebuild fails using MSBuild in command line but Visual Studio rebuild works fine Visual Studio生成解决方案,但命令行中的MSBuild失败 - Visual Studio builds solution but MSBuild from the command line fails 在 MSBuild 中找不到类型或命名空间名称,但在 Visual Studio 中有效 - The type or namespace name could not be found in MSBuild but works in Visual Studio Visual Studio 2015(Windows 10)中的.NET本机编译失败 - .NET Native compilation in Visual Studio 2015 (Windows 10) fails 在Windows XP中使用Framework 2.0将网站从Visual Studio 2005升级到Visual Studio 2008时出现编译问题 - Compilation issue while upgrading a website from Visual Studio 2005 to Visual Studio 2008 with framework 2.0 in Windows XP os的Visual Studio条件编译 - Visual studio conditional compilation for os 为什么要在msbuild Visual Studio中启动xsd执行? - Why starts xsd execution in msbuild Visual Studio? 在没有Visual Studio 2010的情况下安装MSBuild 4.0 - Installing MSBuild 4.0 without Visual Studio 2010 使用Visual Studio与MSBuild发布 - Publish with Visual Studio vs. MSBuild
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM