WCF Client Generic Base Class

Posted: 26th February 2010 in WCF

Further to my previous blog entry about the WCF 10 connection limit and its inability to implement the IDisposable interface or allow the use of the “using block” to free up the connections, I decided to write a generic base class.

This base class does not close the connection automatically,nor does it dispose of the connection, but it does allow me to create and use a service client a lot easier.

  1. public class BaseServiceConnector<T> where T : ICommunicationObject, new()
  2. {
  3. private T serviceClient = new T();
  4. /// <summary>
  5. /// Instance variable to access the Cache from the enterprise library
  6. /// </summary>
  7. private static readonly ICacheManager cacheManager = CacheFactory.GetCacheManager();
  8. /// <summary>
  9. /// Gets the service client.
  10. /// </summary>
  11. /// <value>The service client.</value>
  12. protected T ServiceClient
  13. {
  14. get
  15. {
  16. try
  17. {
  18. if (this.serviceClient.Equals(default(T)) || this.serviceClient.State == CommunicationState.Closed)
  19. {
  20. this.serviceClient = new T();
  21. }
  22. this.ReEstablishProxyIfNecessary();
  23. }
  24. catch (FaultException)
  25. {
  26. this.ReEstablishProxyIfNecessary();
  27. }
  28. catch (Exception)
  29. {
  30. this.ReEstablishProxyIfNecessary();
  31. }
  32. return this.serviceClient;
  33. }
  34. }
  35. /// <summary>
  36. /// Instance variable to access the Cache from the enterprise library
  37. /// </summary>
  38. public ICacheManager CacheManager
  39. {
  40. get { return cacheManager; }
  41. }
  42. /// <summary>
  43. /// Re-establish proxy if necessary.
  44. /// </summary>
  45. private void ReEstablishProxyIfNecessary()
  46. {
  47. if (this.serviceClient.State != CommunicationState.Faulted)
  48. {
  49. return;
  50. }
  51. this.serviceClient.Abort();
  52. this.serviceClient = new T();
  53. }
  54. }

Now I can create service  clients and reuse the code all I have to do is inherit from it and pass in the proxy client instance name.

  1. public class LookupServiceCache : BaseServiceConnector<LookupServiceClient>, ILookupServiceCache
  2. {
  3. #region Lookups
  4. /// <summary>
  5. /// Gets the towns.
  6. /// </summary>
  7. /// <returns>The list from the cache if it exists; or from the database</returns>
  8. public IList<TownDto> GetTowns()
  9. {
  10. var cacheKey = LookupDataType.Towns.ToString();
  11. if (!this.CacheManager.Contains(cacheKey))
  12. {
  13. var towns = this.ServiceClient.GetTowns();
  14. this.CacheManager.Add(
  15. cacheKey,
  16. towns,
  17. CacheItemPriority.Normal,
  18. null,
  19. new SlidingTime(TimeSpan.FromMinutes(Constants.CacheTimeout)));
  20. this.ServiceClient.Close();
  21. }
  22. return (IList<TownDto>)this.CacheManager.GetData(cacheKey);
  23. }
  24. }

I’ve found this useful for creating a business logic layer above the service for purpose of caching, validation, etc.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s