Create custom NuGet packages and host your own Nuget repository

In this tutorial, we’ll cover some basics on how to create a Nuget package. We’ll also look at a way to host your own private Nuget repository. To complete the tutorial, you’ll need to download the NuGet.exe command line utility.

Note: You should consider putting this utility in your system path. I have a dropbox folder on all my machines C:\Users\Kyle\Dropbox\Utils that I put command line utilities in, and then update the PATH variable for my machine.

1. Start by creating a folder for your NuGet packages. I recommend C:\Users\your_username\.nuget (if you create this folder in explorer, it may complain if you don’t name the folder “.nuget.” with the trailing period. Explorer will strip off the trailing period when it’s created).

2. Download the .zip sample project here, and unzip it into a subfolder with the name of the Nuget package you wish to create (i.e. \.nuget\NugetDemo\)

3. You’ll see folders here for /content/ and /lib/.

/content/ – any files here simply unzip into the root of the project, so anything you put here will dump as is to the project the package is being added to. You just match the structure you want to have it unpack into and Nuget will unzip it into your package accordingly. The example from #2 uses a ‘Controllers’ and ‘Views’ folder from an sample C# MVC project. The root content folder also contains a web.config.transform. The changes in .config.transform files are added to the matching .config file in the project. In this example, I have an appsettings key I am adding.

/lib/ – contains your binary files you want to include in subfolders based on framework version. In the example from #2, I have a .NET 4.5 (net45) subfolder, and a dll called Utils that I am packaging in. This will be added to my project as a referenced dll when I install the Nuget package. Utils just contains some sample extension methods which are used in the Controller and View from /content/.

/tools/ – (not included, but could be here if necessary) is for powershell scripts you wish to have executed automatically at installation time. I haven’t included any in this sample, but there is plenty of documentation out there on Powershell and what can be done with Nuget packages.

readme.txt – this file is in the root folder and is opened automatically (but not added to the project) when the package is installed.

4. From the root folder (the one with readme.txt) run the command ‘nuget spec MyPackageName’. This will create a file named MyPackageName.nuspec. Note: Be sure Nuget.exe is added to your %PATH% variable as noted earlier

5. Open and analyze MyPackageName.nuspec. This file contains information about your package (Author, License URL, Version, Project URL, License, Copyright, and Dependencies). Go ahead and delete the ‘Dependencies’ element and update the rest of the information to match how you would like it to look.

