Configuration based activation of WCF service – without SVC files

Prior to .net 4.0, while creating and hosting a WCF service it was mandatory to provide a .svc file. This .svc file is a text file which contains name of the service and other required information for ServiceHost class.

Microsoft introduced a config based activation from .net 4.0. i.e you can configure it from web.config file or application files if hosted in other applications. This feature lets you register your service under a relative virtual path. You have to also specify the name of the class which would be served as a service using the relative virtual path.

Advantage of config based activation is that you don’t have to maintain .svc files separately. Configuration to be done is minimal. This does not make much difference for small projects but it surely makes a significant impact on large and scalable projects wherein you have to deal with hundreds of services.

Lets us create a project to demonstrate this concept.

Create a WCF Service Application project. Name it as ConfigBasedService.

Delete Service1.svc and IService1.cs files from project.

Add a new Interface IMessageListenerService.cs as follows

namespace ConfigBasedService
{
    public interface IMessageListenerService
    {
        bool ReceiveMessage(string message);
    }
}

Add a new class MessageListenerService.cs and implement IMessageListenerService. Also decorate MessageListenerService class with ServiceContract attribute and methods with OperationContract attribute

namespace ConfigBasedService
{
    [ServiceContract]
    public class MessageListenerService : IMessageListenerService
    {
        [OperationContract]
        public bool ReceiveMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
                return false;

            return true;
        }
    }
}

Since we are going to activate our service using configuration to get rid of the .svc files, we have to add activation related information in configuration file.
We will add serviceHostingEnvironment element in system.serviceModel.

Now comes the interesting part. configuration for all the services which are to be hosted are to be added in serviceActivations element. For our MessageListenerService we will have to add a configuration as shown below.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
    <serviceActivations>
       <add relativeAddress="MessageListenerService.svc" service="ConfigBasedService.MessageListenerService"/>
    </serviceActivations>
</serviceHostingEnvironment>

In above code relative address specified is relative to the root application. Service attribute value denotes the class of the service.

Our service is ready. You can host the service on IIS and test it. You can also debug the service and concatenate the URL in browser with relative address to see the running service. This service can be tested using wcftestclient by adding the concatenated path.

Below is the result of successful invocation of MessageListenerService from WcfTestClient

WcfTestClient

WcfTestClient


Entire code sample can be downloaded from Git

Enjoy Coding!!!!!!

About sagar

With over 13 years of professional experience as a .NET developer, I have honed my expertise in various facets of the .NET framework and related technologies. My extensive background includes proficiency in WPF, Web APIs, WCF services, and Windows services, which has enabled me to deliver robust and scalable applications across different domains.

Leave a Reply

Your email address will not be published. Required fields are marked *