-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
7 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
RabbitMq/RabbitMq.Consumer/HostedServices/ConsumerHostedService.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,25 @@ | ||
using Microsoft.Extensions.Hosting; | ||
using RabbitMq.Consumer.Services; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace RabbitMq.Consumer.HostedServices | ||
{ | ||
public class ConsumerHostedService : BackgroundService | ||
{ | ||
private readonly IConsumerService _consumerService; | ||
|
||
public ConsumerHostedService(IConsumerService consumerService) | ||
{ | ||
_consumerService = consumerService; | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
await _consumerService.ReadMessgaes(); | ||
} | ||
} | ||
} |
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,26 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace RabbitMq.Consumer | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
} |
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 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:3920", | ||
"sslPort": 44319 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"RabbitMq.Consumer": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": "true", | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:5005;", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\RabbitMq.Common\RabbitMq.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<ActiveDebugProfile>RabbitMq.Consumer</ActiveDebugProfile> | ||
</PropertyGroup> | ||
</Project> |
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 RabbitMq.Common.Services; | ||
using RabbitMQ.Client.Events; | ||
using System; | ||
using RabbitMQ.Client; | ||
using System.Threading.Tasks; | ||
|
||
namespace RabbitMq.Consumer.Services | ||
{ | ||
public interface IConsumerService | ||
{ | ||
Task ReadMessgaes(); | ||
} | ||
|
||
public class ConsumerService : IConsumerService, IDisposable | ||
{ | ||
private readonly IModel _model; | ||
private readonly IConnection _connection; | ||
public ConsumerService(IRabbitMqService rabbitMqService) | ||
{ | ||
_connection = rabbitMqService.CreateChannel(); | ||
_model = _connection.CreateModel(); | ||
_model.QueueDeclare(_queueName, durable: true, exclusive: false, autoDelete: false); | ||
_model.ExchangeDeclare("UserExchange", ExchangeType.Fanout, durable: true, autoDelete: false); | ||
_model.QueueBind(_queueName, "UserExchange", string.Empty); | ||
} | ||
const string _queueName = "User"; | ||
public async Task ReadMessgaes() | ||
{ | ||
var consumer = new AsyncEventingBasicConsumer(_model); | ||
consumer.Received += async (ch, ea) => | ||
{ | ||
var body = ea.Body.ToArray(); | ||
var text = System.Text.Encoding.UTF8.GetString(body); | ||
Console.WriteLine(text); | ||
await Task.CompletedTask; | ||
_model.BasicAck(ea.DeliveryTag, false); | ||
}; | ||
_model.BasicConsume(_queueName, false, consumer); | ||
await Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_model.IsOpen) | ||
_model.Close(); | ||
if (_connection.IsOpen) | ||
_connection.Close(); | ||
} | ||
} | ||
} |
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,35 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using RabbitMq.Common.Extensions; | ||
using RabbitMq.Consumer.HostedServices; | ||
using RabbitMq.Consumer.Services; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace RabbitMq.Consumer | ||
{ | ||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; set; } | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddCommonService(Configuration); | ||
services.AddSingleton<IConsumerService, ConsumerService>(); | ||
services.AddHostedService<ConsumerHostedService>(); | ||
} | ||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
} | ||
} | ||
} |