WCF .svc item templates for ASP.NET hosted apps
Posted on Wednesday, June 11 2008 by steve maine
I recently discovered a new (to me, at least) feature in Visual Studio called "Export Template". This guy lives in the VS File menu and makes it really easy to spit out VS project/item templates that you can use to create new apps. Very cool, and very easy.
One thing that's bugged me about the default WCF templates that ship in the box is that sometimes, the templates are too rich for what I want to do. Specifically, we don't ship an item template for an "empty .svc file" that doesn't have any configuration and doesn't spit out any code behind. I find myself wishing for this when I'm writing quick-and-dirty ASP.NET apps that host WCF services.
So last night I took my newfound knowledge of VS's "Export Template" feature to build me a few new templates that were more geared for what I personally wanted, and I thought I'd share.
The first one defines a custom System.ServiceModel.Activation.ServiceHostFactory inline in the .svc file, which lets me set up bindings imperatively:
<%@ ServiceHost Language="C#" Debug="true" Service="Service" Factory="CustomFactory" %> using System.ServiceModel; using System.ServiceModel.Activation; public class CustomFactory : ServiceHostFactory { protected override ServiceHost CreateServiceHost(System.Type serviceType, System.Uri[] baseAddresses) { var host = base.CreateServiceHost(serviceType, baseAddresses); host.AddServiceEndpoint(typeof(Service), new BasicHttpBinding(), ""); return host; } } [ServiceContract] public class Service { [OperationContract] public void HelloWorld() { } }
If I'm being a bit more disciplined and externalizing bindings and such in config like a Good Developer, I can get an "empty" .svc file using the InlineService template. This one will throw an exception unless you explicitly add at least one endpoint in configuration yourself:
<%@ ServiceHost Language="C#" Debug="true" Service="Service" %> using System.ServiceModel; //Don't forget to add at least one endpoint in configuration! [ServiceContract] public class Service { [OperationContract] public string Get() { return "Hello, world!"; } }
And finally, here's one that uses the System.ServiceModel.Web.WebServiceHostFactory to bootstrap my development of REST services. This one doesn't need any configuration at all, thanks to WebServiceHostFactory:
<%@ ServiceHost Language="C#" Debug="true" Service="RestService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %> using System.ServiceModel; using System.ServiceModel.Web; [ServiceContract] public class RestService { [OperationContract] [WebGet( UriTemplate="*" )] public string Get() { return "Hello, world!"; } }
If you like, you can download these templates for your own use here:
CustomServiceHost_CS.zip
CustomServiceHost_VB.zip
InlineService_CS.zip
InlineService_VB.zip
RESTService_CS.zip
RESTService_VB.zip
To install, just copy the .zip file to your <user>\Documents\Visual Studio 2008\Item Templates directory (no need to unzip, It Just Works):
Once that's done, you should see them when you do "Add New Item" to an ASP.NET Web Site or ASP.NET Web Application Project in Visual Studio. You can see them hanging out in the bottom of this dialog:
If you find these useful, let me know. Especially if you have interesting ideas about Item Templates/Project Templates relating to WCF development that you'd like to see in the box...
