gpt4 book ai didi

c# - C# Web Api 中的动态注册 Controller

转载 作者:太空狗 更新时间:2023-10-30 00:49:29 24 4
gpt4 key购买 nike

tl;dr:我正在创建扩展 ApiControllerType(通过反射)。我如何动态注册它(它可以在启动时注册;不需要在运行时执行此操作)。

长话短说:

所以在我的应用程序中我有多个接口(interface),即:

interface IMyInterface
{
MyResponse Hello(MyRequest request);
}

我想要实现的是为每个界面创建 Controller ,应该如下所示:

public class IMyInterfaceController : ApiController
{
public IMyInterface MyInterface { get; set; }

public MyResponse Hello([FromBody] MyRequest request)
{
return MyInterface.Hello(request);
}
}

生成此 Controller 已使用大量 C# 反射完成。我现在想做的是在 /api/{controller}/{action} 下注册这个 Controller 。

现在在 Global.asax 中我得到了这个:

public class MvcApplication : System.Web.HttpApplication
{
private readonly InterfaceReader _reader = new InterfaceReader(); // this class is doing all staff with reflection to create controller class

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);

var controller = _reader.CreateController(new MyImplementation()); // MuImplementation implements IMyInterface
}
}

最佳答案

使用 IHttpControllerFactory 的解决方案

我猜你需要的是一个 Controller 工厂:

public class MyHttpControllerFactory : IHttpControllerFactory
{
private readonly InterfaceReader _reader;
private readonly HttpConfiguration _configuration;

public MyHttpControllerFactory(InterfaceReader reader, HttpConfiguration configuration)
{
_reader = reader;
_configuration = configuration;
}

public IHttpController CreateController(HttpControllerContext controllerContext, string controllerName)
{
if (controllerName == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", controllerContext.Request.RequestUri.AbsolutePath));
}

// Change the line below to whatever suits your needs.
var controller = _reader.CreateController(new MyImplementation());
controllerContext.Controller = controller;
controllerContext.ControllerDescriptor = new HttpControllerDescriptor(configuration, controllerName, controller.GetType());

return controllerContext.Controller;
}

public void ReleaseController(IHttpController controller)
{
// You may want to be able to release the controller as well.
}
}

然后在 Global.asax 中,您需要注册自定义 Controller 工厂:

public class MvcApplication : System.Web.HttpApplication
{
private readonly InterfaceReader _reader = new InterfaceReader(); // this class is doing all staff with reflection to create controller class

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);

GlobalConfiguration.Configuration.ServiceResolver.SetService(typeof(IHttpControllerFactory), new MyHttpControllerFactory(_reader, GlobalConfiguration.Configuration));
}
}

使用 IHttpControllerActivator 的解决方案

如果您使用 Web Api 2,那么解决方案是使用 IDependencyResolverIHttpControllerActivator 而不是工厂。我想 IHttpControllerActivator 对您来说是更好的选择。

public class MyServiceActivator : IHttpControllerActivator
{
private readonly InterfaceReader _reader;
private readonly HttpConfiguration _configuration;

public MyServiceActivator(InterfaceReader reader, HttpConfiguration configuration)
{
_reader = reader;
_configuration = configuration;
}

public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
// Change the line below to whatever suits your needs.
var controller = _reader.CreateController(new MyImplementation());
return controller;
}
}

然后在 Global.asax 中,您需要注册自定义激活器:

public class MvcApplication : System.Web.HttpApplication
{
// this class is doing all staff with reflection to create controller class
private readonly InterfaceReader _reader = new InterfaceReader();

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);

HttpConfiguration config = GlobalConfiguration.Configuration;
config.Services.Replace(typeof(IHttpControllerActivator), new MyServiceActivator(_reader, config));
}
}

希望对您有所帮助。

关于c# - C# Web Api 中的动态注册 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37813884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com