ServiceStack: Adding routes dynamically

2019-04-08 17:39发布

问题:

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());