简体   繁体   English

在AssemblyInfo.cs中引用项目时,发布预编译的ASP.NET失败

[英]Publishing precompiled ASP.NET fails when referencing a project in AssemblyInfo.cs

When we try to publish our ASP.NET application with Precompile during publishing enabled, it will fail because we reference a project in the AssemblyInfo.cs file. 当我们尝试启用发布的过程中使用预编译功能发布ASP.NET应用程序时,它将失败,因为我们在AssemblyInfo.cs文件中引用了一个项目。

This is our project structure: 这是我们的项目结构:

Project.WebUI (ASP.NET MVC App on .NET 4.6.2)
Project.Resources (Class Library on .NET 4.6.2)
Project.Domain (Class Library on .NET 4.6.2)
Project.Services (Class Library on .NET 4.6.2)

In the AssemblyInfo.cs of Project.WebUI we are referencing Project.Resources like this: Project.WebUIAssemblyInfo.cs ,我们引用的是Project.Resources,如下所示:

using Project.Resources;

....

[assembly: AssemblyVersion(VersionInformation.FileVersion)]
[assembly: AssemblyFileVersion(VersionInformation.FileVersion)]
[assembly: AssemblyInformationalVersion(VersionInformation.ProductVersion)]

Class VersionInformation is a static class in Project.Resources . VersionInformation类是Project.Resources中的静态类。

All other assemblies also have these references for their AssemblyVersion 所有其他程序集的AssemblyVersion也具有这些引用

When we build (Debug or Release) it will pass without any errors. 当我们构建(调试或发布)时,它将顺利通过而不会出现任何错误。 When we publish the Project.WebUI project, we are getting following errors: 发布Project.WebUI项目时,出现以下错误:

Severity    Code    Description Project File    Line    Suppression State
Error       The type or namespace name 'Project' could not be found (are you missing a using directive or an assembly reference?)   Project.WebUI   Properties\AssemblyInfo.cs  3   
Error       The name 'VersionInformation' does not exist in the current context Project.WebUI   Properties\AssemblyInfo.cs  34  
Error       The name 'VersionInformation' does not exist in the current context Project.WebUI   Properties\AssemblyInfo.cs  35  
Error       The name 'VersionInformation' does not exist in the current context Project.WebUI   Properties\AssemblyInfo.cs  36  

Our publishing profile looks like this: 我们的发布资料如下:

[X] Delete all existing files prior to publish
[X] Precompile during publishing

    Precompile Options
    [ ] Allow precompiled site to be updatable
    [ ] Emit debug information

    Merge Options
    [ ] Do not merge
    [ ] Do not merge. Create a separate assembly for each page and control
    [X] Merge all outputs to a single assembly
        "Project.Views"
        [X] Treat as library component (remove the AppCode.compiled file)

Why does this error occur and how can we fix this? 为什么会发生此错误,我们该如何解决?

Here's the Output log of the error: 这是错误的输出日志:

------ Publish started: Project: Project.WebUI, Configuration: Release Any CPU ------
Connecting to C:\BuildArtifacts\...
Transformed Web.config using C:\Development\Project.WebUI\Web.Release.config into obj\Release\TransformWebConfig\transformed\Web.config.
Copying all files to temporary location below for package/publish:
obj\Release\AspnetCompileMerge\Source.
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -v \ -p C:\Development\Project.WebUI\obj\Release\AspnetCompileMerge\Source -c C:\Development\Project.WebUI\obj\Release\AspnetCompileMerge\TempBuildDir 
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\csc.exe /out:obj\Release\AssemblyInfo\AssemblyInfo.dll /target:library Properties\AssemblyInfo.cs
Properties\AssemblyInfo.cs(34,28): Error CS0103: The name 'VersionInformation' does not exist in the current context
Properties\AssemblyInfo.cs(35,32): Error CS0103: The name 'VersionInformation' does not exist in the current context
Properties\AssemblyInfo.cs(36,41): Error CS0103: The name 'VersionInformation' does not exist in the current context
Properties\AssemblyInfo.cs(3,7): Error CS0246: The type or namespace name 'Project' could not be found (are you missing a using directive or an assembly reference?)

Apparently during "Publish" the csc.exe tries to compile the AssemblyInfo.cs of our Project.WebUI , but only this file. 显然,在“发布”过程中, csc.exe尝试编译Project.WebUIAssemblyInfo.cs ,但仅编译此文件。 So it fails to get the correct references to Project.Resources . 因此,它无法获得对Project.Resources的正确引用。

The solution my colleague brought works for us: 我的同事带来的解决方案为我们工作:

  • Removing all Version attributes from all AssemblyInfo.cs files 从所有AssemblyInfo.cs文件中删除所有Version属性
  • Create a new file in the Project.Resources project like AssemblyVersion.cs Project.Resources项目中创建一个新文件,例如AssemblyVersion.cs
  • The only content of that file are the version attributes like this 该文件的唯一内容是这样的版本属性

     [assembly: AssemblyFileVersion(VersionInformation.FileVersion)] 
  • Create a link to AssemblyVersion.cs in every other project 在每个其他项目中创建一个指向AssemblyVersion.cs的链接

Create CommonAssemblyInfo.cs in the same folder as the .SLN and put the attributes you like to share there. 在与.SLN相同的文件夹中创建CommonAssemblyInfo.cs,然后将要共享的属性放在此处。

One project at a time: Select the Properties group, right-click Add > Existing Item, select CommonAssemblyInfo.cs and choose Add As Link from the Add button menu. 一次一个项目:选择“属性”组,右键单击“添加”>“现有项”,选择CommonAssemblyInfo.cs,然后从“添加”按钮菜单中选择“添加为链接”。 Remove those attributes from AssemblyInfo.cs. 从AssemblyInfo.cs中删除这些属性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM