Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Design the WCF Service without Implementing Interface

4.00/5 (5 votes)
31 Mar 2012CPOL1 min read 33K   418  
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)