EDanaII
EDanaII

Reputation: 199

Service that returns an object with IDisposable?

OK, so... I have this project that I'm working on that is trying to implement a WCF (.SVC) service to return an object with IDisposible. We use it something like this:

FactoryService client = new FactoryService();

LibraryFactory factory = client.CreateLibraryFactory("Hello Library");

using (IErrorWatcher watcher = client.CreateErrorWatcher(factory, "This is a test, this is only a test...")) 
{
    try 
    {
        int a = 0; int b = 1;
        int c = b / a; //Force errror
    }
    catch (Exception ex) 
    {
        watcher.SetError(ex);
    }
}

We're doing this so that the Dispose method can be used to trigger logging. Problem is, we're getting this error: ErrorWatcher: type used in a using statement must be implicitly convertible to 'System.IDisposable'

It IS implementing IDisposible, so I assume this is a problem with the service. Are there any Ts we need to dot or I's we need to cross, or is this even doable? ` FactoryService code looks like this: using Cloud.Library;

[ServiceContract]
public interface FactoryService {
    [OperationContract]
    LibraryFactory CreateLibraryFactory(string applicationId);

    [OperationContract]
    ErrorWatcher CreateErrorWatcher(LibraryFactory factory, string eventURI);
}

`

Any and all clues appreciated.

Upvotes: 0

Views: 456

Answers (2)

Marex
Marex

Reputation: 1

Soap service client inherits from System.ServiceModel.ClientBase:

public partial class MySoapClient : System.ServiceModel.ClientBase<MySoap>, MySoap {..}

ClientBase has implemented IDisposable interface:

public abstract class ClientBase<TChannel> : ICommunicationObject, IDisposable where TChannel : class

So it looks it should be disposed by a programmer. Is it really needed? I dont know. I just did:

MySoap my_soap = new MySoapClient(binding, endpoint);
using (my_soap as IDisposable)
{
...
}

Upvotes: 0

Wiktor Zychla
Wiktor Zychla

Reputation: 48250

I bet your class implements IDisposable at the server. When you create a proxy of it to use the proxy at the client side, the proxy will reflect all the methods but no interfaces. Technically it means that despite your class being disposable on the server, the proxy at the client side doesn't implement this interface by default. You could add the interface to the proxy manually for the client side.

What could be important however, is that in case of disposable objects, calling Dispose on the client to make it call Dispose on the server counterpart often doesn't make much sense as the lifetime of the server side object is restricted to a single method call anyway.

This means that when you call Dispose at the client side, the server creates a new instance of the server side object for the lifetime of the new call and calls Dispose on it.

Upvotes: 1

Related Questions