Click here to Skip to main content
15,878,852 members
Articles / Programming Languages / C#
Tip/Trick

Design the WCF Service without Implementing Interface

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
31 Mar 2012CPOL1 min read 32.4K   418   10   6
Design the WCF service without implementing interface

Introduction

I will demonstrate how to create the WCF service without using interface. The general process to create the WCF service is to create interface and implement it into the class. This is best practice which is defined in the SOA model but similar to Web Service, we can create the WCF without implementing the interface.

In this WCF demonstration, you will see that from the code and implementation perspective, both web service and WCF are similar except WCF has the entire configuration detail in web.config which has almost complete control on runtime characteristics of a service without forcing a change in code.

Using the Code

In this demonstration, I use only one class and define WCF method inside this class. Also, instead of Interface name specifying the Class name in config file end point, I show the Service1.cs file code which has Service1 class and DisplayName WCF method.

C#
using System;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace WCFServiceWithOutUsingInterface
{
    // NOTE: Not implementing the Interface and using interface attribute directly to class
    [ServiceContract ]
    public class Service1 
    {
        [OperationContract]
        public string DisplayName(String value)
        {
            return string.Format("You Name is : {0}", value);
        }        
    }
}

I am showing the Web.config file where instead of Interface name, I specify the Class name as Contract name in the end point .

XML
<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name ="WCFServiceWithOutUsingInterface"  >
        <endpoint contract ="Service1" binding="wsHttpBinding"></endpoint> 
      </service>
      </services> 
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
      </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

Note about the Attached Code

WCFServiceWithOutUsingInterface.zip contains the WCFServiceWithOutUsingInterface.sln which has WCF service and consumes WCF service projects.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United States United States
Working on Microsoft Technologies and Cloud computing.

Comments and Discussions

 
GeneralMy vote of 1 Pin
stooboo9-Aug-12 10:48
stooboo9-Aug-12 10:48 
GeneralRe: My vote of 1 Pin
Umesh Khandelwal10-Aug-12 3:35
professionalUmesh Khandelwal10-Aug-12 3:35 
GeneralMy vote of 5 Pin
Member 89431767-May-12 23:59
Member 89431767-May-12 23:59 
Questionmy vote 5 Pin
beqiraj2542-Apr-12 3:07
beqiraj2542-Apr-12 3:07 
Generalis there any benefit of creating a WCF service without using Interface? Pin
nit_singh1-Apr-12 1:46
nit_singh1-Apr-12 1:46 
GeneralRe: is there any benefit of creating a WCF service without using Interface? Pin
Umesh Khandelwal1-Apr-12 1:53
professionalUmesh Khandelwal1-Apr-12 1:53 

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.