mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
add Uri parser, add ISerializationBinder
ISerializationBinder is added only in whitelist mode. Types for whitelisting should be registered manually via new API `Module.StartAsync()` or attribute `AllowedSerializableType(name)`
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using Castle.Core.Internal;
|
||||
using Kruzya.TelegramBot.Core.Attributes;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Service
|
||||
{
|
||||
public class CoreManager : IHostedService
|
||||
{
|
||||
private readonly Core _core;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
|
||||
public CoreManager(Core core, IServiceScopeFactory scopeFactory)
|
||||
{
|
||||
_core = core;
|
||||
_scopeFactory = scopeFactory;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cts)
|
||||
{
|
||||
await ManagerStartAsync(cts, _scopeFactory.CreateScope());
|
||||
foreach (var module in _core.Modules)
|
||||
{
|
||||
await module.StartAsync(cts, _scopeFactory.CreateScope());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cts)
|
||||
{
|
||||
// await ManagerStopAsync(cts, _scopeFactory.CreateScope());
|
||||
foreach (var module in _core.Modules)
|
||||
{
|
||||
await module.StopAsync(cts, _scopeFactory.CreateScope());
|
||||
}
|
||||
}
|
||||
|
||||
public Task ManagerStartAsync(CancellationToken cts, IServiceScope scope)
|
||||
{
|
||||
var binder = scope.ServiceProvider.GetRequiredService<SerializationBinder>();
|
||||
var allowedSerializationTypes = _core.Modules
|
||||
.SelectMany(module => module.GetType().Assembly.ExportedTypes)
|
||||
.Where(definedType => definedType.GetAttribute<AllowedSerializableTypeAttribute>() != null)
|
||||
.ToDictionary(type => type.GetAttribute<AllowedSerializableTypeAttribute>().Name);
|
||||
|
||||
foreach (var pair in allowedSerializationTypes)
|
||||
{
|
||||
binder.RegisterAllowedType(pair.Key, pair.Value);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user