在 VS2017 中针对多个框架时发布失败

Publish Fails when targeting multiple frameworks in VS2017

在 Visual Studio 2017 项目 (netcoreapp1.1;net462) 中针对多个框架时

每次我尝试发布时都会失败并出现错误:

"The 'Publish' target is not supported without specifying a target framework. The current project targets multiple frameworks, please specify the framework for the published application"

我已经准备好 属性 组条件,但我还需要对 'specify the framework for the published application' 做些什么。我错过了什么吗?

进一步 - 项目编译正常。另外,值得注意的是,它是一个在 VS 2015 中创建并转换为 VS 2017 项目的项目。

这是.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFrameworks>netcoreapp1.1;net462</TargetFrameworks>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <AssemblyName>Project.WebApp</AssemblyName>
    <OutputType>Exe</OutputType>
    <PackageId>Project.WebApp</PackageId>
    <RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">1.1.1</RuntimeFrameworkVersion>
  </PropertyGroup>
  <ItemGroup>
    <None Include="App.config" />
    <None Update="wwwroot\**\*">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Data\Proj.Data.csproj" />
    <ProjectReference Include="..\Proj.Data\Proj.Data.csproj" />
    <ProjectReference Include="..\Proj.Web.Client\Proj.Web.Client.csproj" />
    <ProjectReference Include="..\Proj.Web.Models\Proj.Web.Models.csproj" />
    <ProjectReference Include="..\Proj.Services.Client\Proj.Services.Client.csproj" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="AngleSharp" Version="0.9.9" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
  <Target Name="PrepublishScript" BeforeTargets="PrepareForPublish" Condition=" '$(IsCrossTargetingBuild)' != 'true' ">
    <Exec Command="bower install" />
    <Exec Command="dotnet bundle" />
  </Target>
  <ItemGroup>
    <DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.2.301" />
  </ItemGroup>
</Project>

这里还有发布配置文件,它似乎指定了发布框架:

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <PublishFramework>netcoreapp1.1</PublishFramework>
    <ProjectGuid>3794908a-5af3-4dba-bb6a-8d846b773ff7</ProjectGuid>
    <publishUrl>\app\Applications\App\Web Site\</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
  </PropertyGroup>
</Project>

编辑:对我有用的解决方案 我尝试了仅使用命令行并指定要发布的版本的@Stan88 解决方案。这对我不起作用,因为我的 PrepublishScript 由于某种原因失败了。

最后对我有用的基本上是编辑我的 .csproj 文件以仅针对 netcoreapp1.1 框架。然而,最后我意识到我的 PrepublishScript 有问题并最终删除了 .csproj 文件中对它的引用 - 所以最后我认为如果不是因为预发布疯狂,Stan88 的命令行解决方案会起作用,因此,标记为正确.

这是我的 .csproj 最终的样子。

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFrameworks>netcoreapp1.1</TargetFrameworks>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <AssemblyName>Project.WebApp</AssemblyName>
    <OutputType>Exe</OutputType>
    <PackageId>Project.WebApp</PackageId>
  </PropertyGroup>
  <ItemGroup>
    <None Include="App.config" />
    <None Update="wwwroot\**\*">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Data\Proj.Data.csproj" />
    <ProjectReference Include="..\Proj.Data\Proj.Data.csproj" />
    <ProjectReference Include="..\Proj.Web.Client\Proj.Web.Client.csproj" />
    <ProjectReference Include="..\Proj.Web.Models\Proj.Web.Models.csproj" />
    <ProjectReference Include="..\Proj.Services.Client\Proj.Services.Client.csproj" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="AngleSharp" Version="0.9.9" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.2.301" />
  </ItemGroup>
</Project>

您一次只能发布一个框架,因此您必须在执行 dotnet publish 命令时指定要发布的目标框架。

dotnet publish -f=netcoreapp1.1

dotnet publish -f=net462