Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
fix(fx): fix plugin services not resolved in Development environment
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdayo committed Apr 10, 2023
1 parent 95555d9 commit c2bd032
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Flandre.Framework/Flandre.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Title>Flandre.Framework</Title>
<PackageVersion>1.0.0-rc.5</PackageVersion>
<PackageVersion>1.0.0-rc.6</PackageVersion>
<Authors>FlandreDevs</Authors>
<Description>现代化、跨平台的聊天机器人框架,一次编写,多处运行。</Description>
<PackageTags>bot;chatbot;flandre;framework</PackageTags>
Expand Down
8 changes: 5 additions & 3 deletions src/Flandre.Framework/FlandreApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ void WithCatch(Type pluginType, Func<Plugin, Task> subscriber, string? eventName
{
try
{
var plugin = (Plugin)Services.GetRequiredService(pluginType);
using var scope = Services.CreateScope();
var plugin = (Plugin)scope.ServiceProvider.GetRequiredService(pluginType);
await subscriber.Invoke(plugin);
}
catch (Exception e)
Expand Down Expand Up @@ -146,8 +147,9 @@ private void LoadPlugins()
{
foreach (var pluginType in _pluginTypes)
{
var loadCtx = new PluginLoadContext(pluginType, Services);
var plugin = (Plugin)Services.GetRequiredService(pluginType);
using var scope = Services.CreateScope();
var loadCtx = new PluginLoadContext(pluginType, scope.ServiceProvider);
var plugin = (Plugin)scope.ServiceProvider.GetRequiredService(pluginType);

loadCtx.LoadFromAttributes();
// Fluent API can override attributes
Expand Down
8 changes: 7 additions & 1 deletion src/Flandre.Framework/InternalMiddlewares.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ private FlandreApp UsePluginMessageHandler()

UseMiddleware(async (ctx, next) =>
{
_pluginTypes.ForEach(p => ((Plugin)Services.GetRequiredService(p)).OnMessageReceivedAsync(ctx));
_pluginTypes.ForEach(p =>
{
// create a new scope instead of using ctx scope
using var scope = Services.CreateScope();
((Plugin)scope.ServiceProvider.GetRequiredService(p)).OnMessageReceivedAsync(ctx);
});
await next();
});
return this;
Expand Down Expand Up @@ -212,6 +217,7 @@ public FlandreApp UseCommandInvoker()
if (!ctx.Command.TryParse(ctx.CommandStringParser, cmdService, out var result))
return result.ErrorText!;

// ctx.Service is a service scope
var plugin = (Plugin)ctx.Services.GetRequiredService(ctx.Command.PluginType);
var pluginLogger = (ILogger)Services.GetRequiredService(plugin.LoggerType);

Expand Down

0 comments on commit c2bd032

Please sign in to comment.