4

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.

2

1 Answer 1

6

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.

1
  • On the client side you clearly retain ownership. But on the server side you are passing an object into the WCF framework. Item Get(ItemRequest request) { return new Item(); }
    – denver
    Sep 10, 2014 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.