PluginCore icon indicating copy to clipboard operation
PluginCore copied to clipboard

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

Open yiyungent opened this issue 2 years ago • 1 comments

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 avatar Aug 22 '21 13:08 yiyungent

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

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

yiyungent avatar Aug 22 '21 13:08 yiyungent