MSBuild失败,但在Visual Studio内部构建工作正常

时间:2021-11-16 17:03:42

C#, .NET 2.0

C#,.NET 2.0

I have an ASP.NET website in a solution, with 2 other projects (used as library references). When I build (debug or release) in Visual Studio, everything works fine. However, building with MSBuild fails.

我在一个解决方案中有一个ASP.NET网站,还有另外两个项目(用作库引用)。当我在Visual Studio中构建(调试或发布)时,一切正常。但是,使用MSBuild构建失败。

This build had been working (it's actually invoked via a nAnt task). The only thing that has changed is that I have a new user control whose Type I am referencing in my code behind.

这个版本一直在工作(它实际上是通过nAnt任务调用的)。唯一改变的是我有一个新的用户控件,其类型I在我的代码后面引用。

The offending code is in my ASPX code behind. MessageAlert is the UserControl:

违规代码在我的ASPX代码中。 MessageAlert是UserControl:

MessageAlert userControl = this.LoadControl("~/UserControls/MessageAlert.ascx") as MessageAlert;
        userControl.UserMessage = message;
        this.UserMessages.Controls.Add(userControl);

In order to get Visual Studio to recognize the type 'MessageAlert' I had to:

为了使Visual Studio能够识别“MessageAlert”类型,我不得不:

1) Set the ClassName="MessageAlert" in the @Control markup at the top of the user control (because using the auto-generated UserControls_MessageAlert wasn't working either)

1)在用户控件顶部的@Control标记中设置ClassName =“MessageAlert”(因为使用自动生成的UserControls_MessageAlert也不起作用)

2) Register the user control in the markup of my ASPX, using an @Register

2)使用@Register在我的ASPX的标记中注册用户控件

3) Add a "using ASP" to the top of my code behind

3)在我的代码后面添加一个“使用ASP”

After those steps, I could successfully reference the MessageAlert type in my codebehind from visual studio. But from MSBuild I get "The type or namespace name 'MessageAlert' could not be found (are you missing a using directive or an assembly reference?) "

完成这些步骤后,我可以从visual studio成功引用我的代码隐藏中的MessageAlert类型。但是从MSBuild我得到“无法找到类型或命名空间名称'MessageAlert'(你是否缺少using指令或程序集引用?)”

The MSBuild execution is very simple - it points the the very same solution file and sets the configuration property to release.

MSBuild执行非常简单 - 它指向完全相同的解决方案文件并将配置属性设置为release。

It seems, based on the # of steps I had to go through to get Type references to MessageAlert in Visual Studio, that there is something missing in the MSBuild process. But what? Doesn't Visual Studio in fact invoke MSBuild behind the scenes?

看来,基于我在Visual Studio中获取对MessageAlert的类型引用所需的步骤,MSBuild进程中缺少某些内容。但是什么?事实上,Visual Studio在幕后不会调用MSBuild吗?

Is there a better way to reference a UserControl type in the code behind of an ASPX?

有没有更好的方法在ASPX背后的代码中引用UserControl类型?

EDIT: To clarify, the MessageAlert user control is not in the other referenced assemblies/projects. I mentioned them because, together with the website, the compose the Solution file, which is the same sln file being built by MS Build.

编辑:为了澄清,MessageAlert用户控件不在其他引用的程序集/项目中。我提到它们是因为,与网站一起构成解决方案文件,这是由MS Build构建的同一个sln文件。

3 个解决方案

#1


I also had this problem exactly as you described above.

我也完全按照你的描述解决了这个问题。

I was at a loss as to why this was and as a final mesure, decided to try the following with success.

我不知道为什么会这样,并且作为最后的结果,我决定尝试以下成功。

Change the class name of the user control - the actual class name not the class name in the aspx file.

更改用户控件的类名 - 实际的类名而不是aspx文件中的类名。

so in your example change the class name to MessageAlert (from UserControls_MessageAlert) also change this in the inherits in the aspx

所以在你的例子中将类名更改为MessageAlert(来自UserControls_MessageAlert)也在aspx中的继承中更改此名称

Hope this helps someone

希望这有助于某人

#2


This seems to be a HintPath problem within your Solution. If you add your referenced assemblies to your project as linked project items (as well as project references), MSBuild will used the linked assemblies in preference to the HintPath and even AssemblyFolders.

这似乎是您的解决方案中的HintPath问题。如果将引用的程序集作为链接的项目项(以及项目引用)添加到项目中,MSBuild将使用链接的程序集优先于HintPath甚至AssemblyFolders。

An other way would be to add a key with any name to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio_YourVersion_\AssemblyFolders, and set its (Default) value of type REG_SZ to the path with your assemblies.

另一种方法是将具有任何名称的键添加到HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ VisualStudio_YourVersion_ \ AssemblyFolders,并将其类型为REG_SZ的(默认)值设置为包含程序集的路径。

#3


