Code Generation And WCF Inheritance






4.89/5 (6 votes)
Explain how to implement code generation and inheritance in a WCF service
Introduction
In this article I want to explain how I resolve a problem related to code generation for a WCF service.
Background
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>
[ServiceContract]
public interface ISampleGeneratedSrv
{
[OperationContract]
string HelloWorldGenerated();
}
/// <summary>
/// Interface for custom service
/// </summary>
[ServiceContract]
public interface ISampleSrv : ISampleGeneratedSrv
{
[OperationContract]
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" /> </service>
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.
History
10 April 2014