在ServiceStack动态地创建业务和服务 [英] Dynamically creating operations and services in ServiceStack

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

问题描述

我的工作,需要我搜集的命令,其中我有超过200的列表,并创建一个经营和服务为他们每个人的ServiceStack项目。基本上我想提出一个指挥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?

我AP preciate给予任何帮助,

I appreciate any help given,

迈克

推荐答案

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

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的自动查询实施为例如何<一href=\"https://github.com/ServiceStack/ServiceStack/blob/a522ff333b898005c24af32c1119a3b9fdac0d52/src/ServiceStack.Server/AutoQueryFeature.cs#L175-L176\"相对=nofollow>生成和动态注册动态服务。自动查询查找请求DTO是实现 IQUERY&LT;&GT; 接口,然后生成并注册一个使用它的新服务的实施

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.

它在本质上以同样的方式定义服务类的地方,而不是使用注册后,你可以用code代动态创建,然后可以用注册的服务实现现有的服务类型:

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还支持<一个href=\"https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.Endpoints.Tests/RuntimeAttributeTests.cs\"相对=nofollow>在运行时动态注册的属性,例如:

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

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

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