denver
denver

Reputation: 3135

Does WCF call dispose on transmitted and received objects?

We have a contract passing a large object using streaming. The service and message contracts boil down to something like this.

[ServiceContract]
public interface IData
{
    [OperationContract]
    Item Get(ItemRequest request);

    [OperationContract]
    void Put(Item request);
}

[MessageContract]
public class Item: IDisposable
{
    [MessageBodyMember(Order = 1)]
    public Stream FileByteStream;

    public void Dispose() {...}
}

The Item class provides a standard implementation of the Disposable pattern. My question is does WCF call the Dispose method of the Item class. WCF via the ServiceHost is basically taking responsibility for passing and receiving the Item object from our service contract implementation.

There are many examples on the web (such as this), but none of them ever call dispose or mention if it occurs.

Upvotes: 4

Views: 1063

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

WCF doesn't "take responsibility" for the object. WCF merely serializes the object you pass into it on one side, and creates a new on the other side.

There's really nothing that WCF can take ownership of.. so I'm not sure why you would think WCF would have any reason to dispose an object passed to it.

Usually, you would do something like this:

var proxy = new MyService();

var myobject = new MyObject();

proxy.Send(myobject);

// here myobject is still alive, and you can still do things with it.. 

EDIT:

It occurs to me that you are actually talking about the returned value from a service. In that case, yes.. by default WCF will call dispose on both parameters that are passed in and returned parameters. However, this is controlled by the OperationBehaviorAttribute.AutoDisposeParameters property, which defaults to true.

If you set this to false, you are responsible for calling dispose, typically in the OperationContext.OperationCompleted event handler.

Upvotes: 6

Related Questions