Consuming NuGet packages, that’s what everyone does. Open source projects create NuGet packages and post them on NuGet.org. Meanwhile, all of us are still working with shared projects and fighting relative paths, versioning and so on. In this talk, we’ll use Visual Studio, NuGet and TeamCity to work with NuGet the way you should. Project references must die! Add Package Reference and good continuous integration is everything you will ever need.
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
Using NuGet the way you should - TechDays NL 2014
1.
2. Laat ons weten wat u vindt van deze sessie! Vul de evaluatie
in via www.techdaysapp.nl en maak kans op een van de 20
prijzen*. Prijswinnaars worden bekend gemaakt via Twitter
(#TechDaysNL). Gebruik hiervoor de code op uw badge.
Let us know how you feel about this session! Give your
feedback via www.techdaysapp.nl and possibly win one of
the 20 prizes*. Winners will be announced via Twitter
(#TechDaysNL). Use your personal code on your badge.
* Over de uitslag kan niet worden gecorrespondeerd, prijzen zijn voorbeelden – All results are final, prizes are
examples
4. Who am I?
• Maarten Balliauw
• Antwerp, Belgium
• Technical Evangelist, JetBrains
• Founder, MyGet
• AZUG
• Focus on web
• ASP.NET MVC, Azure, SignalR, ...
• MVP Azure & ASPInsider
• Big passion: Azure
• http://blog.maartenballiauw.be
• @maartenballiauw
Shameless self promotion: Pro NuGet -
http://amzn.to/pronuget2
5. Agenda
• NuGet
• File | New Project…
• The state of NuGet
• Every project is a package
• Creating packages
• Distributing packages
• Consuming packages
7. A brief NuGet introduction...
• Package management system for .NET
• Visual Studio extension, command line, console
• Simplifies incorporating 3rd party libraries
• Developer focused
• Free, open source
• Use packages from the official feed
• Publish your own packages
• Create & use your own feed
10. That was trouble!
•Dependencies on NuGet packages
•Dependencies on projects
•Dependencies on file system layout
•Breaking changes in dependencies
Dependency hell
11. “Dependencies, also in
real life, are trouble.
Have you ever had to
depend on a plumber?”
- Maarten Balliauw
12. Dependencies before NuGet
•Projects in solution
•Reference in-house projects:
• Either the assemblies (typically unversioned / all 1.0.0.0)
• Either the actual project / linked files (unversioned by design)
•Reference third party assemblies (e.g.
JSON.NET)
13. Dependencies with NuGet
•Projects in solution
•Reference in-house projects:
• Either the assemblies (typically unversioned / all 1.0.0.0)
• Either the actual project / linked files (unversioned by design)
•Reference third party packages (e.g. JSON.NET)
• Semantic Versioning to announce breaking changes
(happy times as long as package authors respect that)
15. The state of NuGet
•We are consumers!
• Even quite some overconsumption I dare say…
•We sort of know about Semantic Versioning
• If the mayor version changes, we’re doomed
•Are we producing?
• Some are, e.g. OSS and some companies
•Did we solve our dependency issues?
Where are we, 3 years in?
16. .NET is late to the party!
•Others have been doing package management for
decades
• Perl (CPAN.org)
• Linux (RPM/YUM/APT-GET/...)
• PHP (PEAR, Composer)
• Node (npm)
• Ruby (Gems)
•We can learn a great deal from these!
18. What are the others doing?
•Name and version of the project
•Dependencies (and version range) of the project
•Every project is a package!
• It can have dependencies
• It can be depended on
What was in that package.json?
20. Supporting componentization
•Every project is a package
• Clearly identifyable
• Proper Semantic Versioning
•Every project becomes discoverable
• Nice description, release notes, ...
• Add it to a private feed so developers can find it
•Dependencies (can) stay out of source control
•Dependencies are versioned
21. But… NuGet only has packages.config!
•Nodejs: packages.json
•Composer (PHP): composer.json
•NuGet:
• packages.config: dependencies
• <projectname>.nuspec: package description
• NuGet.config: other settings
• It’s 2-3 files in our world, but it is all there.
•Let’s become producers!
24. All we need is one file
•XML file which can be generated
• nuget.exe spec
• Install-Package NuSpec && Install-Nuspec
•Run nuget.exe pack to create package
<projectname>.nuspec
26. Think about versioning!
• www.semver.org
• SemVer states intentions
• It’s the only way to
avoid dependency
versioning hell.
But not bullet-proof.
Enforce Explicit Semantic Versioning
Major Breaking changes
Minor Backwards compatible changes
e.g. API additions
Patch Bugfixes not affecting the API
<dependency id="ExamplePackage" version="1.3.2" />
1.0 = 1.0 ≤ x
(,1.0] = x ≤ 1.0
(,1.0) = x < 1.0
[1.0] = x == 1.0
(1.0) = invalid
(1.0,) = 1.0 < x
(1.0,2.0) = 1.0 < x < 2.0
[1.0,2.0] = 1.0 ≤ x ≤ 2.0
empty = latest version
28. Which medium?
• In source control (please don’t)
• Local / network folder (slow > ~50 packages)
• NuGet.Server (slow > ~50 packages)
• NuGet Gallery (clone of www.nuget.org)
• MyGet (www.myget.org, SaaS)
• ProGet (www.inedo.com/proget)
• TeamCity NuGet artifacts
(www.jetbrains.com/teamcity)
We will need a “package source” or feed
29. How do they get there?
•Manual upload
•As part of our continuous integration process
• All benefits of CI (e.g. testing)
• Plus reporting on which packages we are using
• Plus publishing packages for consumption
31. What did we just see?
•Creating packages using .nuspec
•NuGet configuration inheritance
•Continuous Integration on TeamCity
• How to run package restore
• How to build a package
• How to see packages that are being used
•How to consume a package
32. NuGet configuration inheritance
•Make settings common to projects!
• NuGet walks up the directory tree to find NuGet.config files
•Or even push them to developer machines
• %AppData%NuGetNuGet.config
• %ProgramData%NuGetConfig*.config
• ...
•See http://bit.ly/nuget-config-inheritance
•Configure feeds, proxy, credentials, ...
33. More recommendations…
•Don’t just update blindly
• Not everyone respects SemVer…
• It would take us back to the original problem
•Don’t autoupdate during builds
• Continuous Integration: Same Input = Same Output
• Be explicit about versioning
34. Deploying from NuGet packages
•Using a simple, handcrafted script
•Using a tool like OctopusDeploy.com
• Which is even cooler combined with Chocolatey.org
•TeamCity supports various ways of deploying
(e.g. WebDeploy)
36. Considerations
•TeamCity internal NuGet feed
• Only build artifacts
• No way to add external packages that are being used
• May not be a problem for your setup
• But maybe it is: a feed is much like source control for dependencies
• Add dependencies that are being used, NuGet.org, component
vendors, ...
• If it is, create your own feed, e.g. MyGet.org
37. Create a package source / feed
•That’s a cool library! And that one too!
• Avoid overconsumption by limiting packages
• May not block developers but at least the build will fail
•Your own feed will only contain packages you
know
• Keep versions under control
• Keep licensing under control
• Easy way to audit external packages in use
For your team or the entire organization
40. Conclusions
• NuGet
• File | New Project…
• The state of NuGet
• Every project is a package
• Creating packages
• Distributing packages
• Consuming packages
What have we learned?
41. “Stop just consuming
NuGet packages. Re-
think your development
by also creating them. It’s
a logical evolution.”
- Maarten Balliauw
43. Laat ons weten wat u vindt van deze sessie! Vul de evaluatie
in via www.techdaysapp.nl en maak kans op een van de 20
prijzen*. Prijswinnaars worden bekend gemaakt via Twitter
(#TechDaysNL). Gebruik hiervoor de code op uw badge.
Let us know how you feel about this session! Give your
feedback via www.techdaysapp.nl and possibly win one of
the 20 prizes*. Winners will be announced via Twitter
(#TechDaysNL). Use your personal code on your badge.
* Over de uitslag kan niet worden gecorrespondeerd, prijzen zijn voorbeelden – All results are final, prizes are
examples
Hinweis der Redaktion
Maarten
Open IniTech.TpsReports solutionExplain demo application we will be buildingExplain we will skip the actual File | New Project... Because of demo purposes, but we will cover the steps right after thatGo through HomeController and the related views and model classesLet’s add some NuGet Goodness!Bootstrap DatepickerELMAH for diagnosticsRun updates! We are get-latest addicts!Next, explain that for date formatting, we need one of our own projects. Which is located somewhere on my disk in another github repository from the framework code team enterprise architects.Open TpsRepoortCoversheet.cshtml and add referenceThat is awesome! Build, run and demonstrate the application.Switch hats: become the architect and open IniTech.Framework solutionA good architect refactors! Let’s switch namespaces. And that formatter,we need a FormatterFactory for that!Close Visual Studio. We also want to move this project in the source repository, because it is not at the best locationSwitch hats again and try compiling IniTech.TpsReports solution...What has happened! RELEASE IS TODAY! HELP! And the architect guy is in a meeting or presenting or something. PANIC!
Open IniTech.FrameworksolutionRun a nuget spec and show the file in action (update it and so on)Run nuget pack to package itOpen it in NuGet Package Explorer (explain the tool!) and see hwt is in thereLevel up: add a dependency on Newtonsoft.Json and run nuget pack againOpen in NPE again and see the dependency magically appeared!If you do not want this, add developmentDependency=“true”Open the IniTech.TpsReports solutionDrop the project reference and add a package source,install the framework that wayExplain how it all works and where packages can come from, folder (and NuGet.Server) are a bad idea! (BAD BAD BAD!)Now that we are here... Create a spec, run packaging on the .csprojExplore the package: dependencies are there, contents are in there too
Explain TeamCity (CI, project, configuration, build steps)Let’s make sure our IniTech.Framework is packagedCreate project from URL - https://github.com/maartenba-demo/initech-nuget.gitAutodetected build steps: none in this case?Add build step: package restoreSolution file: src/framework/IniTech.Framework/IniTech.Framework.slnRestore mode: restoreExplain other options and their evilness (autoupdate? noooo!)Mention because of the private feed we are using (TeamCIty itself), we want to add authentication there as wellAdd build step: build solutionSolution file: src/framework/IniTech.Framework/IniTech.Framework.slnAdd build step: packProject file: src/framework/IniTech.Framework/IniTech.Framework/IniTech.Framework.csprojVersion: 1.0.%build.number%Output: “artifacts” + tick publish to artifacts to make the package visible on the TeamCity NuGet feedRun a build, see package has been created. Run a NuGet.exe listnuget list -Source http://teamcity.balliauw.net/httpAuth/app/nuget/v1/FeedService.svcSources: %teamcity.nuget.feed.auth.server% and https://nuget.org/api/v2Switch to the existing build configuration for the IniTech.TpsReports solutionExplain same setup: package restore, build, packEdit the package restore step and mention for this configuration we are using the TeamCity server itself as a package source, as well as NuGet.orgRun a build, see the results tabs and get an inventory of all packages consumedOpen IniTech.Frameworksolution, make a change, commit, buildOpen the IniTech.TpsReports solution, update packages, keep workingExplain that when the framework would have a breaking change, we have to update the version number so developers will see there are breaking changes and they will have work to do when they update for the update.How do developers know about the feeds to use?Open the NuGet.config file, mention inheritance, mention developers will automatically have that feed availablePackage restore: close the solution, delete the packages folder, open and buildExplain no need to use the Enable package restore menu anymore (in fact, it will hurt you)If package restore fails (which happens a lot when fiddling with settings), make sure the (Aggregate Source) is selected in the NuGet.config file so ALL feeds are checked, not just one
Similar to consuming packages in Visual Studio, only command line this timeDo a nuget list -Source http://teamcity.balliauw.net/httpAuth/app/nuget/v1/FeedService.svcExplain we can get all packages, install them, ...We know this is a web application, so let us create a deploy.batOpen the existing oneExplain the steps in thereExplain we are installing without version number in paths so we can more easily script somethingFeel free to add database deployment in a similar way, calling into SQL Server command lineFeel free to add DevOps stuff: why not create a new Windows Azure VM using th CLI tools they have and then FTP the NuGet contents up there?Mention this is quite archaic but it works. You have to know the project you will be deploying and that is it.Mention we could also run this script from TeamCity, in fact we have a demo on thatShow the configuration thereShow the snapshot dependencyShow promoting buildMention OctopusDeploy.com again, show it on screen quickly, explain it is a good companion to TeamCity:Build on one, convention-based deployments with the other
Explain setup: instead of using the TeamCity internal feed we want to use another oneThe reason for that is: we don’t want to have just our build artifacts on there, but we want to make it the only feed of thruth (much like a VCS does for sources, this feed will do so for dependencies)Quickly create a feed on MyGet, add dependencies from the IniTech.TpsReports solution by uploading packages.config. Make sure they are mirrored.Also configure the framework build to push there and run it.Delete package sources in the IniTech.TpsReports solution and add our one source of thruthAll developers can now only use these approved packages + in-house artifacts that are createdOption: create an “unstable” feed to test out new versions and migrate to teh “real” feed when they get approved (there is a push upstream feature for that on MyGet)