Skip to content

Commit

Permalink
[OneBot] Featuring Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Dec 7, 2023
1 parent dd6726f commit d7f93a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
9 changes: 6 additions & 3 deletions Lagrange.OneBot/Core/Network/LagrangeWebSvcCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ public async Task StartAsync(CancellationToken cancellationToken)
foreach (var section in implsSection.GetChildren())
{
var scope = services.CreateScope();
var factory = services.GetRequiredService<ILagrangeWebServiceFactory>();
var serviceProvider = scope.ServiceProvider;

var factory = serviceProvider.GetRequiredService<ILagrangeWebServiceFactory>();
factory.SetConfig(section);

if (factory.Create() is not { } webService) continue;

var webService = services.GetRequiredService<ILagrangeWebService>();
webService.OnMessageReceived += (sender, args) =>
{
OnMessageReceived?.Invoke(sender, new MsgRecvEventArgs(args.Data));
};

try
{
await webService.StartAsync(cancellationToken);
Expand Down
10 changes: 3 additions & 7 deletions Lagrange.OneBot/Core/Network/Service/ForwardWSServiceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@

namespace Lagrange.OneBot.Core.Network.Service
{
public sealed class ForwardWSServiceFactory : LagrangeWebServiceFactory, ILagrangeWebServiceFactory<ForwardWSService>
public sealed class ForwardWSServiceFactory(IServiceProvider services) : LagrangeWebServiceFactory(services), ILagrangeWebServiceFactory<ForwardWSService>
{
public ForwardWSServiceFactory(IServiceProvider services) : base(services)
{

}

public override ILagrangeWebService? Create()
public override ILagrangeWebService Create()
{
var config = _config ?? throw new InvalidOperationException("Configuration must be provided");
var options = _services.GetRequiredService<IOptionsSnapshot<ForwardWSServiceOptions>>();
config.Bind(options.Value);

return _services.GetRequiredService<ForwardWSService>();
}
}
Expand Down
10 changes: 3 additions & 7 deletions Lagrange.OneBot/Core/Network/Service/ReverseWSServiceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@

namespace Lagrange.OneBot.Core.Network.Service
{
public sealed class ReverseWSServiceFactory : LagrangeWebServiceFactory, ILagrangeWebServiceFactory<ReverseWSService>
public sealed class ReverseWSServiceFactory(IServiceProvider services) : LagrangeWebServiceFactory(services), ILagrangeWebServiceFactory<ReverseWSService>
{
public ReverseWSServiceFactory(IServiceProvider services) : base(services)
{

}

public override ILagrangeWebService? Create()
public override ILagrangeWebService Create()
{
var config = _config ?? throw new InvalidOperationException("Configuration must be provided");
var options = _services.GetRequiredService<IOptionsSnapshot<ReverseWSServiceOptions>>();
config.Bind(options.Value);

return _services.GetRequiredService<ReverseWSService>();
}
}
Expand Down

0 comments on commit d7f93a9

Please sign in to comment.