Creating WCF service without SVC file

Recently I came across WCF service without SVC file and was wondering how to achieve this. This post teaches us how to create SVC less WCF service.

.SVC files are used to host WCF service in IIS.

Creating WCF service without SVC file

  • Create blank solution with name “LearnWCF” with appropriate location.
  • Add class library project with name “WelcomeService”.

We are creating class library to understand service interface implementation better.

  • Add DLL reference “System.ServiceModel”, this provides classes related to the service model. We are one step closer making class library to WCF service.
  • Add an interface file “IWelcomeService.cs” to the project. Include “System.ServiceModel” namespace section and decorate the interface with [ServiceContract] attribute.
  • Add method “GreetWithMessage” decorating with [OperationContract] attribute. It accepts a string parameter “name” returning welcome message.

Service-Operational-Contracts

IWelcomeService Interface with Service & Operational Contracts

  • Create a class file “WelcomeService.cs” which will implement the IWelcomeService interface and only returns formatted string. Here’s code snippet in WelcomeService.cs
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace WelcomeService
    {
        public class WelcomeService : IWelcomeService
        {
            public string GreetWithMessage(string name)
            {
                return name + " Welcome to mithunvp.com !!";
            }
        }
    }

ASP.NET Empty web site as hosting application for WCF

  • Add ASP.NET Empty web site to same solution with name “WelcomeServiceHost”. Add Reference to the class library “WelcomeService” created above. CodeProject

  • Let’s DEBUG this website to see that happens. This is because we don’t have hosting file in this empty web site and of course its not configured to show directory structure.

ASP.NET hosting WCF doesn’t contain SVC file. This is the main agenda of the article - Creating WCF service without SVC file. Following image shows that is main setting that enables to access the WCF service.

web.config for SVC less WCF

  • Update the ASP.NET site’s web.config with below settings to make WCF service to work without SVC

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <configuration>
    <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    </system.web>
    <system.serviceModel>
    <serviceHostingEnvironment >
    <serviceActivations>
    <add factory="System.ServiceModel.Activation.ServiceHostFactory"
    relativeAddress="./WelcomeServiceHost/WelcomeService.svc"
    service="WelcomeService.WelcomeService" />
    </serviceActivations>
    </serviceHostingEnvironment>
    <behaviors>
    <serviceBehaviors>
    <behavior>
    <serviceMetadata httpGetEnabled="true"/>
    </behavior>
    </serviceBehaviors>
    </behaviors>
    </system.serviceModel>
    </configuration>
  • DEBUG or RUN Empty website to verify WCF is service ready to be consumed.

WCF

WCF service ready to be consumed with SVC link.

System.ServiceModel’s ServiceActivations is responsible for creating WCF service without SVC file.