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.
interfaces defines the contract for the WCF service. The
the interface for the generated class, the
ISampleSrv is the interface for the
custom class that inherits from the generated one.
public interface ISampleGeneratedSrv
public interface ISampleSrv : ISampleGeneratedSrv
The classes representing
the WCF service are shown in the following code.
public class SampleGeneratedSrv:ISampleGeneratedSrv
public string HelloWorldGenerated()
return "Hello from generated WCF Service";
public partial class SampleSrv : SampleGeneratedSrv
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirements Mode.Allowed)]
public partial class SampleSrv : ISampleSrv
public string HelloWorldCustom()
return "Hello from custom WCF Service";
SampleGeneratedSrv is the base class where we put all the generated code implementing
ISampleGeneratedSrv which is also generated too.
class inherits from the
SampleGeneratedSrv and implements the
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.
Web.config file I set the contract for the
SampleSrv as follow:
<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