Do not repeat WCF contracts all over your services

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:

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 …

… and contracts and we’re done !

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

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.

Share this !
Was this article helpful?
YesNo

Leave a Reply

Your email address will not be published. Required fields are marked *