Skip to content

Commit

Permalink
COREX-346 CoreDataService keeps crashing (#26)
Browse files Browse the repository at this point in the history
- Add error handling when disposing AnnouncementClient
  • Loading branch information
raffii90 authored Mar 14, 2024
1 parent cb17b44 commit 3a0cd5f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using System;
using System.ServiceModel;
using System.ServiceModel.Discovery;
Expand All @@ -7,10 +8,12 @@ namespace EMG.Utilities.ServiceModel.Discovery
public class DefaultAnnouncementClientWrapper : IAnnouncementClientWrapper
{
private readonly AnnouncementClient _client;
private readonly ILogger<DefaultAnnouncementClientWrapper> _logger;

public DefaultAnnouncementClientWrapper(AnnouncementClient client)
public DefaultAnnouncementClientWrapper(AnnouncementClient client, ILogger<DefaultAnnouncementClientWrapper> logger)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public void AnnounceOnline(EndpointDiscoveryMetadata metadata)
Expand All @@ -27,13 +30,22 @@ public void Dispose()
{
var communicationObject = _client as ICommunicationObject;

if (communicationObject.State != CommunicationState.Faulted)
try
{
((IDisposable)_client).Dispose();
if (communicationObject.State != CommunicationState.Faulted)
{
((IDisposable)_client).Dispose();
}
else
{
communicationObject.Abort();
}
}
else
catch (Exception ex)
{
communicationObject.Abort();

_logger.LogError(ex, "An error occurred while disposing the announcement client");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
Expand All @@ -7,13 +8,20 @@ namespace EMG.Utilities.ServiceModel.Discovery
{
public class DefaultAnnouncementClientWrapperFactory : IAnnouncementClientWrapperFactory
{
private readonly ILogger<DefaultAnnouncementClientWrapper> _logger;

public DefaultAnnouncementClientWrapperFactory(ILogger<DefaultAnnouncementClientWrapper> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public IAnnouncementClientWrapper Create(Uri registryUrl, Binding binding)
{
var endpointAddress = new EndpointAddress(registryUrl);
var endpoint = new AnnouncementEndpoint(binding, endpointAddress);
var client = new AnnouncementClient(endpoint);

var wrapper = new DefaultAnnouncementClientWrapper(client);
var wrapper = new DefaultAnnouncementClientWrapper(client, _logger);

return wrapper;
}
Expand Down

0 comments on commit 3a0cd5f

Please sign in to comment.