New CodeRush 2.5.4 WCF Templates



Those of you who have worked with me or have seen me present know I am a big fan of CodeRush and Refactor Pro, from DevExpress. In fact, if you get me talking about it, you can’t shut me up. To sum all my fan boy slobbering praise down to what you really need to know if you have never heard of it or checked it out, CodeRush is a tool that:

  • Is like a combination of Visual Studio Code Snippets and Intellisense, both on super-crazed, interplanetary steroids
  • Adds code visualization, navigation, and clipboard tools that will also make you way more productive while pounding out code
  • Bottom line: it is a tool that even if used to only 10% of its capabilities, will pay for itself within a week or two in making you more productive
  • Comes with Refactor Pro, another essential tool to make it much faster and safer to refactor your code to make it clean

Anyway, last week they released a small update (2.5.4) which includes some awesome new templates for WCF coding.

If you have done much WCF coding, you’ll know that WCF coding is more about adding attributes and config file entries than it is about writing imperative code, at least for the WCF specific stuff. For examples, defining a service contract, operation contracts, service behaviors, transaction flow, etc. is all done with attributes in code. But there are also a few common constructs you’ll write in code, such as creating an instance of ServiceHost or creating a client-side proxy class by hand because the code that VS and svcutil generates is way more verbose than it needs to be.

Then there is the config file… this is where you will spend a lot of your time, and there is nothing worse than writing a lot of angle brackets by hand (even with intellisense helping) just to get some structure in place for the values you really need to provide. The SvcConfigEditor helps some here, but it sometimes does a little too much magic behind the scenes for you, so knowing exactly what is being set means digging into the config file yourself.

This is where the CodeRush templates come in. With a little collaboration with an unnamed individual at IDesign, they have come up with a great set of templates for all the most common code chunks you will add to your WCF services and clients.

For example, instead of having to type 17 characters to decorate an interface with a ServiceContract attribute (see… it physically hurt me to have to actually type all those characters just now, knowing CodeRush could do most of that for me if I were just in VS), I can type 3 plus the space bar – [sc and I have [ServiceContract()] in my code, with the focus inside the parens in case I want to set some properties, and enter jumps me to the end of line in case I don’t. Maybe those extra 14 characters don’t sound like a lot to you, but add them up over and over and you will get the idea. Here is a tool that can make you 82% faster immediately. OK, well, intellisense saves you some of that, and that assumes you never have to think when coding. I think only Mark Miller, the architect of CodeRush, is that good. But still, trust me, you will feel the time savings in the form of keystroke savings very quickly as you adopt CodeRush.

Even more important, consider a service host you are adding the first service to. To get that service hosted, you need a chunk of code like the following in your config file:

<system.serviceModel>
<services>
<servicename=”MyNamespace.MyService”>
<host>
<baseAddresses>
<addbaseAddress=”http://localhost:2112/”/>
baseAddresses>
host>
<endpointaddress=”MyService”binding=”wsHttpBinding”
contract=”MyNamespace.IMyService”/>
service>
services>
system.serviceModel>

There are over 200 characters of pure structure in there. I can write this now with one character – s and a space bar hit. Placeholders are put on the items I do need to customize (service name, addresses, binding, and contract) so it is very quick to move from one to another by overtyping, enter, overtype, enter, etc.

Lots of great templates along these lines. For the XML config file entries, you can see the top level ones in the template browser:

image

These are all backed by stuff hiding in that System folder that let you hit all the most common combinations such as bt for a TCP binding, bh for an HTTP binding, etc.

Mark even added some new smarts for detecting parent elements so it will check if the binding has an enclosing parent, and will only add it if not.

Code wise the two there for now are to add a service host block:

using (ServiceHost host = new ServiceHost(typeof(ApplicationService)))
{
host.Open();
Console.WriteLine(“Press Enter to Exit…”);
Console.ReadLine();
}

And a “hand coded” proxy class for a client:

publicclass MyServiceProxy : ClientBase, IMyService
{
public MyServiceProxy() { }
public MyServiceProxy(string endpointName) : base(endpointName) { }
}

As long as you have the interface type in scope, you can then just implement the interface, add the calls through the base class Channel and you have a lean, clean proxy in about 30-60 seconds, usually faster than you can get a verbose messy one through Add Service Reference or svcutil.

Anyway, if you are a WCF programmer and using CodeRush, check out the new templates under the WCF categories in C#/VB and XML code types. If you are a WCF programmer and have not considered CodeRush, maybe you should now.