Feeling really dumb

I have several sites which serve up data for the most part. I want to be able to mix and match pieces of those services within applications which might need data from each. So far so good.

Am I reading the Service Gateway correct in being able to help with this? If so for the life of me I don’t understand how to use it. Are there examples for this that I am missing?



It should be able to help yes, but understand why it can seem confusing, something we can look at improving in our docs/videos.

Likely, you will want to control the logic of your own CustomServiceGatewayFactory so you can decide how and where the information from your other services can fetch your data. In our docs about the ServiceGateway, we have an example of this.

public class CustomServiceGatewayFactory : ServiceGatewayFactoryBase
    public override IServiceGateway GetGateway(Type requestType)
        var isLocal = HostContext.Metadata.RequestTypes.Contains(requestType);
        var gateway = isLocal
            ? (IServiceGateway)base.localGateway
            : new JsonServiceClient(alternativeBaseUrl);
        return gateway;

This looks to see if the request can resolved locally, or if it needs to use a JsonServiceClient to fetch the data remotely.

For the application to compose data from different services, it will need access to the shared Request and Response Data Transfer Objects (DTOs), and from there your CustomServiceGatewayFactory will need to map request types to specific ways to fetch the data from the other services.

This will keep your call sites clean, and you will be able to call Gateway.SendAsync specifying just the specific Request DTO, your CustomServiceGatewayFactory will be able to resolve where the external call should be made and return the data.

You can use more sophisticated forms of Service Discovery with the use of Gateway calls like Consul or others, or just manage it yourself in your CustomServiceGatewayFactory if DNS will be resolved correctly from the server compositing from multiple other services.

There can be a lot of moving parts to something like this, and implementation details depend a lot of specific setups, but I hope this has helped a bit to understand what the Service Gateway does and how it works.

This is exactly what I needed to get me going thanks!

1 Like