Ivan Koshelev
Ivan Koshelev

Reputation: 4260

Is there a way to invoke route manually in ServiceStack, using just a string that would otherwise be its URL?

I have a DTO like this in ServiceStack

[Route("/skillslist/{TaskTypeId*}", WebMethods.Get)]
public class GetSkillsList : IReturn<List<SkillDto>>
{
    public long? TaskTypeId { get; set; }
}

and a method on a service like this

public List<SkillDto> Get(GetSkillsList request){...}

Inside another unrelated method (on another service in the same assembly, same host), I want to trigger the above route and get its result, when i only have a ready route string like "/skillslist/5".

Could anyone please tell me how to do it?

Upvotes: 3

Views: 371

Answers (1)

mythz
mythz

Reputation: 143284

Services in ServiceStack are essentially just normal auto-wired classes (with the current RequestContext injected). From inside any Service you can all another service using base.ResolveService<TService>, e.g:

public object Any(AnotherRequest request)
{
    using (var service = base.ResolveService<GetSkillsService>())
    {
        List<SkillDto> results = service.Get(new GetSkillsList { ... });
    }
} 

Note: this is just a standard C# method call, i.e. it just resolves the Service from the IOC and executes it, so there's no performance penalty as it doesn't do any HTTP or DTO de/serialization, etc.

And from outside of ServiceStack (e.g. in MVC) you can call a Service with AppHostBase.ResolveService, e.g:

using (var service = AppHostBase.ResolveService<GetSkillsService>(HttpContext.Current))
{
    List<SkillDto> results = service.Get(new GetSkillsList { ... });
}

Executing a service with just the path info

1) Get the IRestPath with:

var controller = EndpointHost.Config.ServiceController;
var restPath = controller.GetRestPathForRequest("GET","/skillslist/5");

2) Create a new instance with:

var queryString = new Dictionary<string,string>();
var request = restPath.CreateRequest("/skillslist/5", queryString, null); 

3) Then execute it with:

var response = controller.Execute(request, base.RequestContext);

Upvotes: 5

Related Questions