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. If I open that solution in Visual Studio 2010 and try to compile it, it works. However if I try to compile it from my MSBuild script, it fails with error.
I have tracked it down to the fact, that there is a User Control in the WebSite directory, which does not use code behind.
Steps to reproduce:
1.) Imagine you have simple solution. Just with a web site and two user controls:
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 can build this website without a problem.
2.) Use following 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
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 /We开发者_开发知识库bSite -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. 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. It compiles it sucessfully, but problem is that I can't send additional MSBuild properties this way.
Thank you
Edit: Output of msbuild command when executed directly against solution file:
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. Surprisingly AspNetCompiler works in situation when MSBuild tasks fails.
<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.
Thanks for help
What about this line <%@ Register Src="~/Controls/HelpButton.ascx" ...
? Your button seems to be located in a different directory.
精彩评论