Last active
December 11, 2015 17:19
-
-
Save ewilde/4633849 to your computer and use it in GitHub Desktop.
#wcf service client wrapper based on http://web.archive.org/web/20110413044517/http://blog.tomasjansson.com/2010/12/disposible-wcf-client-wrapper/ put here for ease of use.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ServiceModel; | |
using System.ServiceModel.Channels; | |
/// <summary> | |
/// Wraps a service client so you can use "using" without worrying of getting | |
/// your business exception swallowed. Usage: | |
/// <example> | |
/// <code> | |
/// using(var serviceClient = new ServiceClientWrapper<IServiceContract>) | |
/// { | |
/// serviceClient.Channel.YourServiceCall(request); | |
/// } | |
/// </code> | |
/// </example> | |
/// </summary> | |
/// <typeparam name="TServiceType">The type of the service client type to create.</typeparam> | |
public class ServiceClientWrapper<TServiceType> : IDisposable | |
{ | |
/// <summary> | |
/// The channel factory | |
/// </summary> | |
private static ChannelFactory<TServiceType> channelFactory; | |
/// <summary> | |
/// The channel | |
/// </summary> | |
private readonly TServiceType channel; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ServiceClientWrapper{TServiceType}" /> class. | |
/// As a default the endpoint that is used is the one named after the contracts full name. | |
/// </summary> | |
public ServiceClientWrapper() : this(new Logger()) | |
{ | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ServiceClientWrapper{TServiceType}" /> class. | |
/// </summary> | |
/// <param name="logger">The logger.</param> | |
[StructureMap.DefaultConstructor] | |
public ServiceClientWrapper(ILogger logger) : this(typeof(TServiceType).FullName) | |
{ | |
this.Logger = logger; | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ServiceClientWrapper{TServiceType}" /> class. | |
/// </summary> | |
/// <param name="endpoint">The endpoint.</param> | |
public ServiceClientWrapper(string endpoint) | |
{ | |
if (channelFactory == null) | |
{ | |
channelFactory = new ChannelFactory<TServiceType>(endpoint); | |
} | |
this.channel = channelFactory.CreateChannel(); | |
((IChannel)this.channel).Open(); | |
} | |
/// <summary> | |
/// Gets or sets the logger. | |
/// </summary> | |
/// <value> | |
/// The logger. | |
/// </value> | |
public ILogger Logger { get; set; } | |
/// <summary> | |
/// Gets the channel. | |
/// </summary> | |
/// <value>The channel.</value> | |
public TServiceType Channel | |
{ | |
get { return this.channel; } | |
} | |
/// <summary> | |
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | |
/// </summary> | |
public void Dispose() | |
{ | |
try | |
{ | |
((IChannel)this.channel).Close(); | |
} | |
catch (CommunicationException) | |
{ | |
((IChannel)this.channel).Abort(); | |
} | |
catch (TimeoutException) | |
{ | |
((IChannel)this.channel).Abort(); | |
} | |
catch (Exception exception) | |
{ | |
((IChannel)this.channel).Abort(); | |
this.Logger.Error(exception); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment