yiyungent / PluginCore

🔌 ASP.NET Core lightweight plugin framework | ASP.NET Core 轻量级 插件框架 - 一分钟集成 | Vue.js frontend | JavaScript SDK
https://yiyungent.github.io/PluginCore/
GNU Lesser General Public License v3.0
457 stars 87 forks source link

PluginApplicationBuilder: System.NullReferenceException: 'Object reference not set to an instance of an object. #5

Open yiyungent opened 3 years ago

yiyungent commented 3 years ago
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using PluginCore.IPlugins;

namespace WebSocketDemoPlugin
{
    public class WebSocketDemoPlugin : BasePlugin, IStartupXPlugin
    {
        public override (bool IsSuccess, string Message) AfterEnable()
        {
            Console.WriteLine($"{nameof(WebSocketDemoPlugin)}: {nameof(AfterEnable)}");
            return base.AfterEnable();
        }

        public override (bool IsSuccess, string Message) BeforeDisable()
        {
            Console.WriteLine($"{nameof(WebSocketDemoPlugin)}: {nameof(BeforeDisable)}");
            return base.BeforeDisable();
        }

        public void ConfigureServices(IServiceCollection services)
        {

        }

        public void Configure(IApplicationBuilder app)
        {
            var webSocketOptions = new WebSocketOptions()
            {
                // KeepAliveInterval - 向客户端发送 "ping" 帧的频率,以确保代理保持连接处于打开状态。 默认值为 2 分钟。
                KeepAliveInterval = TimeSpan.FromSeconds(120),
            };
            app.UseWebSockets(webSocketOptions);

        }

        public int ConfigureOrder
        {
            get
            {
                return 2;
            }
        }

        public int ConfigureServicesOrder
        {
            get
            {
                return 2;
            }
        }
    }
}

当执行 _components[c](app)System.NullReferenceException: 'Object reference not set to an instance of an object.'

_components[0] 不为 null, 是内部,当 _components[0]() 执行 Func<RequestDelegate, RequestDelegate> 委托时,才报错

image

yiyungent commented 3 years ago

猜测,ApplicationBuilder 还是需要 IServiceProvider ,可能需要什么 Service,

但奇怪的是,之前做过的实验, IPluginFinder 却可以正常从 MiddlewareInvokeAsync 方法上,通过依赖注入获取