Cannot resolve service with Gateway

I am new to ServiceStack and working on modularizing my services. I basically created a few plugins with services in them. I can access the different services from each other using something like:

var service = base.ResolveService<BinariesConfigService>();
var test =service.Get(new GetBinariesConfigById {Id = 1});

However if I try to use this:

var test = Gateway.Send<BinariesConfig>(new GetBinariesConfigById{Id=1});

I get null errors. It seems InProcessServiceGateway is initiated and set irequest req to null. So I am a little stuck as to how to use the service gateway. Any help would be great. Once I get this working I want to work on service discovery next. Or maybe that is my issue? I need to do that… Anyway thanks in advance!

Please include the full StackTrace when reporting an Exception.

Where are you trying to use the Gateway in your Service? Also your Services should have IReturn<T> markers, e.g:

public class GetBinariesConfigById : IReturn<BinariesConfig> { ... }

So you should not need to specify it on the call-site, e.g.:

var test = Gateway.Send(new GetBinariesConfigById { Id = 1 });

Hi sorry for the slow response.
I have a few plugins/services that are added in my web service in the apphost:configure method:
Ex:

Plugins.Add(new ConfigFeature());
Plugins.Add(new DBFeature());

They load in without errors and I can see/test them through the swagger interface.

The DTO looks like this:

[Route("/binariesconfig/{Id}", "GET")]
public class GetBinariesConfigById :  IGet,IReturn<BinariesConfig>
{
    public ulong Id { get; set; }
}

then in a service in the DBFeature i use it like:

var test = Gateway.Send(new GetBinariesConfigById{Id=1});

When my code hits that line i get:

 System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=ServiceStack
  StackTrace:
   at ServiceStack.InProcessServiceGateway.Send[TResponse](Object requestDto) in C:\git\ServiceStack\src\ServiceStack\InProcessServiceGateway.cs:line 182

This however works:

var service = base.ResolveService<BinariesConfigService>();
var test =service.Get(new GetBinariesConfigById {Id = 1});

I compiled ServiceStack and made a symbols package so I could see what was going on. I am at a loss though. Next step is do look into discovery with redis/consul but I would like to see this work first.

Where are you calling the Gateway from? inside a Service that’s called from HTTP?

For reference the NRE on line 182 is due to:

Which indicates that the Request Context isn’t captured, but its populated from the Request from the Service it’s called from. So the question then becomes why isn’t the Request not populated in your Service, is it being called from HTTP?

The flow looks like this:
App host Swagger page -> Calls DB Feature service (BinariesService )
decorated like so:

public class BinariesService : Service

BinariesService service calls Config service (BinariesConfigService)
decorated like so:

public class BinariesConfigService : Service

its in the BinariesConfigService that this error happens.

OMG I absolutley hated I wasted your time. I was “lazy” and put my test in my service constructor ignoring all common sense. I moved that out and things work great. Your last reply gave me the clue. thank you for your time and I absolutely love this framework. Best money spent!

1 Like