WCF: is it safe to override the Client Dispose method using a partial class?

I would like to override the Dispose method of the generated proxy server ( ClientBase) due to the fact that deleting the proxy server causes Close and may throw an exception when the channel fails.

The only way I have come is to create a partial class for my generated proxy, make it inherit from IDisposable:

 public partial class MyServiceProxy : IDisposable
    {
        #region IDisposable Members

        public void Dispose()
        {
            if (State != System.ServiceModel.CommunicationState.Faulted)
                Close();
            else
                Abort();
        }

        #endregion
    }

I did some tests and my method is Disposereally called.

Do you see any problems with this strategy?

Also, I don't like the fact that I will have to create this partial class for each generated proxy.

Well, if I could inherit my proxy from the base class ...

+3
2

. , , , , IDisposable ClientBase.

WCF - , , , IDisposable . if/else , Dispose.

, , Service Proxy Helper, .

:

public static class Service<TProxy>
    where TProxy : ICommunicationObject, IDisposable, new()
{
    public static void Using(Action<TProxy> action)
    {
        TProxy proxy = new TProxy();
        bool success = false;
        try
        {
            action(proxy);
            proxy.Close();
            success = true;
        }
        finally
        {
            if (!success)
            {
                proxy.Abort();
            }
        }
    }
}

:

Service<MyServiceClient>.Using(svc => svc.PerformOperation());

:

Service<MyServiceClient>.Using(svc =>
{
    var result = svc.PerformOperation();
    ProcessResult(result);
});

. WCF , , ( ). , .

+2

Dispose() - .

: ` WC ` WCF?

+1

Source: https://habr.com/ru/post/1740393/


All Articles