Resharper Live Templates for WCF Data Contracts

30. January 2010 12:31

If you find yourself doing much WCF development, you are probably familiar with how tedious it can be to create all of the data contracts involved with your service.  To be specific, data contracts necessitate a lot of writing attributes.  Granted, it is now possible to use POCO objects and allow the WCF runtime to infer most of the data contract information from the public members of the given class.  However, I still prefer to be explicit and create the attributes.  On my current project, we also have some custom tooling that relies on the attributes.

Rather than handwriting all of the attributes, Resharper can save you quite a bit of time by leveraging the Live Template feature that it provides.  If you aren’t familiar with Live Templates, they work similarly to snippets in Visual Studio, but it has a lot more power.  On a current project, we are using a generic message dispatching pattern with WCF.  With this approach, there are only a few services in the entire system, but there are *lots* of data contracts.  In order to save some time, I created some Resharper Live Templates that I thought I would share in case others find them to be useful.

First, we start out by writing a class that we want to use as a data contract.

public class Address
    public string line1 { get; set; }
    public string line2 { get; set; }
    public string city { get; set; }
    public string state { get; set;}
    public string zip_code { get; set;}

Keep in mind that Visual Studio provides a default snippet for quickly creating automatic properties.  Rather than typing the entire information by hand, you can simply type “prop” and press either TAB or ENTER.  The snippet will generate most of the property for you and allow you to quickly fill in the name and type.

With the live templates, we can quickly annotate the class and properties to enable data contracts.  Start by positioning your cursor on the line above the class declaration and type “dc” then hit TAB.


The live template generates the DataContract attribute.  By default, it uses the same name as the class for the Name parameter. 


To generate the data members, you follow a similar process.  Position your cursor above the property you would like to annotate and type “dm” then hit TAB.


The live template generates the DataMember attribute.  By default, it uses the name of the property as the the value of the Name parameter in the attribute.


By only pressing a few keys, you can quickly annotate the entire class:

    Name = "Address",
    Namespace = "http://JeffBarnes.Samples")]
public class Address
    [DataMember(Name = "line1", IsRequired = true)]
    public string line1 { get; set; }

    [DataMember(Name = "line2", IsRequired = true)]
    public string line2 { get; set; }

    [DataMember(Name = "city", IsRequired = true)]
    public string city { get; set; }

    [DataMember(Name = "state", IsRequired = true)]
    public string state { get; set; }

    [DataMember(Name = "zip_code", IsRequired = true)]
    public string zip_code { get; set; }


This can save you quite a bit of typing and it is really only a teaser of the capabilities provided by the Resharper template engine.  If you want to give them a try, you download these two templates here.

To install them, go into the Resharper menu of Visual Studio and select the Live Templates menu item.  This will open the live templates window.  Simply click on the Import button and browse to the xml file containing the templates.  After you have loaded the files, you should be able to start using them in Visual Studio as previously outlined.

About Me

I'm a passionate software developer and advocate of the Microsoft .NET platform.  In my opinion, software development is a craft that necessitates a conscious effort to continually improve your skills rather than falling into the trap of complacency.  I was also awarded as a Microsoft MVP in Connected Systems in 2008, 2009, and 2010.

Can’t code withoutThe best C# & VB.NET refactoring plugin for Visual Studio
Follow jeff_barnes on Twitter

View Jeff Barnes's profile on LinkedIn


Shared Items


Anything you read or see on this site is solely based on my own thoughts.  The material on this site does not necessarily reflect the views of my employer or anyone else.  In other words, I don't speak for anyone other than myself.  So, don't assume I am the official spokesperson for anyone.