title: Usage of (Reactive-) Events in Finalizers
body: <p>I have the following business rule:
  I have a license, that I receive from another programm, that needs to be used by a third programm. After the third programm acknowledges that it received the license I want to write it to a file for documentation purposes. If anything goes wrong (including the user closing the part of my application that is waiting for the response) I need to write the license back to the programm that send me the license, to store it there again. I am (for security Purposes I am told) not allowed to just safe the license (encrypted or not) into a file and call it a day.</p>
  
  <p>The basic case, waiting for the other application to acknowledge the license is straightforward:</p>
  
  <pre><code>public bool UseKey(Func&lt;string, bool&gt; func, string serviceTag)
  {
      Throw.IfNull(serviceTag, nameof(serviceTag));
      Throw.IfNull(func, nameof(func)); //Check if null
  
      if (func(_license)) //Programm acknowledged.
      {
          _keyUsed.OnNext(new UsedKey(serviceTag, _license)); //License was used. Therefore inform others that it can be written to file.
          return true;
      }
      return false; //Didn't work. Inform caller.
  }
  </code></pre>
  
  <p>My idea for the case when something went wrong was the following:
  I implement the <code>IDisposable</code> interface and in my Dispose(bool) method I'd send of the correct event informing the handler to write it properly back.</p>
  
  <pre><code>void Dispose(bool disposing)
  {
      if (!_disposed)
      {
          if (disposing)
          {
              if (!_keyWasUsed)
                  _keyNotUsed.OnNext(_license);
              _keyUsed.Dispose();
              _keyNotUsed.Dispose();
          }
  
          // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
          // TODO: set large fields to null.
  
          _disposed = true;
      }
  }
  </code></pre>
  
  <p>The Problem I see is twofold. </p>
  
  <ol>
  <li>If the client doesn't call the <code>Dispose</code> method I would not get informed that something went wrong.</li>
  <li>If I put the call to the event into a finalizer I cannot know for sure that I could even fire an event anymore because maybe my references are already cleaned or the license field is already collected.</li>
  </ol>
  
  <p>Therefore my Question(s): How can I issue an event or some kind of message (either Rx or even classic events if that makes anything easier) to some other executing code from a finalizer?</p>
  
  <p>(Or is there a better approach than using <code>IDisposable</code> for this kind of work, that gives me the guarantee that either the operation succeeds or I can roll back (kinda like sql transactions)?)</p>