ServiceGateway is null when using BasicAppHost

I use ServiceGateway internally in my services and when testing, the ServiceGateway Instance is always set to null. I tried to register an InProcessServiceGateway but it still returns null. What is the recommended method to set ServiceGateway when testing using the BasicAppHost and using AppHost.Resolve(); to retrieve an instance of the service?

Where is it null? Do you have a StackTrace?

Here’s the stack trace.

System.ArgumentNullException 
HResult=0x80004003
  Message=Value cannot be null.
  Source=ServiceStack
  StackTrace:
   at ServiceStack.ServiceStackHost.GetServiceGateway(IRequest req)
   at ServiceStack.Service.get_Gateway()
   at ServiceInterface.Services.RegistrationService.Post(StartRegistration request) 

And here’s the call that causes it:

var institutions = this.Gateway.Send<InstitutionsResponse>(new Institutions { EmailDomain = request.EmailAddress.GetDomain() });

How are you calling the Service that uses the Gateway? Is it populated with an IRequest instance? If not you can pass in BasicRequest() for an empty IRequest context.

Otherwise you can try overriding GetServiceGateway in a AppHost that inherits BasicAppHost:

        public override IServiceGateway GetServiceGateway(IRequest req) => 
            base.GetServiceGateway(req ?? new BasicRequest());
1 Like

Works perfectly. The answer was to create an AppHost that inherited from the BasicAppHost. Then setting a mocked request does the right thing.