RTUR.NET

.NET and Open Source: better together

NAVIGATION - SEARCH

Using NuGet with BlogEngine.NET

nuget-logo2If you are .NET developer you no doubt heard about NuGet – this little package manager rapidly becoming a big deal in .NET open source community. There is a lot to like about it, and if you want to learn more go subscribe to Phil, Scott Hanselman or Scott Guthrie blogs to stay on top. To me interesting question is how NuGet can help me with daily development, for example, how can I apply it to build things for BlogEngine? Lets check this out.

Business as usual

vsfilesA while ago I did a widget that shows visitor info in the sidebar. It got out of date, and I decided to update it for BE 2.0. I downloaded old source code from my own blog, it is a ZIP file that I had to extract locally. Inside I found “widgets” folder, dragged it over “widgets” folder in BE 2.0 project and content of widgets got merged. Simple enough, right? And this is how it works in BlogEngine world, you download/unzip/merge files into your blog’s directory. To upgrade widget I added couple references to namespaces that changed in 2.0 and call it done. Ok, how do I package it for distribution? Easy – create folder called “widgets”, inside it create folder called “Feedjit” and add updated source files there. Then right-click “widgets” folder –> send to –> compressed (zipped) and I’m done. Resulting ZIP file is what other people will download, unzip and merge to their blogs to be able to use Feedjit. That not hard but obviously screams for automation. And we could build unzip/merge process into BlogEngine pretty easily, but there is so much better way.

Lets get NuGety

What I want is to package my widget into .nupkg format used by NuGet. Lets do it step by step.

1. Create working directory, say c:\packages and add “Feedjit” folder to it. Inside it create folder “content” – you can think of it as your blog root directory.

2. Copy folder structure created to build standard ZIP file and move it to that “content” folder. You should get: c:\packages\Feedjit\content\widgets\Feedjit and 4 source files inside it.

3. Download NuGet.exe and move it to c:\packages.

4. Now create spec file for the widget, it looks like this:

<?xml version="1.0" encoding="utf-8"?> 
<package> 
  <metadata> 
    <id>Feedjit</id> 
    <version>1.0.0.0</version> 
    <authors>rtur</authors>
    <description>Feedjit lets you see friends and visitors as they arrive on your blog or website.</description> 
    <language>en-US</language>
  </metadata> 
</package>

and save it as Feedjit.nuspec in the C:\packages\Feedjit (at same level as “content”).

5. Open command prompt, change to c:\packages and run this line:

nuget pack feedjit\feedjit.nuspec

The result should look something like this:

cmdresult

 

Congratulation, we created our first NuGet package! But… how do we know it works? Lets install it!

The Visual Studio story

In my Visual Studio project, I delete “Feedjit” folder with all source files in it. That effectively uninstalls widget from solution. Here is how to install it from NuGet package we created:

1. Right click BlogEngine.NET project and select “Add Library Package Reference”. In the dialog box, click “Settings” in the lower left corner.

2. To add our “c:\packages” as local repository, fill in name and source and hit “ok”

localrepo

From now on, you should be able to select that local feed and install any NuGet package that you have inside c:\packages folder. Here is what my local repository looks like:

addpkg

 

 

Click “Install”, and Feedjit will be added to solution, just like that. We got all power of NuGet here, for example you can just as easily uninstall package:

uninstallUpdate and versioning all provided by NuGet, so if you happen to have multiple versions of your widget, extension or theme – you’ll just package it with new version number and you done. Pretty cool stuff. What could be more cool?

BlogEngine.NET gallery

Having local package repository is great. You could even submit your package to official NuGet feed and let people know about that wonderful extension you created, so they can install it from Visual Studio. But can we haz app store plz?

