Skip to content

Commit dd82a21

Browse files
committed
EventBus: вынести конфигурирование в клиент
1 parent ee07549 commit dd82a21

File tree

6 files changed

+63
-51
lines changed

6 files changed

+63
-51
lines changed

HwProj.AuthService/HwProj.AuthService.API/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Identity;
88
using Microsoft.EntityFrameworkCore;
99
using HwProj.AuthService.API.Services;
10+
using HwProj.EventBus.Client;
1011
using HwProj.EventBus.Client.Interfaces;
1112
using Microsoft.IdentityModel.Tokens;
1213
using Microsoft.AspNetCore.Authentication.JwtBearer;

HwProj.Common/HwProj.Utils/Configuration/StartupExtensions.cs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Net.Sockets;
53
using System.Threading;
64
using AutoMapper;
7-
using HwProj.EventBus.Client;
8-
using HwProj.EventBus.Client.Implementations;
9-
using HwProj.EventBus.Client.Interfaces;
105
using HwProj.Utils.Auth;
116
using HwProj.Utils.Configuration.Middleware;
127
using Microsoft.AspNetCore.Authentication.JwtBearer;
138
using Microsoft.AspNetCore.Builder;
149
using Microsoft.AspNetCore.Hosting;
1510
using Microsoft.AspNetCore.Mvc;
1611
using Microsoft.EntityFrameworkCore;
17-
using Microsoft.Extensions.Configuration;
1812
using Microsoft.Extensions.DependencyInjection;
1913
using Microsoft.Extensions.Logging;
2014
using Microsoft.IdentityModel.Tokens;
2115
using Microsoft.OpenApi.Models;
2216
using Newtonsoft.Json;
23-
using Polly;
24-
using RabbitMQ.Client;
25-
using RabbitMQ.Client.Exceptions;
2617

2718
namespace HwProj.Utils.Configuration
2819
{
@@ -106,48 +97,6 @@ private static void ConfigureHwProjServiceSwaggerGen(this IServiceCollection ser
10697
});
10798
}
10899

109-
public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
110-
{
111-
var eventBusSection = configuration.GetSection("EventBus");
112-
113-
var retryCount = 5;
114-
if (!string.IsNullOrEmpty(eventBusSection["EventBusRetryCount"]))
115-
{
116-
retryCount = int.Parse(eventBusSection["EventBusRetryCount"]);
117-
}
118-
119-
services.AddSingleton(sp => Policy.Handle<SocketException>()
120-
.Or<BrokerUnreachableException>()
121-
.WaitAndRetry(retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));
122-
123-
services.AddSingleton<IConnectionFactory, ConnectionFactory>(sp => new ConnectionFactory
124-
{
125-
HostName = eventBusSection["EventBusHostName"],
126-
UserName = eventBusSection["EventBusUserName"],
127-
Password = eventBusSection["EventBusPassword"],
128-
VirtualHost = eventBusSection["EventBusVirtualHost"]
129-
});
130-
131-
services.AddSingleton<IDefaultConnection, DefaultConnection>();
132-
services.AddSingleton<IEventBus, EventBusRabbitMq>();
133-
134-
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).ToList();
135-
var eventTypes = types.Where(x => typeof(Event).IsAssignableFrom(x));
136-
foreach (var eventType in eventTypes)
137-
{
138-
var fullTypeInterface = typeof(IEventHandler<>).MakeGenericType(eventType);
139-
var handlersTypes = types.Where(x =>
140-
fullTypeInterface.IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
141-
142-
foreach (var handlerType in handlersTypes)
143-
{
144-
services.AddTransient(handlerType);
145-
}
146-
}
147-
148-
return services;
149-
}
150-
151100
public static IApplicationBuilder ConfigureHwProj(this IApplicationBuilder app, IHostingEnvironment env,
152101
string serviceName, DbContext? context = null)
153102
{

HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using HwProj.CoursesService.API.Repositories;
66
using HwProj.CoursesService.API.Repositories.Groups;
77
using HwProj.CoursesService.API.Services;
8+
using HwProj.EventBus.Client;
89
using HwProj.Utils.Configuration;
910
using Microsoft.AspNetCore.Builder;
1011
using Microsoft.AspNetCore.Hosting;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Linq;
3+
using System.Net.Sockets;
4+
using HwProj.EventBus.Client.Implementations;
5+
using HwProj.EventBus.Client.Interfaces;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Polly;
9+
using RabbitMQ.Client;
10+
using RabbitMQ.Client.Exceptions;
11+
12+
namespace HwProj.EventBus.Client
13+
{
14+
public static class ConfigurationExtensions
15+
{
16+
public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
17+
{
18+
var eventBusSection = configuration.GetSection("EventBus");
19+
20+
var retryCount = 5;
21+
if (!string.IsNullOrEmpty(eventBusSection["EventBusRetryCount"]))
22+
{
23+
retryCount = int.Parse(eventBusSection["EventBusRetryCount"]);
24+
}
25+
26+
services.AddSingleton(sp => Policy.Handle<SocketException>()
27+
.Or<BrokerUnreachableException>()
28+
.WaitAndRetry(retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));
29+
30+
services.AddSingleton<IConnectionFactory, ConnectionFactory>(sp => new ConnectionFactory
31+
{
32+
HostName = eventBusSection["EventBusHostName"],
33+
UserName = eventBusSection["EventBusUserName"],
34+
Password = eventBusSection["EventBusPassword"],
35+
VirtualHost = eventBusSection["EventBusVirtualHost"]
36+
});
37+
38+
services.AddSingleton<IDefaultConnection, DefaultConnection>();
39+
services.AddSingleton<IEventBus, EventBusRabbitMq>();
40+
41+
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).ToList();
42+
var eventTypes = types.Where(x => typeof(Event).IsAssignableFrom(x));
43+
foreach (var eventType in eventTypes)
44+
{
45+
var fullTypeInterface = typeof(IEventHandler<>).MakeGenericType(eventType);
46+
var handlersTypes = types.Where(x =>
47+
fullTypeInterface.IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
48+
49+
foreach (var handlerType in handlersTypes)
50+
{
51+
services.AddTransient(handlerType);
52+
}
53+
}
54+
55+
return services;
56+
}
57+
58+
}
59+
}

HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.Extensions.Configuration;
1313
using Microsoft.Extensions.DependencyInjection;
1414
using HwProj.CoursesService.API.Events;
15+
using HwProj.EventBus.Client;
1516
using HwProj.SolutionsService.API.Events;
1617
using UpdateTaskMaxRatingEvent = HwProj.CoursesService.API.Events.UpdateTaskMaxRatingEvent;
1718

HwProj.SolutionsService/HwProj.SolutionsService.API/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using HwProj.AuthService.Client;
22
using HwProj.CoursesService.Client;
3+
using HwProj.EventBus.Client;
34
using HwProj.EventBus.Client.Interfaces;
45
using HwProj.SolutionsService.API.Models;
56
using HwProj.SolutionsService.API.Repositories;

0 commit comments

Comments
 (0)