Click here to Skip to main content
15,891,513 members
Articles / Programming Languages / C#

Creating WCF Service without SVC File

Rate me:
Please Sign up or sign in to vote.
4.13/5 (11 votes)
2 Oct 2013CC (ASA 3U)2 min read 42.3K   13   2
How to create a 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.

Software Prerequisites

Operating SystemWindows 7
IDEVisual Studio 2012
Language C#
Web ServerIIS/ IIS Express
Level Basic knowledge of C# and Visual Studio 2012/ 2010
Also works on Windows Server 2008 R2, Visual Studio 2010, IIS 7

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 the code snippet in WelcomeService.cs:
    C#
    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 the same solution with name "WelcomeServiceHost". Add Reference to the class library "WelcomeService" created above.
  • 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 it's not configured to show directory structure.

    ASP.NET host without SVC file

    ASP.NET hosting WCF doesn’t contain SVC file

    This is the main agenda of the article – Creating WCF service without SVC file. The following image shows that <ServiceActivations> is the main setting that enables to access the WCF service.

    web.config for SVC less WCF

    System.ServiceModel’s settings in Web.config to activate SVC file
  • Update the ASP.NET site’s web.config with the below settings to make WCF service work without SVC:
    XML
    <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 service ready with SVC

    WCF service ready to be consumed with SVC link

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

The post Creating WCF service without SVC file appeared first on Mithunvp.com.

This article was originally posted at http://www.mithunvp.com/creating-wcf-service-without-svc

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License


Written By
India India
I am Mithun Pattankar, a Software Professional on day job and blogger at nights. My interests are in Microsoft Technologies like DotNet, Visual Studio, ASP.NET, WPF, WCF, C#, HTML5

URL: www.mithunvp.com

Comments and Discussions

 
QuestionHow to do this from a Windows Service ? Pin
stixoffire19-Nov-15 9:39
stixoffire19-Nov-15 9:39 
Can you do this same thing from a Windows Service ?
Question[My vote of 2] To short Pin
AbdullaMohammad18-Nov-14 16:04
AbdullaMohammad18-Nov-14 16:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.