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
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?
I appreciate any help given,
Mike
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") { ... }
}
}
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.
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 also supports dynamically registering attributes at runtime, e.g:
requestDtoType
.AddAttributes(new RouteAttribute("/custom-route"))
.AddAttributes(new RestrictAttribute(RequestAttributes.Json));