Try removing the "using ASP" section from your page and make sure the "ClassName" property of the @Control directive actually matches the name of the class. We had the ClassName value set to "FilterElement" while the actual class name was "Controls_FilterElement".

尝试从页面中删除“使用ASP”部分,并确保@Control指令的“ClassName”属性实际上与类的名称匹配。我们将ClassName值设置为“FilterElement”,而实际的类名称为“Controls_FilterElement”。

So, in summary:

所以,总结一下:

  • Verify your class name in the .ascx.vb and the inherits property of the @Control directive on the .ascx. e.g. Inherits="FilterElement"
  • 验证.ascx.vb中的类名和.ascx上@Control指令的inherits属性。例如继承= “FilterElement”

  • Add the "ClassName" property to the @Control directive with the same value, e.g. ClassName="FilterElement"
  • 将“ClassName”属性添加到具有相同值的@Control指令,例如类名= “FilterElement”

  • On the referring page / control add the "Reference" element. e.g.

    在引用页面/控件上添加“引用”元素。例如

    <%@ Reference Control="~/Controls/FilterElement.ascx" %>

    <%@ Reference Control =“〜/ Controls / FilterElement.ascx”%>

  • In the code behind, refer to the class directly. Do not use the ASP namespace (this seems to be what causes msbuild the difficulty). e.g. (in VB)

    在后面的代码中,直接引用该类。不要使用ASP命名空间(这似乎是造成msbuild困难的原因)。例如(在VB中)

    Dim ctrlElement As FilterElement = CType(LoadControl("~/controls/filterelement.ascx"), FilterElement)

    Dim ctrlElement As FilterElement = CType(LoadControl(“〜/ controls / filterelement.ascx”),FilterElement)

#1


I also had this problem exactly as you described above.

我也完全按照你的描述解决了这个问题。

I was at a loss as to why this was and as a final mesure, decided to try the following with success.

我不知道为什么会这样,并且作为最后的结果,我决定尝试以下成功。

Change the class name of the user control - the actual class name not the class name in the aspx file.

更改用户控件的类名 - 实际的类名而不是aspx文件中的类名。

so in your example change the class name to MessageAlert (from UserControls_MessageAlert) also change this in the inherits in the aspx

所以在你的例子中将类名更改为MessageAlert(来自UserControls_MessageAlert)也在aspx中的继承中更改此名称

Hope this helps someone

希望这有助于某人

#2


This seems to be a HintPath problem within your Solution. If you add your referenced assemblies to your project as linked project items (as well as project references), MSBuild will used the linked assemblies in preference to the HintPath and even AssemblyFolders.

这似乎是您的解决方案中的HintPath问题。如果将引用的程序集作为链接的项目项(以及项目引用)添加到项目中,MSBuild将使用链接的程序集优先于HintPath甚至AssemblyFolders。

An other way would be to add a key with any name to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio_YourVersion_\AssemblyFolders, and set its (Default) value of type REG_SZ to the path with your assemblies.

另一种方法是将具有任何名称的键添加到HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ VisualStudio_YourVersion_ \ AssemblyFolders,并将其类型为REG_SZ的(默认)值设置为包含程序集的路径。

#3


Try removing the "using ASP" section from your page and make sure the "ClassName" property of the @Control directive actually matches the name of the class. We had the ClassName value set to "FilterElement" while the actual class name was "Controls_FilterElement".

尝试从页面中删除“使用ASP”部分,并确保@Control指令的“ClassName”属性实际上与类的名称匹配。我们将ClassName值设置为“FilterElement”,而实际的类名称为“Controls_FilterElement”。

So, in summary:

所以,总结一下:

  • Verify your class name in the .ascx.vb and the inherits property of the @Control directive on the .ascx. e.g. Inherits="FilterElement"
  • 验证.ascx.vb中的类名和.ascx上@Control指令的inherits属性。例如继承= “FilterElement”

  • Add the "ClassName" property to the @Control directive with the same value, e.g. ClassName="FilterElement"
  • 将“ClassName”属性添加到具有相同值的@Control指令,例如类名= “FilterElement”

  • On the referring page / control add the "Reference" element. e.g.

    在引用页面/控件上添加“引用”元素。例如

    <%@ Reference Control="~/Controls/FilterElement.ascx" %>

    <%@ Reference Control =“〜/ Controls / FilterElement.ascx”%>

  • In the code behind, refer to the class directly. Do not use the ASP namespace (this seems to be what causes msbuild the difficulty). e.g. (in VB)

    在后面的代码中,直接引用该类。不要使用ASP命名空间(这似乎是造成msbuild困难的原因)。例如(在VB中)

    Dim ctrlElement As FilterElement = CType(LoadControl("~/controls/filterelement.ascx"), FilterElement)

    Dim ctrlElement As FilterElement = CType(LoadControl(“〜/ controls / filterelement.ascx”),FilterElement)