Yesterday, for the first time, I came across a scenario where I had to reference and build the project using a NuGet package exposed from a local or shared path.
I faced few issues while trying to do this. Let's walk through the process in this blog.
This setup involves following three steps:-
1. Create a NuGet package. Usually, we consume packages already created by some third-party, but here, I want to demonstrate the creation of NuGet packages also.
2. Host the NuGet package
I faced few issues while trying to do this. Let's walk through the process in this blog.
This setup involves following three steps:-
1. Create a NuGet package. Usually, we consume packages already created by some third-party, but here, I want to demonstrate the creation of NuGet packages also.
2. Host the NuGet package
3. Reference the NuGet package in the client application.
- As official NuGet feeds library
- As local feeds.
1. Create a NuGet package:-
For this blog, I created a simple Class Library with the following function in it.
Then, I created the DLL in Release mode.
Now, I opened Package Manager Console from the following path in Visual Studio.
Tools -> NuGet Package Manager -> Package Manager Console.
Check the Package Source and Default Project selected in the Package Manager Console. By default, the location for package source will be nuget.org and your current project as the default project.
You can check the packages installed for your project by using Get-Package cmdlet in the Package Manager Console. Initially, my class library did not have any packages installed on it.
Now, I executed the command to create the nuspec file and got the following error.
nuget spec HelloWorld.dll
After some searching, I found that I was missing the NuGet command-line package. So, I installed that.
install-package nuget.commandline
Then, I was able to create the nuspec file.
nuget spec HelloWorld.dll
After that, I had to edit the nuspec file with all the relevant details viz. id, version, title, author etc.
Then, I had to create the package using the following command.
nuget pack HelloWorld.nuspec
See all those warnings?! Like a true developer, I didn't even bother reading those warnings as soon as I saw that "Successfully created package" message ;)
But, we'll see how I had to redo the whole process due to this mistake.
2. Host the NuGet package:-
We can host our NuGet packages in the following two ways:-
i) As official NuGet feeds library - For this, we need to sign up on nuget.org and get an API key. After this, we just need to upload our package on this website using the API key and that's it. We can search the package up in the Client application and use it.
ii) As local feeds - We need to place the package in some path and go to the Package Manager Settings from the Package Manager Console. Here, we need to add another source path from where we can get NuGet packages as shown below.
3. Reference our new NuGet package in the client application:-
Let's create a new simple console application to consume our new package now.
Using the Package Manager Console, change the source of packages from nuget.org(default) to HelloWorld Package. It will show us our new package.
Now, we need to install this package. I tried doing that but got the following error.
This is where overlooking those warnings(like a true developer) came back to haunt me..!! ;)
Finally after some searching, I got this link which tells about the convention that we need to follow in order to build a proper package.
To check the contents of any nupkg file, you can use NuGet Package Explorer. This is something similar to ildasm for a package.
To check the contents of any nupkg file, you can use NuGet Package Explorer. This is something similar to ildasm for a package.
After going through the link, I restructured the folder where I had placed my DLL as shown below. Here, the net45 folder is used as I had built the console app targeting .NET Framework 4.5. More details on the conventions for this folder structure are provided here.
Now, follow the steps from beginning and create the package again. That's all.
After that, referencing the DLL in the new project works like a charm..!! :)
Hope this helps.
No comments:
Post a Comment