将内容和编译文件从nuget复制到.net核心项目

我正在尝试创建一个包含类和xsd文件的多目标框架nuget。类文件是从xml模式定义文件生成的,并在开发过程中使用。然后在运行时使用xsd来验证序列化的类输出。

由于nuget需要是多目标框架,因此已选择.net标准lib项目,因此将xsd导入到与生成的类文件相同的项目中。我正在使用Visual Studio 2019环境,并想使用pack选项。

我面临的问题是,每当我在nuget中指定cs和xsd文件是不同的buildaction类型时,使用nuget的项目中就不包括xsd。如果我指定cs和xsd文件都来自同一buildaction类型,则全部包含在内。

但是,如果将CS文件定义为buildaction“ None”或“ Content”,则不能在使用Nuget的项目中使用它们。仅当您指定cs文件来自buildaction类型“ Compile”时,才可以使用它们。 xsd文件不能来自“编译”类型,因为这将导致安装Nuget时出错。

我已经检查了文件是否存在于nuget中。它们也在创建的.nuspec文件中指定,并放在nuget中。在将nuget安装到项目中之后,它们还将出现在obj \ projects.assets.json文件中。我看不到任何错误或警告。

我在某处读到,因为nuget是通过.net核心项目的参考打包的,所以内容不会始终显示设计时,但必须在运行时。有趣的是,xsd始终丢失,而从不包含cs文件。而且在运行时也看不到xml文件。

我已经更改了csproj文件,使其支持多目标框架,并且我添加了不将创建的dll添加到nuget包中的选项。

 <PropertyGroup>
    <TargetFrameworks>netstandard2.0;netcoreapp2.1;net45</TargetFrameworks>
    <Version>1.0.30</Version>
  </PropertyGroup>
  <PropertyGroup>
    <IncludeBuildOutput>false</IncludeBuildOutput>
  </PropertyGroup>

要将内容复制到nuget中,我添加了以下几行:

<ItemGroup>
    <Content Include="XSD/**/*.xsd" copyToOutput="true">
      <IncludeInPackage>true</IncludeInPackage>
      <CopyToOutput>true</CopyToOutput>
      <buildaction>Content</buildaction>
      <copyToOutput>true</copyToOutput>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <Content Include="Messages.Voorbeeld/**/*.cs" copyToOutput="true">
      <IncludeInPackage>true</IncludeInPackage>
      <CopyToOutput>true</CopyToOutput>
      <buildaction>Compile</buildaction>
      <copyToOutput>true</copyToOutput>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

nuget包中的nuspec文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>XsdInclude</id>
    <version>1.0.30</version>
    <authors>XsdInclude</authors>
    <owners>XsdInclude</owners>
    <requireLicenseacceptance>false</requireLicenseacceptance>
    <description>Package Description</description>
    <dependencies>
      <group targetFramework=".NETFramework4.5" />
      <group targetFramework=".NETCoreApp2.1" />
      <group targetFramework=".NETStandard2.0" />
    </dependencies>
    <contentFiles>
      <files include="any/net45/XSD/V202001/Voorbeeld/Common.xsd" buildaction="Content" />
      <files include="any/netcoreapp2.1/XSD/V202001/Voorbeeld/Common.xsd" buildaction="Content" />
      <files include="any/netstandard2.0/XSD/V202001/Voorbeeld/Common.xsd" buildaction="Content" />
      <files include="any/net45/XSD/V202001/Voorbeeld/Specific.xsd" buildaction="Content" />
      <files include="any/netcoreapp2.1/XSD/V202001/Voorbeeld/Specific.xsd" buildaction="Content" />
      <files include="any/netstandard2.0/XSD/V202001/Voorbeeld/Specific.xsd" buildaction="Content" />
      <files include="cs/net45/Messages.Voorbeeld/V202001/Common.cs" buildaction="Compile" />
      <files include="cs/netcoreapp2.1/Messages.Voorbeeld/V202001/Common.cs" buildaction="Compile" />
      <files include="cs/netstandard2.0/Messages.Voorbeeld/V202001/Common.cs" buildaction="Compile" />
      <files include="cs/net45/Messages.Voorbeeld/V202001/Voorbeeld.cs" buildaction="Compile" />
      <files include="cs/netcoreapp2.1/Messages.Voorbeeld/V202001/Voorbeeld.cs" buildaction="Compile" />
      <files include="cs/netstandard2.0/Messages.Voorbeeld/V202001/Voorbeeld.cs" buildaction="Compile" />
    </contentFiles>
  </metadata>
</package>

和其中包含nuget包的project.assets.json文件如下所示

{
  "version": 3,"targets": {
    ".NETCoreApp,Version=v3.1": {
      "XsdInclude/1.0.30": {
        "type": "package","contentFiles": {
          "contentFiles/any/netcoreapp2.1/XSD/V202001/Voorbeeld/Common.xsd": {
            "buildaction": "Content","codeLanguage": "any","copyToOutput": false
          },"contentFiles/any/netcoreapp2.1/XSD/V202001/Voorbeeld/Specific.xsd": {
            "buildaction": "Content","contentFiles/cs/netcoreapp2.1/Messages.Voorbeeld/V202001/Common.cs": {
            "buildaction": "Compile","codeLanguage": "cs","contentFiles/cs/netcoreapp2.1/Messages.Voorbeeld/V202001/Voorbeeld.cs": {
            "buildaction": "Compile","copyToOutput": false
          }
        }
      }
    }
  },"libraries": {
    "XsdInclude/1.0.30": {
      "sha512": "4MxqovEuuUlTmSxbgxcmsMnCF/6N1He6nPJ0FvvJ9l7588xiGKJgU4HtDktI66cKThpn+WsX5tzpm7K8HoRZ7A==","type": "package","path": "xsdinclude/1.0.30","files": [
        ".nupkg.metadata","content/Messages.Voorbeeld/V202001/Common.cs","content/Messages.Voorbeeld/V202001/Voorbeeld.cs","content/XSD/V202001/Voorbeeld/Common.xsd","content/XSD/V202001/Voorbeeld/Specific.xsd","contentFiles/any/net45/XSD/V202001/Voorbeeld/Common.xsd","contentFiles/any/net45/XSD/V202001/Voorbeeld/Specific.xsd","contentFiles/any/netcoreapp2.1/XSD/V202001/Voorbeeld/Common.xsd","contentFiles/any/netcoreapp2.1/XSD/V202001/Voorbeeld/Specific.xsd","contentFiles/any/netstandard2.0/XSD/V202001/Voorbeeld/Common.xsd","contentFiles/any/netstandard2.0/XSD/V202001/Voorbeeld/Specific.xsd","contentFiles/cs/net45/Messages.Voorbeeld/V202001/Common.cs","contentFiles/cs/net45/Messages.Voorbeeld/V202001/Voorbeeld.cs","contentFiles/cs/netcoreapp2.1/Messages.Voorbeeld/V202001/Common.cs","contentFiles/cs/netcoreapp2.1/Messages.Voorbeeld/V202001/Voorbeeld.cs","contentFiles/cs/netstandard2.0/Messages.Voorbeeld/V202001/Common.cs","contentFiles/cs/netstandard2.0/Messages.Voorbeeld/V202001/Voorbeeld.cs","xsdinclude.1.0.30.nupkg.sha512","xsdinclude.nuspec"
      ]
    }
  },"projectFileDependencyGroups": {
    ".NETCoreApp,Version=v3.1": [
      "XsdInclude >= 1.0.30"
    ]
  },"packageFolders": {
    "C:\\Users\\xx\\.nuget\\packages\\": {},"C:\\microsoft\\Xamarin\\NuGet\\": {}
  },"project": {
    "version": "1.0.0","restore": {
      "projectUniqueName": "D:\\xx\\Projects\\Nugets\\CoreTest\\CoreTest.csproj","projectName": "CoreTest","projectPath": "D:\\xx\\Projects\\Nugets\\CoreTest\\CoreTest.csproj","packagesPath": "C:\\Users\\xx\\.nuget\\packages\\","outputPath": "D:\\xx\\Projects\\Nugets\\CoreTest\\obj\\","projectStyle": "PackageReference","fallbackFolders": [
        "C:\\microsoft\\Xamarin\\NuGet\\"
      ],"configFilePaths": [
        "C:\\Users\\xx\\AppData\\Roaming\\NuGet\\NuGet.Config","C:\\Program Files (x86)\\NuGet\\Config\\microsoft.VisualStudio.Offline.config","C:\\Program Files (x86)\\NuGet\\Config\\Xamarin.Offline.config"
      ],"originalTargetFrameworks": [
        "netcoreapp3.1"
      ],"sources": {
        "C:\\Program Files (x86)\\microsoft SDKs\\NuGetPackages\\": {},"D:\\xx\\Projects\\NugetServer": {},"https://api.nuget.org/v3/index.json": {}
      },"frameworks": {
        "netcoreapp3.1": {
          "projectReferences": {}
        }
      },"warningProperties": {
        "warnAsError": [
          "NU1605"
        ]
      }
    },"frameworks": {
      "netcoreapp3.1": {
        "dependencies": {
          "XsdInclude": {
            "target": "Package","version": "[1.0.30,)"
          }
        },"imports": [
          "net461","net462","net47","net471","net472","net48"
        ],"assetTargetFallback": true,"warn": true,"frameworkReferences": {
          "microsoft.NETCore.App": {
            "privateAssets": "all"
          }
        },"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\3.1.101\\RuntimeIdentifierGraph.json"
      }
    }
  }
}

它一定很小,但我找不到。谁可以帮助我?

iCMS 回答:将内容和编译文件从nuget复制到.net核心项目

通过将cs文件的BuildAction属性从“ Compile”更改为“ C#Compiler”,解决了该问题。如果正确,我不确定。

这样做,生成的nuspec文件不再具有用于CS文件的buildAction,但仍使用BuildAction =“ Compile”创建json文件。

现在,带有文件的目录都显示在Visual Studio中,并且CS文件已编译且可用。

本文链接:https://www.f2er.com/1916104.html

大家都在问