Search code examples
c#.net-corenugetpack

Copy Content and Compile files into .net core project from nuget


I'm trying to create a multi target framework nuget that holds class and xsd files. The class files are generated from the xml schema definition files and are used during development. The xsd's then are used while running to validate the serialized class output.

As the nuget needs to be multi target framework a .net standard lib project has been choosen, the xsd's are imported into the project same as the generated class files. I'm using the visual studio 2019 environment and want to use the pack option.

The problem that I'm facing is that whenever I specify that in the nuget the cs and xsd files are different buildAction types, the xsd are not included in the project that uses the nuget. If I specify that both the cs and xsd files are the from the same buildaction type, all are included.

But if you define the cs files as buildAction "None" or "Content" then they cannot be used in the project that uses the Nuget. Only when you specify that the cs files are from the buildAction type "Compile" they can be used. The xsd files cannot be from the type "Compile" as that will result into errors installing the Nuget.

I've checked if the files are present in the nuget, they are. They are also specified in the .nuspec files that is created and put inside the nuget. And after installation of the nuget into the project they also appear in the obj\projects.assets.json file. I do not see any errors or warnings appear.

I've read somewhere that because the nuget is packaged by reference for .net core project the content will not allways show designtime but it must be there runtime. The funny part is the xsd are allways missing and never the cs files. but also during runtime I do not see the xml files appear.

I've altered my csproj file so that it supports multi target framework and I added the option not to add the created dll into the nuget package.

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

to copy the content into the nuget I've added these lines:

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

the nuspec file inside the nuget package looks like this:

<?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>

and the project.assets.json file where the nuget package is included into looks like this

{
  "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",
            "codeLanguage": "any",
            "copyToOutput": false
          },
          "contentFiles/cs/netcoreapp2.1/Messages.Voorbeeld/V202001/Common.cs": {
            "buildAction": "Compile",
            "codeLanguage": "cs",
            "copyToOutput": false
          },
          "contentFiles/cs/netcoreapp2.1/Messages.Voorbeeld/V202001/Voorbeeld.cs": {
            "buildAction": "Compile",
            "codeLanguage": "cs",
            "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"
      }
    }
  }
}

It must be something small but I cant find it. Who can help me out?


Solution

  • By changing the BuildAction property for the cs files from 'Compile' to 'C# Compiler' solved the problem. If it is correct I'm not sure.

    By doing so the generated nuspec file no longer has a buildAction for the cs files but the json file still is created with BuildAction="Compile".

    Now both the directories with the files are shown in visual studio and the cs files are compiled and useable.