Is this a Appropriate method to safely close a proxy on WCF-calls?
In my application i discovered that sometimes the Close() for a
WCF-call/channels trowed errors. And i did a little research on the
subject and borrowed some code on the internet to get me started.
And now, I wonder is this the right way to go? or should i improve the
solution or maybe implement something totally different?
Generic-Class/Static Class:
public class SafeProxy<Service> : IDisposable where Service :
ICommunicationObject
{
private readonly Service _proxy;
public SafeProxy(Service s)
{
_proxy = s;
}
public Service Proxy
{
get { return _proxy; }
}
public void Dispose()
{
if (_proxy != null)
_proxy.SafeClose();
}
}
public static class Safeclose
{
public static void SafeClose(this ICommunicationObject proxy)
{
try
{
proxy.Close();
}
catch
{
proxy.Abort();
}
}
}
This is how i make calls to the WCF:
(The WCFReference is a Service Reference pointing to the WCF service adress)
using (var Client = new SafeProxy<WCFReference.ServiceClient>(new
WCFReference.ServiceClient()))
{
Client.Proxy.Operation(info);
}
No comments:
Post a Comment