Created
October 24, 2011 12:28
-
-
Save RobinHerbots/1308911 to your computer and use it in GitHub Desktop.
Simple WCF Proxybase with a static ChannelFactory
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.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