Skip to content

Commit

Permalink
Merge pull request #5 from FrendsPlatform/#4_fix_hang_on_finalize
Browse files Browse the repository at this point in the history
Issue #4: Removed finalizer call to dispose message listener, changed to Abort()
  • Loading branch information
EkiH committed Dec 21, 2018
2 parents 05f8dc2 + a5d6bd0 commit 6eb7e49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
29 changes: 15 additions & 14 deletions Frends.ServiceBus/MessagingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Net;

Expand All @@ -22,7 +23,7 @@ public static ServiceBusMessagingFactory Instance
}

private static readonly object factoryLock = new Object();
private static readonly ConcurrentDictionary<string, MessagingFactory> _messagingFactories = new ConcurrentDictionary<string, MessagingFactory>();
private readonly ConcurrentDictionary<string, MessagingFactory> _messagingFactories = new ConcurrentDictionary<string, MessagingFactory>();

private ServiceBusMessagingFactory()
{
Expand Down Expand Up @@ -139,30 +140,30 @@ private void Dispose(bool disposing)
{
// TODO: dispose managed state (managed objects).
}

foreach (var item in factoriesToClose)
{
try
{
item.Value.Close();
item.Value.Abort();
}
catch (Exception ex)
{
Trace.TraceError("Error when aborting messaging factory connection " + ex);
}
catch (Exception) { }

}

// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.

_disposedValue = true;
}
}

// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
/// <summary>Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.</summary>
~ServiceBusMessagingFactory()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
//~ServiceBusMessagingFactory()
//{
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// //Dispose(false);
//}

/// <summary>
/// Dispose of the MessagingFactory and close all the cached connections
Expand All @@ -171,8 +172,8 @@ public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
GC.SuppressFinalize(this);

//GC.SuppressFinalize(this);
}
#endregion

Expand Down
4 changes: 2 additions & 2 deletions Frends.ServiceBus/ServiceBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ private static Encoding GetEncoding(MessageEncoding encodingChoice, string encod
switch (options.DestinationType)
{
case QueueOrTopic.Queue:
await EnsureQueueExists(input.QueueOrTopicName, input.ConnectionString);
await EnsureQueueExists(input.QueueOrTopicName, input.ConnectionString).ConfigureAwait(false);
break;
case QueueOrTopic.Topic:
await EnsureTopicExists(input.QueueOrTopicName, input.ConnectionString);
await EnsureTopicExists(input.QueueOrTopicName, input.ConnectionString).ConfigureAwait(false);
break;
default:
throw new Exception($"Unexpected destination type: {options.DestinationType}");
Expand Down

0 comments on commit 6eb7e49

Please sign in to comment.