6. After you finish updating the MyPackageName.nuspec, run the command (from the same directory as #4) ‘nuget pack’. This will create your Nuget package. Congratulations, you just wrote your first Nuget package! To make the next step simpler, move the newly created .nupkg file and place it in the ‘C:\Users\your_username\.nuget\ folder.

7. Inside Visual Studio, select “Tools > Nuget Package Manager > Package Manager Settings’. Then select “Package Sources” and use the “+” icon to add a new source. Attached is a screenshot of how I have added my own person Nuget repository.

personal-repo

8. Now, create a basic C# MVC application. Right click your new web project and select “Manage Nuget Packages”. You see your source on the left rail. Go ahead and add your package you created from step #6. Note that ‘Controllers’ now has an ‘Extensions’ subfolder, ‘Views’ now has an ‘Extensions’ subfolder, Web.config has an AppKey named ‘ExtensionsKey’ and your project has a reference named ‘Utils’ that was not there before. Also, a file called “readme.txt” showed up in your solution explorer although it was not added to your project.

add-nuget-package

9. If you debug/run your application and head to the Url /extensions/strings/ you’ll see that the Extensions controller action ‘Strings’ will execute, leveraging the namespace/dll Utils to present some information. Congrats!

 

Create custom project templates for Visual Studio

You can use the ‘VSIX Project’ template to create a Visual Studio extension. This project type is installed as part of the Visual Studio SDK and you will need it to complete the exercise in this post.

1. Create a project which you wish to serve as the basis for your new project template. In this example, we will create a Console Application.

1-console-app

2. Make some changes to the application and how you would like it to behave as a project template. For this example, let’s rename Program.cs to Start.cs and also add a new text file called ReadMe.txt.

2-console-changes

3. Once everything is to your liking, select “File > Export Template” from the Visual Studio menu. You will want to select “Project Template” as the template type and select the project from your solution (if following the demo, there will be only 1) from the dropdown. Click ‘Next’.

3-project-template

4. Specify the details about your template. Give it a name, a description, and optionally an image if you would like. Be sure to uncheck the ‘Automatically import the template into Visual Studio’. We will be bundling this into a VSIX package so you can deploy it to others as well. Note: This will export the template to “C:\Users\your_name\Documents\Visual Studio your_version\My Exported Templates\” by default.

4-project-template-details

5. Close your current solution, and create a new project of type “Extensibility > VSIX Project”. (note: If you do not see this template, ensure you installed the Visual Studio SDK as noted at the beginning of this post).

5-vsix-template

6. Update the VSIX manifest file (which should open by default) and specify details for ‘Product name’, ‘Description’, and ‘Author’.

6-vsix-details

7. Right click your new VSIX project, and select “Add > Existing Item”. Navigate to the export folder from Step #4 and select the .zip file that was created for you.” (Note: You may need to change the file type filter to “All Files (*.*)”).

7-add-reference-to-template

8. Update the properties for the item you just added to be “Copy to Output Directory: Copy always”.

8-copy-always

9. Back in your VSIX manifest file, select the “Assets” tab on the left rail. Select “New” and specify the Type as ‘Microsoft.VisualStudio.ProjectTemplates’. For ‘Source’ specify “File on the file system” and specify the .zip file you just imported in step #7. It should be in the root of the project folder. Click “Ok” to continue.

9-vsix-assets

10. Navigate to the bin\Debug folder of your VSIX project folder in explorer. You should now see a VSIX file located here. Double click this file, and install the extension. Then close Visual Studio, and re-open it. If you navigate to “Tools > Extensions and Updates” then click “Templates” you will see your new Visual Studio template. Congrats! This is also navigable in the ‘New Project’ window under “Visual C#” (or Visual Basic” if you did this tutorial using Visual Basic).

10-vs-template-project

10-vs-template-window

Optional extras: If you want to change the location that the template installs to another folder, you will need to make some changes. We will, for this example, want our Console Application to appear inside “Visual C# > Windows Desktop”.

11. Navigate to the “bin\debug” folder of your VSIX project in explorer (same as Step #10). Right click your VSIX file and change the extension to .zip. Unzip the contents to a folder of the same name. You should see something like the following.

11-unzip-vsix

12. Go into the ‘ProjectTemplates’ folder, and add a new folder called “CSharp”, and below “CSharp” add a folder called “Windows”. Move the original .zip file from “\ProjectTemplates\Your%20%Plugin%20%Name.zip” to “\ProjectTemplates\CSharp\Windows\Your%20%Plugin%20%Name.zip”. Go back to the root folder of the zip that you unpacked and re-archive everything into “YourVSIXTemplate.zip”. Rename the .zip file to .vsix extension.

Important note: You will want to mirror the folder structure located at ‘\Common7\IDE\ProjectTemplates’. On my machine this was “C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ProjectTemplates”.

12-vsix-folder-changes

13. Inside Visual Studio, go to “Tools > Extensions and Updates” and uninstall your template you installed from Step #10. Close Visual Studio. Run the VSIX you modified from Step #12. You will now see your template in the proper location.

13-vsix-proper-folder

 

SideWaffle Templates for Visual Studio 2012/2013

The SideWaffle extension, (http://sidewaffle.com/) adds a bunch of useful Snippets, Project- and Item Templates to Visual Studio. The purpose is to make your daily work in Visual Studio a richer and more productive experience.

SideWaffle for Web

  • Project templates

    • Blank App
    • Browser Link Extension
    • Browser Link Extension (simple)
    • Code Snippet Extension
    • Durandal451
    • Google Chrome Extension
    • Google Chrome Theme
    • Google Chrome Web Store App
    • HTML5 Boiler Plate v4.3
    • Nancy demo
    • Nancy empty project with ASP.NET host
    • Nancy empty project with ASP.NET host and Razor
    • Nancy empty self hosted
    • Nancy empty self hosted with razor
    • Nancy with ASP.NET host
    • Nancy with ASP.NET host with Razor
    • Nancy with self host
    • Nancy with self host with Razor
    • Windows Azure WebJobs Console Application
  • Item templates

    • _preprocess.xml
    • A basic NuGet .nuspec file
    • An advanced NuGet .nuspec file
    • AngularJs Controller using $scope
    • AngularJs Controller using ‘Controller as’
    • AngularJs Directive
    • AngularJs Factory
    • AngularJs Module
    • AngularJs TypeScript Controller using $scope
    • AngularJs TypeScript Controller using ‘Controller as’
    • AngularJs TypeScript Directive
    • AngularJs TypeScript Factory
    • AngularJs TypeScript Module
    • ASP.NET Scaffolding T4 files.
    • Basic build script
    • Basic props file
    • Basic SignalR Hub and HTML Client Page
    • Basic targets script
    • Browser Link extension (VS2013 only)
    • Caliburn Micro Bootstrapper
    • CKEditor plugin
    • Code Snippet
    • Customize ASP.NET T4 Files
    • DurandalJs Controller
    • DurandalJs main.js
    • DurandalJs Service
    • DurandalJs ViewModel
    • Editor Drop Handler class
    • Empty PowerShell file
    • Favicon .ico File
    • FirefoxOS manifest
    • GruntJS configuration file
    • HTML Smart Tag (VS2013 only)
    • HTML Validator Class (VS2013 only)
    • HTML Whitespace Removal HttpModule
    • Humans.txt File
    • Jasmine Spec and HTML files
    • Jasmine Spec file
    • JavaScript IIFE
    • JavaScript IIFE Module/Namespace
    • jQuery Plugin
    • JSHint Ignore File (.jshintignore)
    • JSHint Rules File (.jshintrc)
    • Karma Configuration File
    • Knockout Custom Binding
    • Nancy bootstrapper
    • Nancy module
    • Ninect Controller Factory
    • NUnit Fixture
    • Offline Application Cache Manifest
    • Package Definition (.pkgdef)
    • Package.json for NodeJS
    • QUnit Spec and HTML files
    • QUnit Spec file
    • Readme.md File
    • Readme.md Markdown File
    • RequireJs File
    • Robots.txt File
    • SideWaffle Definitions Folder
    • SideWaffle Item Template
    • SideWaffle Project Template Files
    • SVG File
    • TinyMCE plugin
    • TSLint Rules File (tslint.json)
    • VS Command Table (.vsct)
    • Web API 2 Empty Controller
    • WebVTT File
    • Windows Azure Blob Upload Helper
    • Windows Azure Cloud Service Plugin
    • Windows Azure Table Storage Entity

 

 

Visual Studio Shortcut to Attach to IISExpress

Looking for a way to quickly attach to the IISExpress process for your ASP.net development?  In previous versions of Visual Studio, I used the ‘Macro’ feature to search for and attach to a process with the name iisexpress.exe.  I could then bind this to a keyboard shortcut (like F1).

Everything worked great, however, in Visual Studio 2012 the ‘Macro’ feature has been removed.  Instead, the approach now required is to create a Visual Studio Add-In.  I started working on this and then found there was one that actually already existed, and it works well with Visual Studio 2012.  No administrative privileges needed to install it either.  You may need to restart Visual Studio to see it in the TOOLS menu.

Visual Studio Gallery AttachTo Extension Download

Update: The VSGallery extension does not support VS2013 as noted in the ‘SUPPORTS’ section of the page above. A user submitted a merge request from the main Github branch (https://github.com/whut/AttachTo/pull/5) which updates this for VS2013. I have downloaded and compiled this into an updated VSIX that I am providing here for download if you use VS2013.

Without going into too much detail why, I use this instead of the “IIS Express” features built into Visual Studio because I still haven’t quite figured out how to make IIS Express work with port 80 and not go ballistic on my applicationhost.config file or require some elaborate configuration changes.  Ideally, I would be able to associate my website project to a <site> entry already in my applicationhost.config that not change any of my bindings, although I haven’t quite figured out how to do that yet.

In the meantime, the AttachTo extension seems to work great, and Visual Studio 2012 gives you great control over what appears in your menus.   If you go to TOOLS > Customize, then select the “Commands” tab you will see where you can personalize your menu.  You can re-arrange the add-ins shortcuts and/or remove unused ones it creates.  Also, TOOLS > Options > Keyboard and search for Tools.AttachtoIISExpress.  You can then bind this to any keyboard shortcut you like.

 

My strategy for passing the Microsoft 70-515 Exam.

I recently took and passed the Microsoft 70-515: Web Applications Development with Microsoft .NET Framework 4 exam.  This exam covers:

  • Master Pages
  • Themes
  • State Management
  • Server Controls/User Controls
  • Input Validation
  • Debugging/Deployment
  • Client-side Scripting (MS Ajax/jQuery)
  •  HttpModules/HttpHandlers
  • WCF
  • LINQ
  • Data-bound controls (Gridview, Formview)
  • User Authentication/Profiles
  • MVC2

I had several strategies when approaching this exam.   The first was working through each chapter and ensuring I did the practices at the end of each chapter, even if I felt I knew it well.  I created a single solution and folder for each chapter.  When I was done, I had a nice handy reference for everything I had learned bundled into a single solution.

The second thing I did was use my Pluralsight subscription to review the following courses:

ASP.net 3.5 Fundamentals (only deployment and health monitoring)
ASP.net Dynamic Data Fundamentals

ASP.net Ajax Fundamentals
ASP.net 3.5 Working with Data
ASP.net 3.5 Security
ASP.net MVC 2.0 Fundamentals
WCF Fundamentals
LINQ Fundamentals

The final thing I did to help prepare me for the test was take alot of notes.  I used Google Documents as I read through each chapter to make a copy of my notes, and I also then converted those notes at the end into flash cards.  This helped my memory retention when I quickly reviewed them each time.

All in all, I think the exam was a great experience and exposed me to some features I would not have otherwise used in my day job.   There were a few features, like Web Parts, I just skipped, but overall everything was a great learning experience.  I’m currently using this same approach for the 70-516 exam, and will post about there here shortly.  Feel free to contact me if you have any specific questions about it.

 

The New Microsoft

Let’s put aside all the discussion of re-branding Microsoft to make them more hip and talk about the developer platform.  I may be stating the obvious here on what many have already witnessed at Microsoft over the past few years, but there appears to be a paradigm shift in the way the company is operating from a development standpoint.

Recently, at ThatConference, I listened to Scott Hanselman give his talk on “One ASP.net” and his discussion reinforced much of what I had been experiencing lately from a developer perspective.   The underlying principal seems to be “If it sells more Windows licenses, it’s good for Microsoft“.  Gone are the days where Microsoft is trying to push a particular piece of technology or development approach, and third party platforms/libraries are being invited to join the party.

That may seem “evil” to focus only on licensing, but think about it for a moment.   What will keep the enterprise, and consumers, focused on buying Microsoft products?  That’s right, developers.  It’s the same audience that Google, Apple, HP, and everyone is trying to attract right now.  Windows is the bread & butter of the Microsoft ecosystem, and maintaining a positive relationship with developers by listening to the community and providing top notch tooling will help enable that.

What are some examples?  Well, for those may not already be following the Microsoft development environment, consider that historically Microsoft has never been “open source” as a business.  I prefer to use that term in quotes, because Google can hardly consider themselves “open source”.  If they were, they would publish their page rank algorithm, which is the main money driver for their business, but I digress.  Things have changed at Microsoft in recent years.  MVC, Entity Framework, Azure SDK, and much more has been released as open source software.  This isn’t the marketing version of “open source” either, you can actually branch and check-in.

Microsoft has even demonstrated this new strategy with CodePlex, a free open source project hosting site.  Phil Haack and his team produced the NuGet package manager which allows other developers, and even Microsoft, to continuously deploy library packages into your projects painlessly.  No more waiting for the latest .NET framework release, just check NuGet.  Look a bit deeper, and you’ll find that Windows Azure now allows you to host PHP, Node.js, and even run Linux virtual machines (*gasp*).

If you’re like me, you’re frustrated that Microsoft Visual Studio isn’t free development software.  This has changed for me recently after I came to a few realizations.  If I am a student, I can get Visual Studio FREE from Microsoft Dreamspark.  If I am a new startup (and supporting Microsoft technology), I can get Visual Studio FREE from Microsoft Bizspark.  Even the “Express” (free) versions of Visual Studio now have unit testing support.   Each release of the Express versions is offering more and more functionality.  Finally, consider that Visual Studio 2012 Professional is now $499.  To put that in perspective, a license for Photoshop (at the time of this post) will set you back $700.   A final comment on this, is that I have used Eclipse, Sublime, Xcode, and other IDEs.   Visual Studio is on another level as far as the quality, UI, feature-richness, and speed of the IDE that the others cannot match.   It is perfectly reasonable to expect a mild cost to be associated with a superior product.

In conclusion, I feel that some some of the bashing that Microsoft gets is warranted, but any of it that is directed at the developer platform needs to stop or at least be tempered.   The new Microsoft appears completely focused on listening to the community as well as providing top notch tooling and services to get us to the end result, which is making great applications.

 

thatConference.com – Day 1

Today was the first day of a new developer conference in the midwest, thatConference.  It is taking place in the Wisconsin Dells @ Kalahari Resort (a very family friendly place).  Clark Sell, and Scott Seely have been the primary driving forces behind the conference, but it certainly has been a collective effort of many people.

The presentations I attended today were:

You, by Leon Gersing

Leon has an extensive background as a ruby developer and is a very enlightened individual.   His keynote speech was about knowing yourself as a developer (or more abstractly, as a person).   An emphasis was placed on establishing healthy boundaries in your life, your work, and your relationships.   He highlighted how it was important to be empathetic, and listen to other people.   He talked about facing our fears and understanding why we make the decisions we do and how to to better ourselves and take steps toward being the developers we want to be.   Every decision we make on a daily basis will bring us closer to, or put us further from our goals in life.

One ASP.net, Scott Hanselman

Scott is championing a new mentality surrounding ASP.net.   His premise, and I admire the approach, is to preach a philosophy of the .NET framework where all components are interchangeable and if you don’t like a particular approach, it doesn’t make it wrong.  Developers are free to choose and use any components they like, wether they are NuGet packages or built-in framework features.

Many new features were highlighted including intellisense improvements for css and javascript, SignalR, NuGet, default browsers, the dynamic data type, and creating a class from a JSON object.   The talk was very entertaining (almost as much as a stand-up comedy show at the Improv) and I commend Scott for doing what he does.   Clearly Microsoft acknowledges Scott’s talent at speaking and he is a great spokesman for the platform.

Knockout.js, Ryan Niemeyer

Ryan gave a great talk on Knockout.js.   His discussion was fast paced and had a deeply technical element to it.  As a non-Knockout developer the talk did move quickly to me, but I was able to keep up by concentrating my focus on everything that Ryan was saying.  He discussed how knockout uses the Model, View, View Model approach and how the objects in Knockout derive from the subscribable classs.

Other important elements in the talk were how to perform bindings (simple and complex), how to make objects observable, and how to use computed properties.   I will likely follow-up this discussion with a Pluralsight video to better understand the framework in detail, but for the hour that I had to commit, it covered a lot of ground.

SignalR, Jeremiah Billman

One of the technologies I had heard of, but never fully understood coming into the conference was SignalR.   After hearing Scott Hanselman discuss it earlier in the day, and then seeing Jeremiah cover it more extensively in this talk, I can say this may be one of the most impressive and most disruptive technologies I learned of.   SignalR allows me to maintain a consistent connection between the client and server.  Data is pushed back and forth between the client and server.  The underlying communications are obfuscated as well depending on the browser capabilities.  The first step SignalR will attempt to use is HTML5 web sockets and it will degrade step-by-step all the way down to long-polling.

Some great uses for this technology are a real-time chat, reporting dashboards that update with real-time data, notification services to your website users or even HTML5 based games.   Jeremiah was a great presenter and gave a very entertaining demo showcasing a HTML5/SignalR ported version of the Rampage arcade game (originally by Midway games).  The characters updated in real time in each of 3 different browsers he had open thanks to SignalR.   Clearly this framework has a huge potential to change the way web development works.

ASP.net and Mobile, Scott Hanselman

In this second talk Scott gave today, he covered the various approaches ASP.net developers who wish to embrace “curly braces and angle brackets” have at their disposal to create mobile enabled websites.  Scott, as always it seems, has a knack to create a fluid, entertaining, and informative talk.  He highlighted ways that both MVC and Webforms users (emphasizing the ONE Asp.net mindset) could take advantage of the new technology.

Several options for mobile enabling design were highlighted.  Blogs and other non-data heavy applications would benefit from response design using some simple CSS3 media queries.  Data heavy, or interface heavy apps, would benefit from using the jQuery mobile framework.   Many smart people are on the jQuery mobile team and Microsoft is taking advantage of this to allow MVC (and Webforms) users to easily create Views which use the same model and present different displays depending on the browser type (mobile or desktop).

Conclusion:

The one prevailing theme I noticed today was how far the dollar stretches with thatConference.   For a $350 ticket, attendees were given a fully catered breakfast, lunch, and cocktail hour dinner.   The presentations are top notch, the resort the conference is hosted at is very family friendly, and things ran smoothly.   Knowing thatConference already has a 2013 date, I will be sure to register in advance next year as soon as it is possible.   I couldn’t be more impressed with everything the guys (and gals) have put together so far.   It’s amazing, inspiring, and an excellent opportunity to network with like-minded individuals.   I’m looking forward to days 2 and 3.

 

Registering ASP.net Membership, Roles, Personalization and Health Monitoring in Windows Azure

While attempting to setup the ASP.net membership provider with the newly updated Windows Azure, I logically assumed I could use the default aspnet_regsql.exe console utility to specify my connection details to setup my Azure SQL database.   Once I ran this utility, I ran into an error message:

Commands failed:

USE [MyDatabase]

SQL Exception:
System.Data.SqlClient.SqlException: USE statement is not supported to switch between databases. Use a new connection to connect to a different Database.

After some research, I landed on this support article which indicates there are new SQL scripts specific to Windows SQL Azure.  Clicking this link, I was able to download aspnet_regsqlazure.zip and use it in exactly the same manner as I was able to use aspnet_regsql.exe.   Hope this helps.