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

  • Question

  • Hi,

    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

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

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

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

    Wednesday, November 9, 2016 5:23 PM

Answers

  • 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](http://www.strathweb.com/2013/08/customizing-controller-discovery-in-asp-net-web-api/)
    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.
    • Proposed as answer by DotNet Wang Sunday, November 13, 2016 2:56 AM
    • Marked as answer by dtf017 Friday, November 25, 2016 1:29 PM
    Thursday, November 10, 2016 5:12 PM

All replies

  • 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](http://www.strathweb.com/2013/08/customizing-controller-discovery-in-asp-net-web-api/)
    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.
    • Proposed as answer by DotNet Wang Sunday, November 13, 2016 2:56 AM
    • Marked as answer by dtf017 Friday, November 25, 2016 1:29 PM
    Thursday, November 10, 2016 5:12 PM
  • Hi dtf017,

    Thank you for sharing your solution. Please mark you solution as answer. It will help other members who encountered similar issue.

    Best Regards,
    Li Wang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, November 13, 2016 2:58 AM