feat: added DI to Client
This commit is contained in:
38
JOBot.TClient/DependencyInjection.cs
Normal file
38
JOBot.TClient/DependencyInjection.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Grpc.Core;
|
||||
using Grpc.Net.Client;
|
||||
using JOBot.Proto;
|
||||
using JOBot.TClient.Commands;
|
||||
using JOBot.TClient.Core.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegram.Bot;
|
||||
|
||||
namespace JOBot.TClient;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection ConfigureServices(this IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddSingleton(config);
|
||||
|
||||
services.AddScoped<ChannelBase, GrpcChannel>(_ => GrpcChannel.ForAddress(config.GetValue<string>("BackendHost")
|
||||
?? throw new MissingFieldException("Host is not defined")));
|
||||
|
||||
//Commands
|
||||
services.AddScoped<StartCommand>();
|
||||
|
||||
services.AddSingleton<UserService>();
|
||||
|
||||
//gRPC Clients
|
||||
services.AddScoped<User.UserClient>();
|
||||
|
||||
// Telegram Bot
|
||||
services.AddSingleton<ITelegramBotClient>(_ =>
|
||||
new TelegramBotClient(config.GetValue<string>("TelegramToken")
|
||||
?? throw new MissingFieldException("TelegramToken is not set")));
|
||||
|
||||
services.AddLogging(builder => builder.AddConsole());
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user