Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RabbitMQ connection not closing #158

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Services/RabbitMQService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.RabbitMQ
internal sealed class RabbitMQService : IRabbitMQService
{
private readonly IRabbitMQModel _rabbitMQModel;
private readonly IConnection _connection;
private readonly IModel _model;
private readonly IBasicPublishBatch _batch;
private readonly string _connectionString;
Expand All @@ -27,14 +28,14 @@ public RabbitMQService(string connectionString, string hostName, string userName
_port = port;

ConnectionFactory connectionFactory = GetConnectionFactory(_connectionString, _hostName, _userName, _password, _port);

_model = connectionFactory.CreateConnection().CreateModel();
_connection = connectionFactory.CreateConnection();
_model = _connection.CreateModel();
}

public RabbitMQService(string connectionString, string hostName, string queueName, string userName, string password, int port)
: this(connectionString, hostName, userName, password, port)
{
_rabbitMQModel = new RabbitMQModel(_model);
_rabbitMQModel = new RabbitMQModel(_connection, _model);
_queueName = queueName ?? throw new ArgumentNullException(nameof(queueName));

_model.QueueDeclarePassive(_queueName); // Throws exception if queue doesn't exist
Expand Down
29 changes: 27 additions & 2 deletions src/Trigger/RabbitMQModel.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using RabbitMQ.Client;

namespace Microsoft.Azure.WebJobs.Extensions.RabbitMQ
{
public class RabbitMQModel : IRabbitMQModel
public class RabbitMQModel : IRabbitMQModel, IDisposable
{
private readonly IModel _model;
private readonly IConnection _connection;

public RabbitMQModel(IModel model)
private bool _disposed = false;

public RabbitMQModel(IConnection connection, IModel model)
{
_model = model;
_connection = connection;
}

~RabbitMQModel() => Dispose(false);

public IModel Model => _model;

public IBasicPublishBatch CreateBasicPublishBatch()
Expand Down Expand Up @@ -74,7 +81,25 @@ public void ExchangeDeclare(string exchange, string exchangeType)

public void Close()
{
_disposed = true;
_model.Close();
_connection.Close();
}

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

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

Close();
}
}
}