I saw some duplication of code, which could easily be centralized and eliminated. I also renamed a few pieces as per Microsoft Framework Design Guidelines. Finally, you may also want to look at this bit of code this bit of code which seems somewhat similar and caches the service proxy rather than recreating it for each call.
I saw some duplication of code, which could easily be centralized and eliminated. I also renamed a few pieces as per Microsoft Framework Design Guidelines. Finally, you may also want to look at this bit of code which seems somewhat similar and caches the service proxy rather than recreating it for each call.
I saw some duplication of code, which could easily be centralized and eliminated. I also renamed a few pieces as per Microsoft Framework Design Guidelines. Finally, you may also want to look at this bit of code which seems somewhat similar and caches the service proxy rather than recreating it for each call.
I saw some duplication of code, which could easily be centralized and eliminated. I also renamed a few pieces as per Microsoft Framework Design Guidelines. Finally, you may also want to look at this bit of code which seems somewhat similar and caches the service proxy rather than recreating it for each call.
namespace System.ServiceModel
{
public class WcfClientWrapper<TProxy> : IDisposable where TProxy : class
{
private string endpointAddress;
private TProxy service;
public WcfClientWrapper(string endpointAddress)
{
this.service = this.CreateChannel(endpointAddress);
var serviceChannel = this.service as IClientChannel;
if (serviceChannel == null)
{
return;
}
serviceChannel.Faulted += this.ClientChannelFaulted;
}
~WcfClientWrapper()
{
this.Dispose(false);
}
public TProxy Service
{
get
{
return this.service;
}
}
public void Dispose()
{
this.Dispose(true);
}
public void Dispose(bool disposing)
{
if (disposing)
{
// get rid of managed resources
}
this.DisposeService();
GC.SuppressFinalize(this);
}
private TProxy CreateChannel(string newEndpointAddress)
{
this.endpointAddress = newEndpointAddress;
var factory = new ChannelFactory<TProxy>(new NetTcpBinding(), new EndpointAddress(newEndpointAddress));
return factory.CreateChannel();
}
private void ClientChannelFaulted(object sender, EventArgs e)
{
this.DisposeService();
this.service = this.CreateChannel(this.endpointAddress);
}
private void DisposeService()
{
var serviceChannel = this.service as IClientChannel;
if (serviceChannel == null)
{
return;
}
var success = false;
try
{
serviceChannel.Faulted -= this.ClientChannelFaulted;
if (serviceChannel.State != CommunicationState.Faulted)
{
serviceChannel.Close();
success = true;
}
}
finally
{
if (!success)
{
serviceChannel.Abort();
}
}
}
}
}