-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b695a96
commit 7d98af3
Showing
21 changed files
with
180 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
docs/examples/RabbitMQ.Next.Examples.DependencyInjection/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using RabbitMQ.Next; | ||
using RabbitMQ.Next.DependencyInjection; | ||
using RabbitMQ.Next.Examples.DependencyInjection; | ||
using RabbitMQ.Next.Serialization.PlainText; | ||
|
||
var builder = Host.CreateDefaultBuilder(args); | ||
|
||
builder.ConfigureServices(services => | ||
{ | ||
services.AddRabbitMQConnection( | ||
builder => builder | ||
.UseConnectionString("amqp://guest:guest@localhost:5672/") | ||
.UsePlainTextSerializer()); | ||
services.AddHostedService<Worker>(); | ||
}); | ||
|
||
using IHost host = builder.Build(); | ||
|
||
host.Run(); |
18 changes: 18 additions & 0 deletions
18
...bitMQ.Next.Examples.DependencyInjection/RabbitMQ.Next.Examples.DependencyInjection.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\RabbitMQ.Next.DependencyInjection\RabbitMQ.Next.DependencyInjection.csproj" /> | ||
<ProjectReference Include="..\..\..\src\RabbitMQ.Next.Publisher\RabbitMQ.Next.Publisher.csproj" /> | ||
<ProjectReference Include="..\..\..\src\RabbitMQ.Next.Serialization.PlainText\RabbitMQ.Next.Serialization.PlainText.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
docs/examples/RabbitMQ.Next.Examples.DependencyInjection/Worker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using RabbitMQ.Next.Publisher; | ||
|
||
namespace RabbitMQ.Next.Examples.DependencyInjection; | ||
|
||
public class Worker : BackgroundService | ||
{ | ||
private readonly IPublisher _publisher; | ||
private readonly IHostLifetime _hostLifetime; | ||
|
||
public Worker(IConnection connection, IHostLifetime hostLifetime) | ||
{ | ||
this._hostLifetime = hostLifetime; | ||
this._publisher = connection.Publisher("amq.fanout"); | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
await this._publisher.PublishAsync("Hello World!", cancellation: stoppingToken); | ||
Console.WriteLine("Message was published."); | ||
Console.WriteLine("Press [Enter] key to exit..."); | ||
Console.ReadLine(); | ||
await this._hostLifetime.StopAsync(stoppingToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/RabbitMQ.Next.DependencyInjection/RabbitMQ.Next.DependencyInjection.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net8.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\RabbitMQ.Next\RabbitMQ.Next.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Condition="'$(TargetFramework)' == 'net6.0'" Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" /> | ||
<PackageReference Condition="'$(TargetFramework)' == 'net8.0'" Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
50 changes: 50 additions & 0 deletions
50
src/RabbitMQ.Next.DependencyInjection/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace RabbitMQ.Next.DependencyInjection; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
#if NET8_0_OR_GREATER | ||
public static IServiceCollection AddRabbitMQConnection( | ||
this IServiceCollection serviceCollection, | ||
Action<IConnectionBuilder> connectionBuilder, | ||
ServiceLifetime lifetime = ServiceLifetime.Singleton, | ||
object serviceKey = null) | ||
{ | ||
serviceCollection.TryAdd( | ||
new ServiceDescriptor( | ||
typeof(IConnection), | ||
serviceKey, | ||
(sp, _) => | ||
{ | ||
var builder = ConnectionBuilder.Default; | ||
connectionBuilder(builder); | ||
return builder.Build(); | ||
}, | ||
lifetime)); | ||
|
||
return serviceCollection; | ||
} | ||
#else | ||
public static IServiceCollection AddRabbitMQConnection( | ||
this IServiceCollection serviceCollection, | ||
Action<IConnectionBuilder> connectionBuilder, | ||
ServiceLifetime lifetime = ServiceLifetime.Singleton) | ||
{ | ||
serviceCollection.TryAdd( | ||
new ServiceDescriptor( | ||
typeof(IConnection), | ||
sp => | ||
{ | ||
var builder = ConnectionBuilder.Default; | ||
connectionBuilder(builder); | ||
return builder.Build(); | ||
}, | ||
lifetime)); | ||
|
||
return serviceCollection; | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters