Saturday, August 29, 2015

WCF Service without .svc file or Configuration Based Activation in WCF


Normally when we create a wcf service, we have the .svc file added into it. As per MSDN, the reason we have the .svc file is:

"In .NET Framework 3.5, a .svc file was required for activating a service. This caused additional management overhead, because an additional file was required to be deployed and maintained along with the application. With the release of .NET Framework version 4, the activation components can be configured using the application configuration file."


As the link says, with .Net framework 4.0 and above, we do not need the .svc file in wcf and still create the service. So the required service activation can be done using some configuration settings in the config files. This is through the use of <serviceActivations> tag in the configuration file. Let's see how we can do this:


So we create this sample with a simply ClassLibrary project type. We call it as WCFConfigBasedActivation. Add reference to the System.ServiceModel.dll from the Assemblies. Next, we add a interface named IServiceContract with a method which takes 2 integer parameters as input and returns their sum. Add the ServiceContract and OperationContract on the class and the method respectively. You can read about the contracts in wcf here.


    [ServiceContract]
    public interface IServiceContract
    {
        [OperationContract]
        Int32 GetSum(Int32 a, Int32 b);
    }

Add a new class named Service.cs, which implements this interface. So our implementation will look like:


    public class Service : IServiceContract
    {
          Int32 IServiceContract.GetSum(Int32 a, Int32 b)
          {
               return a + b;
          }
    } 

Next, add an empty asp.net application within the same solution. This application is be to used as a host for the service. So add the reference to the service library project that we created in the start of discussion. So our solution will look like the following:




Now, without adding any page, run the application and append Service.svc to the browser url and see if you can view the service.





You can't. It says 404 error, as the service was not activated by the host (due to missing .svc file). Now let's go to host configuration file and add the settings which can activate the service. So we add the following settings:


 <system.serviceModel>
    <serviceHostingEnvironment >
      <serviceActivations>
        <add factory="System.ServiceModel.Activation.ServiceHostFactory" relativeAddress="Service.svc" service="WCFConfigBasedActivation.Service" />
      </serviceActivations>
    </serviceHostingEnvironment>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>


Now run the application again and add the Service.svc to the browser url and see. Bingo, this time it works.




Easy to implement isn't it. 
We could have used the WCF Service Library project template also for creating the service project rather then the typical ClassLibrary project template.

Hope you enjoyed reading it. Happy coding...!!!

No comments:

Post a Comment