Yes we can! Please meet dnbegallery.org Ok, just to be clear – see that “beta” in the logo? It is wrong. It should really be “alpha” so if you crash site it is all your fault. But it should let you to register and upload your packages. Don’t’ tell anyone, just try it yourself and let us know what works and what doesn’t, we are very interested in your feedback. The process is very much identical to Orchard Gallery, except we don’t require email validation at this point so it is simpler a little.

Don’t get too excited just yet, we still missing a link when blogger can go to admin and click “install” button. Right now, it is just what it is – online repository to store and promote your packages. To install it, someone would have to download .nupkg file and then either rename/unzip/merge manually or add gallery feed to Visual Studio sources and get it from there. But that “Install” button is very high on to-do list.

dnbegallery

Comments (14) -

Hi,
I&amp;amp;amp;#39;m Francesco, an italian developer, I try to open link in the post but at this moment dnbegallery do not work, this is the msg:

Exception Details: System.Data.SqlServerCe.SqlCeException: There is a file sharing violation. A different process might be using the file. [ D:\Hosting\7173912\html\cms\App_Data\Sites\Default\Orchard.sdf ]

at this line:

Line 23:             builder.Add(T(&amp;amp;amp;quot;&amp;amp;amp;quot;), &amp;amp;amp;quot;2&amp;amp;amp;quot;, BuildMenu);

Bye from Italy (Florence)

United States Janier Davila

Being a Nuget and BlogEngine user, I got to love the idea! I went ahead and registered but the site crashed on me twice with a:

There is a file sharing violation. A different process might be using the file. [ D:\Hosting\7173912\html\cms\App_Data\Sites\Default\Orchard.sdf ]



Line 81:
Line 82:         private PackageListViewModel BuildList(string searchTerm, string authorName, string packageType, string packageCategory, PagerParameters pagerParameters) {
Line 83:             Pager pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
Line 84:
Line 85:             var packageTypeTerm = GetPackageTypeTerm(packageType);

So yes, I guess it is not production ready just yet. Keep it up!

Having the same issue.

There is a file sharing violation. A different process might be using the file. [ D:\Hosting\7173912\html\cms\App_Data\Sites\Default\Orchard.sdf ]

Yes, this file sharing is killing us :( Presumably, this is due to our host (GoDaddy) having issue running SqlServer CE on its farm (http://orchard.codeplex.com/discussions/246199). Looking into way to upgrade to full Sql Server.
This goes away if you hit refresh button in the browser, but very annoying.

Just confirming I&amp;amp;amp;#39;m seeing the same file sharing errors.  Nice initiative anyways.

Ok, we are on SQL server now. Should not see that stupid file sharing violation any more.

Welcome back Ben!

And down again... looks like GoDaddy blocked connection to SQL server running outside their farm. Opened support ticket.

Getting this error when going to Widgets links:

URL: http://dnbegallery.org/cms/Orchard.Gallery/Packages/ByCategory?categoryName=Administration

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 125:                var shape = _orchardServices.ContentManager.BuildDisplay(contentItem, &amp;amp;amp;quot;Summary&amp;amp;amp;quot;);
Line 126:                shape.AllowOwnerActions = false;
Line 127:                if (_userPackageAuthorizer.AuthorizedToEditPackage(contentItem.As&amp;amp;amp;lt;PackagePart&amp;amp;amp;gt;().PackageID)) {
Line 128:                    shape.AllowOwnerActions = true;
Line 129:                }


Source File: d:\hosting\7173912\html\cms\Modules\Orchard.Gallery\Controllers\PackagesController.cs    Line: 127

Seems to be working great now!

Great blog about blogengine.net, I&amp;amp;amp;#39;m getting more and more interested in trying it ,the more I read about it.

its great to hear all such stuff. I too started contributing to blogengine, very soon you will hear lot more from me.

Nice post, NuGet rocks!

Its time for Nuget. Its been here since long time but couldnt used earlier.

Nuget makes life easier. Nice post !!

NuGet rocks <3
thank you for this nice article !

Pingbacks and trackbacks (3)+

Comments are closed