Sunday, 15 March 2009

AttachFile Plug-in

attachfile

I’m preparing a release to CodePlex and the Windows Live Gallery of my Windows Live Writer plug-in Attach File. The battery on my laptop is about to run out though so I honestly can’t be bothered with the MSI file that is a requirement of the Gallery submission. So, at the bottom of this post is a direct link to a zip containing the DLLs needed to add the plug-in functionality to Live Writer. Simply drag the two files to the Plugins folder of the Windows Live Writer installation directory.

I should note that the second of the two files is a third party DLL from CodePlex that provides the FTP communications layer. I will eventually do my own, but credit where it is due. The FTP project can be located at http://ftpclient.codeplex.com/.

Also, the icon is from FamFamFam. And yes, the link below was created using AttachFile.

AttachFile.zip

Attach To Process

Just a quick note on debugging Windows Live Writer plug-ins. I only mention it because I have not seen it discussed elsewhere, you can attach Visual Studio to the Live Writer process. Attaching to the Live Writer process enables you to step through the code line by line, as you would a Windows Forms App in Visual Studio.

If you are already developing Live Writer plug-ins, you may be aware that by adding the following line to the Post Build Event, you can import the plug-in directly into Live Writer upon completion of a successful build.

post build

XCOPY /D /Y /R "$(TargetPath)" "C:\Program Files\Windows Live\Writer\Plugins\"

On loading up Live Writer after successful build, switch back to Visual Studio and click on Debug > Attach to Process. You’ll now be presented with a window similar to the one in the screenshot below.

Attach

Double clicking on WindowsLiveWriter.exe will now provide debugging capabilities to the plug-in. Try it, create a a couple of break points and start interacting with the plug-in in Live Writer. I use this same technique for debugging Windows Services.

Wednesday, 4 March 2009

Setting Up a Windows Service Project in VS2008

Setting up, debugging and deploying a windows service can be a daunting task, but it is actually relatively straight forward. Relative to something ever so slightly more difficult to a console application.

Before I jump into the steps I have one piece of advice that has helped me side step a lot of grief. It is possible to debug a windows service using Visual Studio, by installing it on your development machine. The process can become a bit laborious though, in the early stages of development. My advice is to write all the guts of your service in a separate class library and add that library (as a reference) to a console application in the same solution. Use the console application to debug the majority of your code before adding the windows service project. You can then debug the various service state changes with the windows service itself.

Also, I have noticed that running a windows service on Vista is not exactly the same as running a service on Windows Server 2003. I don’t always do it, but I recommend some sort of internal logging so that you can work out any quirks without the aide of Visual Studio. I have my own code, that I am preparing to add to my K3R library, but I notice Live Labs have there own library on CodePlex now. The Live Labs logging library can be found here.

  1. Add a windows service project. Add references to your class libraries, if you have them.
  2. Open up Service1.cs (I’ve renamed mine to Service.cs) in design mode. This can be achieved by right clicking on Service.cs and selecting “View Designer”.
  3. Right click anywhere on the grey area of Service.cs and select “Add Installer”. This creates a new file called ProjectInstaller.cs.
  4. In the design mode of Project Installer.cs, I renamed serviceProcessInstaller1 to ServiceProcessInstaller and selected the account I would the service to run under. This is all done by selecting what was serviceProcessInstaller1 and opening the Properties window.
  5. I also renamed serviceInstaller1 to ServiceInstaller and personalised a few of the Misc properties; Description, Service Name, etc… The important property here though, is StartType. The selections you make here should be self explanatory.
  6. Add a setup project to the solution. I called mine ServiceSetup.
  7. Right click on the ServiceSetup project and click Add > Project Output. Select the windows service project from the drop down and add the project’s “Primary output” to setup project.
  8. Right click on the Service Setup project again and click View > Custom Actions. Right click on the root of Custom Actions and click Add Custom Action. Select the “Primary output” (of the Service project) from the Application Folder.
  9. Set up the properties of the new setup project, getting briefly annoyed that there isn’t a British locale.
  10. You are there. Providing your solution builds, you’re ready to install your service on a test machine. Remeber, you will have to start the service manually first of all, even if you have set the StartType to Automatic.

I should say, this post really constitutes a quick start guide and if you’re looking for more detailed information, I recommend an excellent series of posts on Arcane Code, links of which I’ve listed below.

  1. Getting Started
  2. Getting Started
  3. Adding the Installer
  4. Debugging Windows Services
  5. Controlling Your Service from Visual Studio
  6. Controlling Your Service from Another Application
  7. Sending Commands to your Windows Service
  8. Pulling in the Event Log for your Windows Service

Tuesday, 3 March 2009

