title: Disable automatic disposal of Input Parameters in WCF service call
body: <p>I have a WCF server configured for singleton instancing.  My client is passing as a parameter to a service method, an object that implements the <code>IDisposable</code> interface and the server is attempting to cache this instance, however, the parameter is being disposed automatically by the WCF runtime at some point <em>after</em> the service method is executed , destroying my cached instance prematurely.</p>
  
  <p>By looking at the stack trace,  I find that the <code>Dispose()</code> call of the parameter is being invoked inside the <code>MessageRpc.DisposeParametersCore()</code> method.</p>
  
  <p>This is the reference source for that method as taken from <a href="https://github.com/Microsoft/referencesource/blob/master/System.ServiceModel/System/ServiceModel/Dispatcher/MessageRpc.cs" rel="nofollow noreferrer">here</a>:</p>
  
  <pre><code> internal void DisposeParametersCore(bool excludeInput)
          {
              if (!this.ParametersDisposed)
              {
                  if (!excludeInput)
                  {
                      this.DisposeParameterList(this.InputParameters);
                  }
                  this.DisposeParameterList(this.OutputParameters);
                  IDisposable disposableParameter = this.ReturnParameter as IDisposable;
                  if (disposableParameter != null)
                  {
                      try
                      {
                          disposableParameter.Dispose();
                      }
                      catch (Exception e)
                      {
                          if (Fx.IsFatal(e))
                          {
                              throw;
                          }
                          this.channelHandler.HandleError(e);
                      }
                  }
                  this.ParametersDisposed = true;
              }
          }
  </code></pre>
  
  <p>As you can see, the disposal of the input parameters is controlled by the bool parameter <code>excludeInput</code> which hints me that this behavior is optional. </p>
  
  <p>I know that if I cache a <strong>deep copy</strong> of the parameter it will circumvent this issue, but is there a way I can turn off this automatic behavior for a particular WCF method?</p>
  
  <p>Here is the object class I am trying to cache on the server (written in C++ CLI):</p>
  
  <pre><code>[Serializable()]
     public ref class OpaqueMediaType : ISerializable, IConcreteMediaType {
     private:
        static const Byte _version = 1;
     private:
        clr_scoped_ptr&lt;CComPtrIMFMediaType&gt; _ppMediaType;
     protected:
        virtual DMO_MEDIA_TYPE* __clrcall GetConcreteDMOMediaType() sealed 
           = IConcreteMediaType::GetConcreteDMOMediaType;
        virtual CComPtrIMFMediaType __clrcall  GetConcreteMFMediaType() sealed 
           = IConcreteMediaType::GetConcreteMFMediaType;
        virtual void __clrcall FreeConcreteDMOMediaType(DMO_MEDIA_TYPE* pDMOMediaType) sealed
           = IConcreteMediaType::FreeConcreteDMOMediaType;
     protected:
        OpaqueMediaType(SerializationInfo^ info, StreamingContext context);
     public:
        OpaqueMediaType(DMO_MEDIA_TYPE&amp; dmoMediaType);
        OpaqueMediaType(IMFMediaType* pMFMediaType);
        OpaqueMediaType(PCM_MediaType pcmMediaType);
        virtual void __clrcall GetObjectData(SerializationInfo^ info, StreamingContext context);
        PCM_MediaType AsPCM();
     };
  </code></pre>
  
  <p>The <code>_ppMediaType</code> member is taking ownership of a CComPtr derived class pointer, so when the instance is disposed, the associated COM object is released. Since this member is disposable, the <code>IDisposable</code> interface for the <code>OpaqueMediaType</code> class is automatically defined and implemented by C++/CLI.</p>
  
  <p>Here is the WCF method that is caching the object:</p>
  
  <pre><code> Task IStorageBackendSvc.AcceptWmaMediaType(int stationId, OpaqueMediaType mediaType) {
           try {
              WmaWriter wmaWriter = GetWmaWriter(stationId);
              wmaWriter.MediaType = mediaType;  // parameter object is cached here (shallow copy)
              return Task.CompletedTask;
           } catch( Exception exception ) {
              throw _faultFactory.Wrap(exception);
           }
  // the `mediaType` parameter is being disposed by WCF at some point AFTER calling this code, releasing the internal COM object held by the cached instance prematurely
        }
  </code></pre>