Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and use ASP.NET vNext class library NuGet package?

I would like to create a NuGet package of my ASP.NET vNext class library. How can I do it, step by step? I understand there is kpm build, but I couldn't find a guide regarding where to download kpm, etc.

Also, after getting a NuGet package (or DLL) of it, how can I add it from local machine to my vNext project?

like image 510
Adam Szabo Avatar asked Oct 10 '14 21:10

Adam Szabo


People also ask

How use NuGet package in asp net?

Open your Visual Studio application. On the Tools menu, select Options. Expand the NuGet Package Manager and select Package Sources. Click the Add button (green plus), and enter the 'Package Name' and 'Package Source URL' of the Syncfusion ASP.NET Web Forms NuGet packages.

How do I publish a class library project?

Right-click on project menu and select Publish. Select the option to publish to a Folder.


2 Answers

Kpm is the package manager for the new KRuntime. Instructions on how to install the KRuntime command line utilities on your developer machine can be found on the aspnet Home repo. Once you have kvm and a version of the KRuntime set up you will have kpm available as well.

Now you can run kpm build on your class libraries project location. Output should be something like this:

kpm build src\ClassLibrary1\
ClassLibrary1 -> C:\Users\username\Documents\Visual Studio 14\Projects\WebApplication1\src\ClassLibrary1\bin\Debug\ClassLibrary1.1.0.0.nupkg
ClassLibrary1 -> C:\Users\username\Documents\Visual Studio 14\Projects\WebApplication1\src\ClassLibrary1\bin\Debug\ClassLibrary1.1.0.0.symbols.nupkg

Build succeeded.
    0 Warnings(s)
    0 Error(s)

Time elapsed 00:00:01.7556414

The easiest way to add a reference to a class project is to do it in your project.json, assuming you have it in the same solution. Here is an example project.json from a web application, which references a class library called ClassLibrary1.

{
    "webroot" : "wwwroot",
    "exclude": "wwwroot/**/*.*",
    "dependencies": {
        "Microsoft.AspNet.Server.IIS": "1.0.0-alpha4",
        "ClassLibrary1": ""
    },
    "frameworks" : {
        "aspnet50" : { },
        "aspnetcore50" : { }
    }
}

If you want to set up a NuGet feed you can read the official NuGet documentation to see how that is done. Copy the outputs of kpm build into your NuGet feed.

Note: VS14 CTP4 only works with alpha4 of the KRuntime. If you want to use VS14 for vNext without errors popping up you need to downgrade your KRuntime to version 1.0.0-alpha4.

like image 97
AndersNS Avatar answered Sep 20 '22 05:09

AndersNS


Creating NuGet package from class library

If you use Visual Studio 2015 RC and later: go to your class library project properties, open Build tab and check Produce outputs on build option:

enter image description here

NuGet package will be created in {SolutionDir}\artifacts\bin\{ProjectName}\{Configuration} directory on each project build.

If you use command line:

  1. Make sure you installed DNVM and DNX (see ASP.NET Home repo for instructions).
  2. Run dnu pack in the project directory. NuGet package will be created in {ProjectDir}\bin\{Configuration} directory by default.

Using packaged class library

To use the class library in another project from the same solution, add it as a usual project reference in Visual Studio or to dependencies property in project.json:

"dependencies": {
  "ClassLibrary1": ""
}

To use the library in other solutions, publish NuGet package to nuget.org or any other NuGet feed and add it to your project using Visual Studio (References ~> Manage NuGet Packages...) or to dependencies property in project.json.

like image 26
whyleee Avatar answered Sep 19 '22 05:09

whyleee