How to dynamically create Web APIs (REST apis) in selfhosted owin server?

∥☆過路亽.° 提交于 2020-04-13 08:33:30

问题


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

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

  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).

  2. 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).

  3. 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.

Any ideas?

Thanks in advance


回答1:


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.



来源:https://stackoverflow.com/questions/40522242/how-to-dynamically-create-web-apis-rest-apis-in-selfhosted-owin-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!