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



当我们尝试在启用发布期间预编译的情况下发布我们的 ASP.NET 应用程序时,它将失败,因为我们引用了AssemblyInfo.cs文件中的项目。

这是我们的项目结构:

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)

Project.WebUIAssemblyInfo.cs中,我们引用了Project.Resources,如下所示:

using Project.Resources;
....
[assembly: AssemblyVersion(VersionInformation.FileVersion)]
[assembly: AssemblyFileVersion(VersionInformation.FileVersion)]
[assembly: AssemblyInformationalVersion(VersionInformation.ProductVersion)]

版本信息Project.Resources 中的一个静态类。

所有其他程序集也具有这些引用,用于其AssemblyVersion

当我们构建(调试或发布(时,它将通过而没有任何错误。当我们发布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   PropertiesAssemblyInfo.cs  3   
Error       The name 'VersionInformation' does not exist in the current context Project.WebUI   PropertiesAssemblyInfo.cs  34  
Error       The name 'VersionInformation' does not exist in the current context Project.WebUI   PropertiesAssemblyInfo.cs  35  
Error       The name 'VersionInformation' does not exist in the current context Project.WebUI   PropertiesAssemblyInfo.cs  36  

我们的发布配置文件如下所示:

[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)

为什么会发生此错误,我们如何解决此问题?

下面是错误的输出日志:

------ Publish started: Project: Project.WebUI, Configuration: Release Any CPU ------
Connecting to C:BuildArtifacts...
Transformed Web.config using C:DevelopmentProject.WebUIWeb.Release.config into objReleaseTransformWebConfigtransformedWeb.config.
Copying all files to temporary location below for package/publish:
objReleaseAspnetCompileMergeSource.
C:WINDOWSMicrosoft.NETFrameworkv4.0.30319aspnet_compiler.exe -v  -p C:DevelopmentProject.WebUIobjReleaseAspnetCompileMergeSource -c C:DevelopmentProject.WebUIobjReleaseAspnetCompileMergeTempBuildDir 
C:Program Files (x86)Microsoft Visual Studio2017CommunityMSBuild15.0BinRoslyncsc.exe /out:objReleaseAssemblyInfoAssemblyInfo.dll /target:library PropertiesAssemblyInfo.cs
PropertiesAssemblyInfo.cs(34,28): Error CS0103: The name 'VersionInformation' does not exist in the current context
PropertiesAssemblyInfo.cs(35,32): Error CS0103: The name 'VersionInformation' does not exist in the current context
PropertiesAssemblyInfo.cs(36,41): Error CS0103: The name 'VersionInformation' does not exist in the current context
PropertiesAssemblyInfo.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?)
显然

在"发布"期间,csc.exe尝试编译我们Project.WebUIAssemblyInfo.cs,但只有这个文件。因此,它无法获得对Project.Resources的正确引用。

我的同事带来的解决方案对我们有用:

  • 从所有AssemblyInfo.cs文件中删除所有版本属性
  • Project.Resources项目中创建一个新文件,如AssemblyVersion.cs
  • 该文件的唯一内容是这样的版本属性

    [assembly: AssemblyFileVersion(VersionInformation.FileVersion)]
    
  • 创建指向其他每个项目中AssemblyVersion.cs的链接

Create CommonAssemblyInfo.cs 与 .SLN并将您喜欢共享的属性放在那里。

一次一个项目:选择"属性"组,右键单击"添加>现有项",选择"CommonAssemblyInfo".cs然后从"添加"按钮菜单中选择"添加为链接"。从AssemblyInfo.cs中删除这些属性。

最新更新