Click here to Skip to main content
13,452,991 members (55,354 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 10 Apr 2014

Code Generation And WCF Inheritance

, 10 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Explain how to implement code generation and inheritance in a WCF service


In this article I want to explain how I resolve a problem related to code generation for a WCF service.


In my company we have a tool, implemented using DSL Tools, allowing us to generate code from a business model. By this tool we also generate all the WCF services for our architecture. We generate a base class were we put all generated code, and then we generate a partial class, inheriting from the base class, were we put the custom code or were we override some methods of the base class.

Using the code

First of all I started defining the interface of the WCF service. Since we have two classes (the generated one and the custom one) that represent a single WCF service, I define two different interfaces for each kind of class of the WCF service.

The following interfaces defines the contract for the WCF service. The ISampleGeneratedSrv is the interface for the generated class, the ISampleSrv is the interface for the custom class that inherits from the generated one.

/// <summary>
/// Interface for auto-generated service
/// </summary>
public interface ISampleGeneratedSrv
    string HelloWorldGenerated();
/// <summary>
/// Interface for custom service
/// </summary>
public interface ISampleSrv : ISampleGeneratedSrv
    string HelloWorldCustom();

The classes representing the WCF service are shown in the following code.

/// <summary>
/// Generated WCF service
/// </summary>
public class SampleGeneratedSrv:ISampleGeneratedSrv
    public string HelloWorldGenerated()
        return "Hello from generated WCF Service";

/// <summary>
/// WCF custom service that inherit from generated
/// </summary>
public partial class SampleSrv : SampleGeneratedSrv
{ }

/// <summary>
/// Partial of custom code of SampleSrv
/// </summary>
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirements        Mode.Allowed)]
public partial class SampleSrv : ISampleSrv
    public string HelloWorldCustom()
        return "Hello from custom WCF Service";

The SampleGeneratedSrv is the base class where we put all the generated code implementing the ISampleGeneratedSrv which is also generated too.

The SampleSrv class inherits from the SampleGeneratedSrv and implements the ISampleSrv interface. In this class and in the related interface we put all the custom code that we can’t generate (some customizations that are not part of the original model for the generation). We can also override an existing method in the parent class.

In the Web.config file I set the contract for the SampleSrv as follow:

<service name="WCFInheritance.Web.WCFServices.SampleSrv">
        <endpoint address="" binding="customBinding" bindingConfiguration="DefaultContent" contract="WCFInheritance.Web.WCFServices.ISampleSrv" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

Points of Interest

In this article we saw how implement inheritance in a WCF service. This can be useful when you have a tool that automatically generates the WCF services and you want to add new method or modify the logic of an existing generated one.


10 April 2014


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


About the Author

Mirko Rezzin
Software Developer (Senior)
Italy Italy
I am a software developer working with Microsoft technologies since 2002.

Skills include C/C++/C#, MS SQL Server, ASP.NET, WPF, WCF, Winforms, MVC.

You may also be interested in...


Comments and Discussions

QuestionMy vote of 5 Pin
mdesavi10-Apr-14 5:15
membermdesavi10-Apr-14 5:15 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180318.3 | Last Updated 10 Apr 2014
Article Copyright 2014 by Mirko Rezzin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid