在 ServiceStack 中动态创建操作和服务 [英] Dynamically creating operations and services in ServiceStack

查看:13
本文介绍了在 ServiceStack 中动态创建操作和服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 ServiceStack 项目,该项目需要我收集一个命令列表,其中我有 200 多个命令,并为每个命令创建一个操作和服务.本质上,我正在制作一个命令 API,它允许用户在不使用 UI(暴露我的命令)的情况下发送命令.

I'm working on a ServiceStack project that requires me to gather a list of commands, of which I have over 200, and create a operation and service for each of them. Essentially I am making a Commanding API which will allow users to send commands without using the UI (exposing my commands).

我正在尝试做的一个简单的例子:(应用启动)

A simplistic example of what I am trying to do: (Application Start)

Gather all commands (with some exemptions)
for each command
    make an operation and service for that command
    map the commands attributes to the new operation and service

我遇到的问题是操作和服务的创建.我不确定在 ServiceStack 框架中是否支持它以允许动态创建服务和操作,但我真的没有找到这样做的方法.为了澄清起见,动态我的意思是在应用程序启动期间从列表中创建命令,而不是即时创建它们.

The problem I'm running into is the creation of operations and services. I'm not sure if it's supported within the ServiceStack framework to allow dynamic creation of services and operations but I've literally had no luck in finding a way of doing it. For clarification, by dynamic I mean create the commands off of a list during application start, not creating them on the fly.

有人能帮我理解一下吗?

Can someone shed some light for my understanding?

感谢您提供的任何帮助

迈克

推荐答案

委托给多个内部服务的单个服务

我首先考虑是否使用使用 自由通配符的单个服务实现路由接受多种请求类型,然后根据一些参数委托给不同的服务实现,例如:

Single Service that delegates to multiple internal Services

I'd first consider whether using a single Service implementation that uses a liberal wildcard route to accept multiple request types which then delegates to different Service implementations based on some param, e.g:

[Route("/services/{Type}/{PathInfo*})]
public class DynamicService 
{
    public string Type { get; set; }
    public string PathInfo { get; set; }
}

public class DynamicServices : Service
{
    public object Any(DynamicService request)
    {
        if (request.Type == "type1") 
        {
            //Resolve existing Service and call with converted Request DTO
            using (var service = base.ResolveService<Type1Service>())
            {
                return service.Any(request.ConvertTo<Type1Request>());
            }
        }
        else if (request.Type == "type2") { ... }
    }
}

动态生成和注册服务实现

否则,您可以查看 ServiceStack 的 AutoQuery 实现以了解如何动态生成和注册动态服务/a>.AutoQuery 查找实现 IQuery<> 接口的请求 DTO,然后生成并注册使用它的新服务实现.

Dynamically Generating and Registering Service implementations

Otherwise you can look at ServiceStack's AutoQuery implementation for an example on how to generate and register dynamic services on the fly. AutoQuery looks for Request DTO's that implement IQuery<> interface then generates and registers a new Service implementation that uses it.

它本质上与定义的 Service 类的工作方式相同,其中您可以使用代码生成来动态创建可以注册的 Service 实现,而不是使用注册现有的 Service 类型:

It essentially works the same way as defined Service classes where instead of using registering an existing Service type you can use code-generation to dynamically create the Service implementation which can then be registered with:

appHost.RegisterService(serviceType);

ServiceStack 还支持动态注册属性在运行时,例如:

ServiceStack also supports dynamically registering attributes at runtime, e.g:

requestDtoType
    .AddAttributes(new RouteAttribute("/custom-route"))
    .AddAttributes(new RestrictAttribute(RequestAttributes.Json));

这篇关于在 ServiceStack 中动态创建操作和服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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