18

how can i call my own service?

I have a service that use other services to compose information.

I want to call other services within the code of this service.

How can I do that?

Tom
  • 15,781
  • 14
  • 69
  • 111
  • Borrowed this question from chat room, I think it is useful to document it for others. – Tom Oct 10 '12 at 02:38

2 Answers2

19

There is a base method called base.ResolveService<TMyService>() which just resolves your autowired service from the IOC and injects the current request context

So just call:

using (var service = base.ResolveService<MyService>()) { 
    service.Post(new MyRequest()); 
} 

You can also call a Service with just a Request DTO which will also execute the Services Global Request Filters:

base.ExecuteRequest(new MyRequest());

This is just a wrapper around ServiceController which can be called statically:

HostContext.ServiceController.Execute(new MyRequest(), base.Request)
mythz
  • 141,670
  • 29
  • 246
  • 390
Tom
  • 15,781
  • 14
  • 69
  • 111
  • How do I call this from my ASP.NET MVC Controller? Since my Controller does not inherit from `ServiceStack.ServiceInterface`. – Rosdi Kasim Nov 14 '13 at 14:29
  • 1
    @RosdiKasim, here is a link to mythz's answer about a similar question http://stackoverflow.com/questions/10571450/should-servicestack-be-the-service-layer-in-an-mvc-application-or-should-it-call – Tom Nov 15 '13 at 08:49
  • Hi thanks Tom!.. that link is really helpful.. appreciate the help. – Rosdi Kasim Nov 15 '13 at 10:49
  • HI @mythz is there a way to do this that also runs the fluent validation for the request? thanks – richardwhatever Jan 16 '15 at 13:13
  • 1
    Does this still apply with "new style" services... I can't see `base.ExecuteRequest` on them? – Chris Apr 10 '15 at 14:11
  • Using base.ExecuteRequest(new MyRequest());, can we define if we want the override OnGet/OnPost to be executed ? And at the same time, how do we get the returned response from the internally called methods ? Or is there a way i can find the corresponding IReturn Type, so i can use using (var service = base.ResolveService()) - then i could use the service.Post and service.Get methods as required. Thanks – Jeffrey Holmes Nov 15 '17 at 15:11
4

Jumping in a bit late, since this popped up on a search engine. The new way as of about ServiceStack v4.5 is to use ServiceGateway. Every SS Service now has a Gateway property which can be executed against:

var response = this.Gateway.Send(new MyRequest());
jklemmack
  • 3,518
  • 3
  • 30
  • 56