0

I have a question regarding use of interfaces when unmanaged resources come to play. Suppose I have a web service and generated WCF client. Service contract looks like this:

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    string GetData(int value);
}

On client side I use dependency injection and bind ITestService interface to TestServiceClient (generated with svcutil). However, when I create ITestService and it really is TestServiceClient it should be disposed in correct way but the clients don't know about it. How do you deal with this problem?

I thought about generating proxy classes like this:

class TestServiceClientProxy : ITestService
{
    #region ITestService Members

    public string GetData(int value)
    {
        var client = new TestServiceClient();
        bool success = false;
        try
        {
            var result = client.GetData(value);
            client.Close();
            success = true;
            return result;
        }
        finally
        {
            if (!success)
            {
                client.Abort();
            }
        }
    }

    #endregion
}

However, I don't think code generation would be best way to go. Should I use some AOP framework or DynamicProxy?

Thanks in advance for help.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
empi
  • 15,755
  • 8
  • 62
  • 78
  • Take a look at this answer: http://stackoverflow.com/questions/3010820/dependency-injection-wcf/3011473#3011473 – Mark Seemann Jun 26 '10 at 15:09
  • What is the benefit of binding a factory instead of a proxy interface? You mean that by using factory you can put the proxy in using block, right? My problem is that wcf proxies shouldn't be used in using blocks: http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue and I cannot force all my clients to rember to write proper code. – empi Jun 26 '10 at 15:22
  • You can always Decorate the real implementation with an IDisposable wrapper that properly closes the client in its Dispose method. This would allow you to have the proxy interface in a using block and still close it safely. – Mark Seemann Jun 26 '10 at 15:52

1 Answers1

0

Declare that your interface implements IDisposable, implement Dispose() in the concrete class, and your DI provider will dispose it properly. If your DI provider doesn't do this, find a new one.

[ServiceContract]
public interface ITestService : IDisposable
{
    [OperationContract]
    string GetData(int value);
}
Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • @Josh Einstein: This is not about services but about clients – empi Jun 26 '10 at 14:57
  • @Jame Ide: Adding IDisposable to my interface definition forces users of my service to remember to release it through DI container. What is more, if I want to create service mock I would need to define Dispose that is completely unnecessary. – empi Jun 26 '10 at 15:01
  • @empi, My mistake you are correct. I interpreted the question to be how to "dipose" the server side of the service via a client's implementation of IDisposable. – Josh Jun 26 '10 at 21:52