Operator, can you connect me to 555 Coolsville?

I’ve admired the operator keyword from afar for some time, the ability to overload operators and conversions is pretty wicked. It wasn’t until this morning that I had a requirement, so I’ve had a little play, in order to understand the basics.

My first experiment was very basic, my intention was to create a class that I can add together using the + operator. The code is below.

public class Account {
 public int Balance { get; set; }

 public static int operator + (Account a, Account b) {
   return a.Balance + b.Balance;
 }
}

var a = new Account { Balance = 5 };
var b = new Account { Balance = 5 };

int totalBalance = a + b; // 10

Now I happen to think that is pretty smart. My example was very basic, but think of all of the calculations that could be encapsulated within an operator overload. Within this framework you could calculate multiple property values and even output to a secondary custom class representing a balance sheet.

This last example was created to understand the relationship between calculations involving more than two instances of a class. Before I continue, I realise there are already short cuts in place to quickly create Generic lists. This example overloads the % operator to allow the quick creation of Generic lists. What is particularly interesting is that you can describe the relationships between your class and any other class that it may encounter.

public class Account {
 public int Balance { get; set; };

 public static List operator %(Account a, Account b) {
  return new List {
   a,
   b
  };
 }

 public static List operator %(List a, Account b) {
  a.Add(b);
  return a;
 }
}

var a = new Account { Balance = 5 };
var b = new Account { Balance = 5 };
var c = new Account { Balance = 6 };

(a % b % c).Count; // 3

Imagine the example above returning a custom collection or another object that allowed you to query the three accounts simultaneously for balances and other financial totals.

Saturday, 28 February 2009

Premature Announcement of Nerdy Wonderment!

This post is really premature but I’m really excited. I’ve written my first Liver Writer plug-in! I didn’t say you would be excited.

Windows Live Writer is my primary blogging tool on Windows and OS X (via Parallels). The software has a really nice Windows Live style interface and allows you (the user) to write into a content area styled the same as your blog. You can save drafts locally and online and of course you have all the little niceties like Spell Checker. The best bit by far though is it’s extensibility.

Live Writer provides a simple but powerful .NET 2.0 (bring on 3.5) API that can be used to extend Live Writer’s functionality. Plug-ins I have already installed allow me to insert Flickr & Facebook photos, insert styled code examples and broadcast new blog entries to Twitter.

There was something I felt was missing though, and I was unable to find an existing plug-in that exactly fit my requirement. While my blog is obviously hosted with Blogger, my main site http://www.dogma.co.uk is hosted privately. The two are visually separate entities at the moment, but I hope to bring them under one design, once I come up with a design that I like. As a rule then, when I want to link to something from my blog, I first upload the file to a sub folder of my main site and add the link in my post. Until the creation of my plug-in – have you guessed what it is yet? – I would use Visual Studio 2008 to upload the file, as it is my primary FTP client.

My only FTP client, I am irrationally against installing software that is designed solely as an FTP client, this irrationality does not include plug-ins however.

My plug-in is a FTP client for Windows Live Writer. In it’s current state it is functional, but I will not release it into the wild yet as it is not very pretty, and is missing some important features. Hence the “premature” prefix of the title.

The plug-in, currently going under the name “Attach File”, at this moment allows you to specify FTP details and the HTTP equivalent URL for FTP area. These details are stored in Live Writer, so you only need to enter them once, the settings can obviously be updated as required.

Once the details are stored, you have access to an option called “Attach File…” from the main Live Writer window.

Clicking “Attach File…” brings up a dialog box with a list of files and folders, this list can be traversed up or down to the root folder you specified in your settings.

When you have located the folder that contains the links you require, you can either double click on the desired file to add the corresponding anchor tag to your post, or you can select a number of files and/or folders.

Selecting more than one file or folder will insert an unordered list of anchor tags into the blog post. Awesome.

Why is it unfinished? Well firstly it is still pretty ugly with no images or application icons. The main reasons though, is the complete lack of error handling and a few missing features that I would to see in the plug-in before it is released.

  1. Most importantly, the ability to upload a file to the FTP area.
  2. The ability to create multiple FTP profiles.
  3. The list and item templates are not hard coded into the plug-in. I am going to provide an interface to allow the user to amend the header, footer and item templates of the unordered lists and the link template of the individual anchor tags.

I really can’t wait to get the thing finished, but was bubbling with excitement and my wife is out.

As ever, any comments are appreciated. If you have similar plug-in or if you are interested in my humble project, I’d love to know. When the project is released, I’ll be putting it on CodePlex.

Saturday, 21 February 2009

JavaScript/CSS bookmark tip for Visual Studio

pixlr

