I had the duty to regain control of an old application which was a MVC 4 website app. With around 15 different WCF services but each coupled with a Windows Service as clients, it was a bit messy. Each WCF couple (server and client) had a different responsibility but always with an identical functionality to allow the client to warn the server that the process has started and stopped, plus the result (success or fail). And while spending time to fix some bugs, something was keeping whispering to me you should dig on how to not repeat WCF contracts similar everywhere
You have understood it. There was no common code to perform these operations, with the consequence that the implementation was never 100% the same. And if you would have change something, you have to impact the change everywhere.
Tested the first improved thing that comes to mind
The first thing that comes to mind is to create methods to warn the server app that the client has started, succeeded or failed and finally ended.
Obviously, it works. But you still have to write the implementation on the server side for each service on the server side. Even if it simply consist of a call to a method written in an external project.
Mind WCF is based on service contracts !
Keep in mind two things:
- WCF is based on service contracts
- A C# class can implement multiple interfaces (a.k.a. contracts).
We’re now able to not repeat WCF contracts each time
In an external project, we can then create an abstract class to implement common functionalities, and a contract declaring these common functionalities.
All we have to do then is to inherit our abstract class …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
namespace WcfServiceBase { public abstract class AbstractServiceBase : IHealthCheckService, INotificationService { public bool IsServiceHealthy() => true; public void SetDashboard(EEventType eventType, string comment, EProcessType processType) { // Implement your behavior } } } |
… and contracts and we’re done !
1 2 3 4 5 6 7 8 9 10 |
namespace WcfServiceBase.Interface { public interface IHealthCheckService { [OperationContract] bool IsServiceHealthy(); } } |
1 2 3 4 5 6 7 8 9 10 11 |
namespace Common.WcfServiceContracts { [ServiceContract] public interface IDashboardService { [OperationContract] void SetDashboard(string apbNumber, EDashboardEvent eventType, string comment, DateTime clientTime, EProcessType processType); } } |
Mind-blowing the first WCF optimal implementation
We’re now ready to implement our first WCF Service with the common feature. And yes, we’ll avoid WCF contract repetition (and implementation or download the full project).
1 2 3 4 5 6 7 8 9 10 11 12 |
namespace ClosingDebts { public class MyService : AbstractServiceBase, MyService { public List<TypedObject> Get() { // Implement your behavior } } } |
You may have notice that we do not need to implement our common contracts. As it is done in our base abstract class, it is totally useless.