You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨.NET Framework 4.8与.NET 6的NuGet包配置问题求助

.NET 4.8与.NET 6兼容的NuGet包配置方案(含JSON Schema与C#类)

一、纯SDK风格csproj配置(推荐)

这个配置确保C#类编译后分框架存放到lib目录(设计时可直接引用),JSON Schema文件同时兼容新旧项目的内容引用规则,构建时自动复制到输出目录。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net48;net6.0</TargetFrameworks>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <PackageId>Your.Shared.Schema.Package</PackageId>
    <Version>1.0.0</Version>
    <Authors>YourName</Authors>
    <Description>Shared JSON Schema and C# classes for .NET Framework 4.8 and .NET 6</Description>
    <IncludeContentInPack>true</IncludeContentInPack>
    <ContentTargetFolders>contentFiles</ContentTargetFolders>
  </PropertyGroup>

  <!-- 处理C#类:编译后分框架放入lib目录 -->
  <ItemGroup>
    <Compile Include="GeneratedClasses\**\*.cs" />
  </ItemGroup>

  <!-- 处理JSON Schema:全局contentFiles目录兼容所有框架,同时兼容旧.NET Framework的content目录 -->
  <ItemGroup>
    <Content Include="Schemas\**\*.json">
      <Pack>true</Pack>
      <PackagePath>contentFiles\any\any\Schemas\</PackagePath>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>

  <!-- 为.NET Framework 4.8额外添加content目录,兼容非PackageReference的旧项目 -->
  <ItemGroup Condition="'$(TargetFramework)' == 'net48'">
    <Content Include="Schemas\**\*.json">
      <PackagePath>content\Schemas\</PackagePath>
    </Content>
  </ItemGroup>
</Project>

二、Nuspec配置方案

如果需要更精细的包结构控制,可使用nuspec配置,明确指定程序集和内容文件的存放路径:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>Your.Shared.Schema.Package</id>
    <version>1.0.0</version>
    <authors>YourName</authors>
    <description>Shared JSON Schema and C# classes for .NET Framework 4.8 and .NET 6</description>
    <contentFiles>
      <!-- 声明JSON文件的构建行为 -->
      <files include="any/any/Schemas/**/*.json" buildAction="Content" copyToOutput="true" />
    </contentFiles>
  </metadata>
  <files>
    <!-- 分框架放置编译后的C#程序集 -->
    <file src="bin\Release\net48\Your.Shared.Schema.Package.dll" target="lib/net48" />
    <file src="bin\Release\net48\Your.Shared.Schema.Package.pdb" target="lib/net48" />
    <file src="bin\Release\net6.0\Your.Shared.Schema.Package.dll" target="lib/net6.0" />
    <file src="bin\Release\net6.0\Your.Shared.Schema.Package.pdb" target="lib/net6.0" />

    <!-- JSON文件同时放入contentFiles(SDK项目)和content(旧.NET项目)目录 -->
    <file src="Schemas\**\*.json" target="contentFiles\any\any\Schemas" />
    <file src="Schemas\**\*.json" target="content\Schemas" />
  </files>
</package>

原有配置参考

原有SDK风格csproj(多目标失效版本)

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net48;net6.0</TargetFrameworks>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <PackageId>Your.Shared.Schema.Package</PackageId>
    <Version>1.0.0</Version>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="GeneratedClasses\**\*.cs" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="Schemas\**\*.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

原有Nuspec(仅contentFiles\any\any生效版本)

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>Your.Shared.Schema.Package</id>
    <version>1.0.0</version>
    <authors>YourName</authors>
  </metadata>
  <files>
    <!-- 原分框架配置失效,仅全局路径生效 -->
    <!-- <file src="Schemas\**\*.json" target="contentFiles\net48\net48\Schemas" /> -->
    <!-- <file src="Schemas\**\*.json" target="contentFiles\net6.0\net6.0\Schemas" /> -->
    <file src="Schemas\**\*.json" target="contentFiles\any\any\Schemas" />
    <file src="bin\Release\net48\*.dll" target="lib/net48" />
    <file src="bin\Release\net6.0\*.dll" target="lib/net6.0" />
  </files>
</package>

项目与包结构说明

本地项目结构

Your.Shared.Schema.Package/
├── GeneratedClasses/
│   ├── UserSchema.cs
│   └── OrderSchema.cs
├── Schemas/
│   ├── user.schema.json
│   └── order.schema.json
├── Your.Shared.Schema.Package.csproj
└── (可选) Your.Shared.Schema.Package.nuspec

正确配置后的NuGet包结构

Your.Shared.Schema.Package.1.0.0.nupkg/
├── lib/
│   ├── net48/
│   │   ├── Your.Shared.Schema.Package.dll
│   │   └── Your.Shared.Schema.Package.pdb
│   └── net6.0/
│       ├── Your.Shared.Schema.Package.dll
│       └── Your.Shared.Schema.Package.pdb
├── content/
│   └── Schemas/
│       ├── user.schema.json
│       └── order.schema.json
└── contentFiles/
    └── any/
        └── any/
            └── Schemas/
                ├── user.schema.json
                └── order.schema.json

内容的提问来源于stack exchange,提问作者martijn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 10:15:33