Skip to content

Commit 04e6fa8

Browse files
authored
Version: 1.1.14 (#7)
* Added a simple ConfigurationSection for use in appsettings.json * Using the new section in one of the examples.
1 parent 3fad9c9 commit 04e6fa8

6 files changed

Lines changed: 79 additions & 4 deletions

File tree

Examples/RabbitExpress.Example.RpcClient/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ namespace RabbitExpress.Example.RpcClient
3939
/// </summary>
4040
internal class Program
4141
{
42-
private static IConfiguration _configuration;
42+
private static IConfigurationRoot _configuration;
4343

4444
private static async Task Test()
4545
{
46-
using (var qc = new QueueClient<MsgPackSerializer>(new Uri(_configuration["RabbitExpressConnection"])))
46+
var qConfig = new QueueConfig();
47+
_configuration.GetSection("RabbitExpress").Bind(qConfig);
48+
using (var qc = new QueueClient<MsgPackSerializer>(qConfig))
4749
{
4850
IExampleService client = qc.RpcClient<IExampleService>();
4951
Console.WriteLine(client.Calculate(2, 4));

Examples/RabbitExpress.Example.RpcClient/RabbitExpress.Example.RpcClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
11+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.4" />
1112
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
1213
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
1314
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"RabbitExpressConnection": "[connectionString]"
2+
"RabbitExpress": {
3+
"ConnectionString": "[connectionString]",
4+
"PrefetchSize": 1
5+
}
36
}

RabbitExpress/QueueClient.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
// ***********************************************************************
2929
namespace RabbitExpress
3030
{
31+
using Microsoft.Extensions.Options;
3132
using MsgPack.Serialization;
3233
using RabbitMQ.Client;
3334
using RabbitMQ.Client.Events;
@@ -209,14 +210,42 @@ private void RegisterQueues<TInterface>()
209210
/// </summary>
210211
/// <param name="connectionString">The connection string.</param>
211212
public QueueClient(Uri connectionString)
213+
: this(new ConnectionFactory() { Uri = connectionString, DispatchConsumersAsync = true })
212214
{
213215
var factory = new ConnectionFactory()
214216
{
215217
Uri = connectionString,
216218
DispatchConsumersAsync = true
217219
};
220+
}
218221

219-
_connection = factory.CreateConnection();
222+
/// <summary>
223+
/// Initializes a new instance of the <see cref="QueueClient{TSerializer}"/> class.
224+
/// </summary>
225+
/// <param name="config">The connection setting.</param>
226+
public QueueClient(IOptions<QueueConfig> config)
227+
: this(config.Value.ConnectionString)
228+
{
229+
_model.BasicQos(0, config.Value.PrefetchSize, false);
230+
}
231+
232+
/// <summary>
233+
/// Initializes a new instance of the <see cref="QueueClient{TSerializer}"/> class.
234+
/// </summary>
235+
/// <param name="config">The connection setting.</param>
236+
public QueueClient(QueueConfig config)
237+
: this(config.ConnectionString)
238+
{
239+
_model.BasicQos(0, config.PrefetchSize, false);
240+
}
241+
242+
/// <summary>
243+
/// Initializes a new instance of the <see cref="QueueClient{TSerializer}"/> class.
244+
/// </summary>
245+
/// <param name="connectionFactory">The connection factory used to create the connection.</param>
246+
public QueueClient(IConnectionFactory connectionFactory)
247+
{
248+
_connection = connectionFactory.CreateConnection();
220249
_model = _connection.CreateModel();
221250

222251
_consumer = new AsyncEventingBasicConsumer(_model);

RabbitExpress/QueueConfig.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ***********************************************************************
2+
// Assembly : RabbitExpress
3+
// Author : Rene Windegger
4+
// Created : 06-06-2019
5+
//
6+
// Last Modified By : Rene Windegger
7+
// Last Modified On : 06-06-2019
8+
// ***********************************************************************
9+
// <copyright file="QueueConfig.cs" company="Rene Windegger">
10+
// Copyright (c) Rene Windegger. All rights reserved.
11+
// </copyright>
12+
// <summary>
13+
// This file is part of RabbitExpress.
14+
//
15+
// RabbitExpress is free software: you can redistribute it and/or modify
16+
// it under the terms of the GNU Lesser General Public License as published by
17+
// the Free Software Foundation, either version 3 of the License, or
18+
// (at your option) any later version.
19+
//
20+
// RabbitExpress is distributed in the hope that it will be useful,
21+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
// GNU Lesser General Public License for more details.
24+
//
25+
// You should have received a copy of the GNU Lesser General Public License
26+
// along with this RabbitExpress. If not, see <http://www.gnu.org/licenses/>.
27+
// </summary>
28+
// ***********************************************************************
29+
namespace RabbitExpress
30+
{
31+
using System;
32+
33+
public class QueueConfig
34+
{
35+
public Uri ConnectionString { get; set; }
36+
public ushort PrefetchSize { get; set; }
37+
}
38+
}

RabbitExpress/RabbitExpress.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
</PropertyGroup>
2323

2424
<ItemGroup>
25+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
26+
<PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" />
2527
<PackageReference Include="RabbitMQ.Client" Version="5.1.0" />
2628
<PackageReference Include="MsgPack.Cli" Version="1.0.1" />
2729
<PackageReference Include="SexyProxy" Version="2.0.104" />

0 commit comments

Comments
 (0)