Tom
Tom

Reputation: 16236

ServiceStack How to call my service from code

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?

Upvotes: 18

Views: 4307

Answers (2)

jklemmack
jklemmack

Reputation: 3636

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());

Upvotes: 4

Tom
Tom

Reputation: 16236

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)

Upvotes: 19

Related Questions