Injecting gateways for use within a servicestack service

I’d like to use a few gateways within servicestack.

For example, let’s say I want to use the Stripe gateway (one of the demos) and inject it so it’s accessible within a servicestack service.

How would I register the gateway and use it within the service?

Taking the Stripe gateway as an example, would it be better to instantiate an instance per request or have it accessible globally?

And if I want to use a third-party client/gateway (ex. https://github.com/ipinfo/csharp ) is the procedure the same?

For this third-party gateway, the code to connect looks like this:

// initializing client
string token = "MY_TOKEN";
IPinfoClient client = new IPinfoClient.Builder()
    .AccessToken(token)
    .Build();

What’s the issue with registering it with the IOC then resolving it like any other dependency?

Sorry I wasn’t clear. I was wondering if there were any special consideration since ServiceStack is using (or was?) funq. I can’t find any documentation specific to funq.

I was going through the doc here:

https://docs.servicestack.net/ioc

And the 2 links are broken:

http://funq.codeplex.com/

http://www.servicestack.net/benchmarks/

Is there support to add/retrieve service implementations by name?

These are the docs for using ServiceStack’s enhanced Funq’s IOC showing different APIs to Register dependencies: ServiceStack's IOC do you have a specific issue with any of the existing APIs?

Only dependencies registered against a type are injected, registering by name would require manual resolution from the IOC which would mean your services would be coupled to the IOC’s concrete implementation instead of being injected.

You also don’t need to use ServiceStack’s IOC and can register your dependencies in ASP .NET’s IOC instead in your AppHost’s .ConfigureServices():

[assembly: HostingStartup(typeof(MyApp.AppHost))]

namespace MyApp;

public class AppHost : AppHostBase, IHostingStartup
{
    public void Configure(IWebHostBuilder builder) => builder
        .ConfigureServices(services => {
            // Configure ASP.NET Core IOC Dependencies
        });

    public AppHost() : base("MyApp", typeof(MyServices).Assembly) {}

    public override void Configure(Funq.Container container)
    {
    }
}

I don’t have any issues with any of the existing APIs.

I’m new to web services, and I’m still getting up to speed on many concepts (part-time).

The external gateways I’m calling are grouped and have multiple backups in each group/cluster.

My use case:

A support employee will call the service, and depending on the customer being serviced, a different external gateway will be called, each customer is assigned to a specific group/cluster.

For now, I moved the group/cluster configuration in my appsettings file and I have this:

using ExternalApiBase;
using ExternalRestApi;

[assembly: HostingStartup(typeof(InternalApi.ConfigureExternalRestApi))]

namespace InternalApi;

public class ConfigureExternalRestApi : IHostingStartup
{
    public void Configure(IWebHostBuilder builder) => builder
        .ConfigureServices((context, services) =>
        {
            var externalClusterConfiguration = new ExternalClusterConfiguration();

            context.Configuration.GetSection(nameof(ExternalClusterConfiguration))
                .Bind(externalClusterConfiguration);

            services.AddSingleton<IExternalRestGatewayClusterList>(new ExternalRestGatewayClusterList(externalClusterConfiguration)
            );
        })
        .ConfigureAppHost(appHost =>
        {
            foreach (var externalRestGatewayClustered in appHost.Resolve<IExternalRestGatewayClusterList>().ExternalRestGatewayClusters)
            {
                externalRestGatewayClustered.ExternalRestGateway.Login();
            }
        });
}

And when a query is received I resolve the gateway list and match to the correct gateway:

public QueryUserResponse Get(QueryUser queryUser)
{
    var gateway = ResolveService<IExternalRestGatewayClusterList>();

    //match queryUser to gateway
}

I will look into the multitenancy documentation and see how you implemented this.

ResolveService<T>() should only be used for resolving ServiceStack Services, TryResolve<T>() can be used to resolve IOC dependencies, however most of the time they should be injected into your Services either via a constructor or injected into a public property using the registered type, e.g:

public class MyServices : Service
{
    public IExternalRestGatewayClusterList ExternalGateway { get; set; }
}

Excellent. It works. I’ll go through all your documentation.

I’ve never used regular WCF or ASP web services before and came straight to ServiceStack.

Understanding how the initial configuration and how dependency injection works is what I’m struggling the most with at the moment.

I guess that most of the service startup, it’s initial configuration via dependency injection, etc., is the same as AspNetCore?

If that’s the case, I guess as a first step going over this documentation would be a good idea:

Let me know if you have any other good resources to suggest for beginners.

Yeah using an IOC to register dependencies is basically prevalent in all .NET Apps now, although for the majority of cases it’s pretty simple as you basically only need to register dependencies with AddSingleton for thread-safe dependencies otherwise use AddTransient to ensure the same instance isn’t being reused, then whatever Type you register it against will be injected into your Services containing public properties of that type.

1 Like