Click here to Skip to main content
Click here to Skip to main content
Go to top

Design the WCF Service without Implementing Interface

, 31 Mar 2012
Rate this:
Please Sign up or sign in to vote.
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.

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 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)

Share

About the Author

Umesh Khandelwal
Wipro
India India
Working in Microsoft Technologies.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 89431767-May-12 23:59 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 1 Apr 2012
Article Copyright 2012 by Umesh Khandelwal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid