如何在自托管的owin服务器中动态创建Web API(REST API)? [英] How to dynamically create Web APIs (REST apis) in selfhosted owin server?

查看:91
本文介绍了如何在自托管的owin服务器中动态创建Web API(REST API)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种动态生成Web api并在运行时添加它们的方法(在初始化自托管服务器之后).

I'm looking for a way to dynamically generate web apis and add them at runtime (after selfhosted server has been initialized).

基本上,该想法是:1.创建一个为静态/已经定义的控制器(Web API)提供服务的自托管主机服务器->这部分是可以的

Basically the idea would be to: 1. Create a selfhost owin server serving static/already defined controllers (web apis) -> this part is ok

  1. 稍后,我想动态生成一个新的控制器并将其以某种方式添加到服务器,以便客户端可以向其发送请求.->有办法吗?我知道我可以动态构建一个控制器并将其添加到服务器,然后再对其进行初始化以服务现有的Web api(使用CustomAssemblyResolver).

  1. At a later time, I want to dynamically generate a new controller and add it somehow to the server so that client can send request to it. -> is there a way to do that? I know I can dynamically build a controller and add it to the server BEFORE it is initialized to serves existing web apis (using CustomAssemblyResolver).

现在可能需要更新现有控制器.我想重新生成一个现有的控制器并更新服务器以使用新的定义(可能是参数更改,api名称更改等).有什么办法吗?我们可以在不停止所有控制器的情况下回收控制器吗?如果以某种方式可以支持此操作,是否表示该服务将在一段时间内不可用(直到更新完成).

Now existing controller may need to be updated. I would like to re-generate an existing controller and update the server to use the new definition (maybe parameter change, name of apis changed, etc.) Any way to do that? Can we recycle a controller without stopping all the controllers? If somehow this can be supported, do it mean the service will not be available for some time (until the update is done).

理想情况下,它将像IIS中托管的Web服务一样工作.如果Web服务定义在2个请求之间更改.第一个请求转到旧定义,第二个请求透明地定向到新定义.服务不会中断.

Ideally it would work like web service hosted in IIS. If web service definition change between 2 requests. 1st request go to old definition and 2nd request is transparently directed to new definition. There is no interruption of service.

有什么想法吗?

预先感谢

推荐答案

找到了解决方案.如果有人在寻找这个,我需要覆盖DefaultHttpControllerSelector.这是一篇关于该主题的非常不错的文章:

Found the solution for it. In case somebody else is looking for this, I need to overwrite the DefaultHttpControllerSelector. Here is a very nice article on the subject: link So basically for my use case mentioned above, I need to create a new AppDomain, start my service in it, and load my assemblies dynamically at runtime. I finally need to overwrite the DefaultHttpControllerSelector to catch the request. When the request arrive, it have then control on which controller I want to use. There I can update the controller dynamically by loading a new assembly, etc. Main thing to be careful on is that this is executed for each request so it could easily impact performance and memory. So I will implement my own caching of controller.

这篇关于如何在自托管的owin服务器中动态创建Web API(REST API)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