Skip to content

Instantly share code, notes, and snippets.

@RobinHerbots
Created October 24, 2011 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobinHerbots/1308911 to your computer and use it in GitHub Desktop.
Save RobinHerbots/1308911 to your computer and use it in GitHub Desktop.
Simple WCF Proxybase with a static ChannelFactory
using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace ServiceProxies
{
public class ProxyBase<TChannel> : IDisposable where TChannel : class
{
private static ChannelFactory<TChannel> _channelFactory;
private TChannel _clientChannel;
public ProxyBase()
{
if (_channelFactory == null) _channelFactory = new ChannelFactory<TChannel>("*", null);
_clientChannel = _channelFactory.CreateChannel();
}
protected TChannel Channel
{
get
{
((IClientChannel)_clientChannel).Open();
return _clientChannel;
}
}
public void Dispose()
{
var channel = _clientChannel as IClientChannel;
switch (channel.State)
{
case CommunicationState.Closed:
case CommunicationState.Closing:
break;
case CommunicationState.Opened:
try
{
channel.Close(); //gracefully close
}
catch (Exception)
{
channel.Abort(); //on exception kill the connection
}
break;
default:
channel.Abort();
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment