ServiceStack:动态添加路由 [英] ServiceStack: Adding routes dynamically

查看:41
本文介绍了ServiceStack:动态添加路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有尝试过,但我希望每个模块 (Silverlight) 注册自己的路由,而不是在应用程序启动中添加它.

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.

是否可以在应用程序启动后将路由添加到 AppHost,还是必须在配置步骤中立即注册?

我想在启动时扫描所有程序集,并将所有实现服务堆栈服务的程序集提供给AppHost,但让每个模块添加自己的路由(还没有想出确切的机制.

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.

推荐答案

ServiceStack 中的所有配置和注册都应该是在 AppHost.Configure() 方法中完成,此后保持不变.

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

如果你想在一个模块中封装路由的注册而不是把它打包成一个插件 并在 IPlugin.Register(IAppHost) 上手动注册它们.

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"); 
    }
}

然后在您的 AppHost.Configure 中注册插件,例如:

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

Plugins.Add(new MyModule());

这篇关于ServiceStack:动态添加路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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