ServiceStack: Adding routes dynamically

Deadly 提交于 2019-12-06 03:55:57

问题


I have not tried this yet, but I would like each module (Silverlight) to register its own routes, rather then adding it in application start.

Can routes be added to AppHost after application start, or do they all have to be immediatelly registered during Configure step?

I am thinking to scan all assemblies at the startup and provide AppHost with all assemblies that implement service stack services, but let each module add its own routes (have not figured out yet exact mechanism.

Before I go down this route, need to know if it is possible to add routes after the Configure step.


回答1:


All configuration and registration in ServiceStack should be done within the AppHost.Configure() method and remain immutable thereafter.

If you want to encapsulate registrations of routes in a module than package it as a Plugin and register them manually on IPlugin.Register(IAppHost).

Here are some different ways to register routes:

public class MyModule : IPlugin
{
    public void Register(IAppHost appHost)
    {
        appHost.Routes.Add<MyRequestDto>("/myservice", "POST PUT");

        appHost.Routes.Add(typeof(MyRequestDto2), "/myservice2", "GET");

        appHost.RegisterService(typeof(MyService), "/myservice3"); 
    }
}

Then inside your AppHost.Configure you would register the Plugin, e.g:

Plugins.Add(new MyModule());


来源:https://stackoverflow.com/questions/16245203/servicestack-adding-routes-dynamically

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