How to dispose unsent messages in a queue in C#

324 views Asked by At

I am using a concurrent queue in my C# Web API. I want to handle the situation where in case of a graceful shutdown, I should be able to empty my queue, process it and dispose it off.

Below is my code snippet where I am trying to dispose and write last message to the file but I am unable debug or get dispose file created when I manually stop IIS App pool.

Any suggestions?

 public static void SendToDisposeFile()
    {
        File.AppendAllText(Path.Combine(Path.Combine(@"D:\", "Input"), "dispose-input-file-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt"), "Disposed text" + Environment.NewLine);          

    }
 #region IDisposable implementation

    ~RequestQueue()
    {
        Dispose(false);
    }

    public void Dispose()
    {            
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed)
            return;

        _disposed = true;

        if (!disposing)
            return;
        if(_queue.Count > 0)
            SendToDisposeFile();           
      
    }

    #endregion
0

There are 0 answers