How to create nuget package (dll) for standard framework and .Net Core?

1.5k views Asked by At

I want to create data small data access library for Sql Server that wraps standard Sql Client classes, and publish it to NuGet. I want to use this NuGet package both in standard and .Net core apps.

I created class library project with some data access code (it uses System, System.Core, and System.Data) and published it to nugget. I have added System, System.Core, and System.Data as NuGet framework dependencies.

UPDATE - described problems both in RC1 and RC2

In RC1 version it works with 4.6 framework, but I had to remove DNX 5 from package.json.

In RC2 version it works with ASPNET Core (.Net Framework) projects, but when I create ASPNET Core (.Net Core), compilation fails:

Error NU1002 The dependency does not support framework .NETCoreApp,Version=v1.0.

Is there any way to create nugget package that works in both versions?

2

There are 2 answers

1
Eric Davis On

Since .NET Core 1.0 was just released this week, I'll answer the question in the context of the latest version of the framework.

If you are attempting to target both ASP.NET Core 1.0 and .NET 4.5 you have to define both frameworks separately, including all dependencies each framework build would require.

Here is an example project.json file for a class library project that targets .NET Core Standard 1.5, ASP.NET Core 1.0, .NET 4.5, and .NET 4.0 and has a dependency on the System.Linq.Expressions namespace:

{
    "version": "1.0.0-*",

    "frameworks": {
        "netstandard1.5": {
            "imports": "dnxcore50",
            "dependencies": {
                "NETStandard.Library": "1.6.0",
                "System.Linq.Expressions": "4.1.0"
            }
        },
        "netcoreapp1.0": {
            "imports": "dnxcore50",
            "dependencies": {
                "Microsoft.NETCore.App": {
                    "type": "platform",
                    "version": "1.0.0"  
                },
                "System.Linq.Expressions": "4.1.0"
            }
        },
        "net45": {
            "frameworkAssemblies": {
                "System.Linq.Expressions": ""
            }
        },
        "net40": {}
    }
}
0
Fab On

With Visual Studio 2017, you can create a .NET Standard project library. You can select the version in the properties of the project.

Such project looks like this :

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
    <AssemblyName>MyAssembly</AssemblyName>
    <PackageId>MyPackage</PackageId>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
    <Copyright>Copyright</Copyright>
    <Description>Simple Library</Description>
    <Authors>Me</Authors>
    <AssemblyVersion>1.1.0</AssemblyVersion>
    <FileVersion>1.1.0</FileVersion>
    <Version>1.1.0</Version>
    <Company>MyCompany</Company>
  </PropertyGroup>
  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>
</Project>

In IDE, you have the pack command on the right click menu or you can type this :

msbuild /t:pack myproject.csproj

Only one package.... to rule them all and... :)