Visual Studio is great, Visual Studio 2008 Standard Edition is my favourite iteration so far and the best IDE I’ve had the pleasure of using. I sometimes think that the only reason I don’t stray into Java and PHP is because it’d mean giving up Visual Studio. I cannot state this strongly enough, Visual Studio is great.

There is always room for improvement however, and in this post I am suggesting a workaround to make working with JavaScript and CSS a bit easier.

When working with C#, VB, (X)HTML, XML, etc… in VS2008 you have a number of ways to make large files more manageable. XML tags are collapsible, as are C# and VB methods and classes. You can also create collapsible regions to group collapsible objects together. There are drop down lists and class views for jumping to specific objects and sections. There are a lot of sign posts in VS to prevent you from getting lost.

Unfortunately, none of this exists for JavaScript and only a limited amount for CSS. Especially unfortunate as both of these file types can become quickly unwieldy. It is tempting to split JavaScript files into smaller more manageable files, using jQuery plugins as an example. But when your trying to keep HTTP Requests to minimum, recombining JavaScript files can be laborious. The situation with CSS files is less dire because of the class view, but a big web site usually means big CSS files and the class view doesn’t always cut it for me.

My tip is quite an obvious one, I can’t believe it has taken me so long to work it out. The tip is bookmarks, I know clue in the title. By chopping up your code into logical sections and assigning each of those sections a bookmark, large files become a great deal easier to navigate. I told you it was obvious, I’ve been using bookmarks since Word ‘97.

Adjusting the environment to my requirements was straight forward. I’ve given up trying customise keyboard shortcuts, there just too many. I hate chords! So, my first step was to create a new toolbar (called Bookmarks). In the newly created toolbar I placed three buttons from the Edit menu, “Enable Bookmark”, “Prev Bookmark in Doc” and “Next Bookmark in Doc”. The first toggles the bookmark on or off at any given location, the second two allow me to navigate my JavaScript and CSS files a lot more efficiently.

That’s it, sorry if it’s a bit anticlimactic, but it has really sped things up for me.

Friday, 20 February 2009

Bookmarklets

Bookmarklets

I’ve been using bookmarklets for a while now. I find them to be great time saving devices, so I’m always surprised when I’m confronted with people unaware of the phenomena. In an attempt to correct this wrong, I present this post. The purpose of the post is not spend a great deal of time explaining what they are, Wikipedia does it more comprehensively here. What I’ll actually try to do is tell you how I use bookmarklets. Then, having never heard of bookmarklets before, you’ll be able to decide if bookmarklets are for you.

So very briefly, bookmarklets are browser shortcuts that contain JavaScript (like JavaScript in an Anchor tag). When you hit the bookmark, the said JavaScript performs a task, generally, in relation to the page you are on. As I say, more info here. Be careful though, don’t just drag any old nonsense into the bookmarks bar, make sure it comes from reputable source.

I mainly use bookmarklets for social networking and communication, whether it’s bookmarking to Delicious or composing an email with Google Mail. Here is a list of some of the socially orientated bookmarlets I use, a brief description, and when possible, the source url.

Bookmark – The “Post to Delicious” bookmarklet from Delicious. Clicking “Bookmark” brings up a Delicious dialog that allows you to enter tags and a description for the current page.

Compose – Found here, this bookmarklet brings up a Google Mail Compose window, as if you’d opened it directly from your Google Mail account. I had to alter the url embedded in the JavaScript slightly to work with my Google Apps account.

Share – My friends on facebook get an influx of links throughout the day because of this bad boy. The bookmarklet can be found in the Posted Items section of facebook and works in a similar fashion to the Delicious bookmarklet. You can also send links directly to your friends, rather than sharing with everyone.

Subscribe - Not used so much now as I tend to use Twitter as my primary source of information, but still handy. This bookmarklet, supplied by Google Reader, detects RSS feeds on the current page and subscribes them to my Google Reader account.

Twitlet – As my latest find, this bookmarklet prompted this very post. Twitlet allows me to tweet from a JavaScript dialog, I can also use shortcuts to provide context to the current page. I’d like to see a nicely styled popup in the future, but still very useful. The bookmarklet can be found here.

In addition to social networks, I also use booklets in my development. Admittedly a lot of them tend to fall by the wayside as similar functionality appears in Firebug or alike. One bookmarklet that has stood the test of time is Visual Event. Visual Event provides a visual reference for all JavaScript events registered on the current page. Current JavaScript libraries (at time of writing) include, jQuery, Prototype, MooTools and YUI. A wholly more accurate description can be found here.

Well that’s it, great personalised functionality with cross browser support. I’ve even got my Delicious and facebook bookmarklets working on my iPhone. Rock on.