How to host WCF services in ASP.NET applications without bloating your web.config

This post is more than 13 years old.

Posted at 08:00 on 09 December 2010

If you add a WCF service to a web project in Visual Studio, it will dump a whole lot of garbage in your web.config file to make it work. If you want to keep your web.config file slim and clean (and you should), there is an alternative. You can of course create the services programmatically in a console application, but how do you do it in a web application?

Simply open the .svc file itself (not the .svc.cs codebehind file) in a text editor (you can right-click on the file and choose "Edit markup" on the context menu), and add the following attribute to the <%@ServiceHost %> tag:

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

This will give you something like this:

<%@ ServiceHost Language="C#" Debug="true" Service="My.Wcf.Service" CodeBehind="Service.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

You can then remove all the extraneous cruft that Visual Studio adds to the <system.serviceModel> section in your web.config, thereby keeping it cleaner and more manageable.