CoolCat icon indicating copy to clipboard operation
CoolCat copied to clipboard

提供一个非 Controller/Action 的类库,宿主程序根据其方法签名提供 WebApi

Open QJesus opened this issue 5 years ago • 3 comments

如果是一个类库 dll,里面有方法,方法就是需要提供的 api 接口。 框架如何实现,按照其方法签名自动提供 api 接口?

QJesus avatar Oct 24 '19 04:10 QJesus

@QJesus 你的意思是dll里面放的是Controller/Action的代码,你希望主程序能自动加载这些Controller/Action。 之前版本.NET Core里面可以使用services.ControllerAsService()这个方法来加载,但是前提是你的dll不是加载在Custom Load Context中,应该加载在Default Load Context中。不知道这样是否解答了你的问题,如果还有问题,可留言

lamondlu avatar Oct 24 '19 06:10 lamondlu

类库 dll 中不是 Controller/Action 代码,仅为普通方法,比如下面的 Class1.HelloWorld 方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DemoPlugin
{
    public class Class1
    {
        public dynamic HelloWorld(int times)
        {
            var data = Enumerable.Range(0, times).Select(o => "hello world").ToArray();
            return data;
        }
    }
}

编译 DemoPlugin.Class1 后打包上传到宿主程序,点击启用,宿主解析该 dll 的方法后自动提供和方法签名一致的 api。

宿主程序应该如何实现?

QJesus avatar Oct 24 '19 16:10 QJesus

类库dll中不是控制器/动作代码,只有普通方法,有些下面的Class1.HelloWorld方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DemoPlugin
{
    public class Class1
    {
        public dynamic HelloWorld(int times)
        {
            var data = Enumerable.Range(0, times).Select(o => "hello world").ToArray();
            return data;
        }
    }
}

编译DemoPlugin.Class1后打包上传到宿主程序,单击启用,托管解析该dll的方法后自动提供和方法签名一致的api。

主机程序应该如何实现?

可参考abp动态api代理

laosandegudai avatar Sep 02 '20 05:09 laosandegudai