top of page

Microsoft .NET API Startup and SDK integration

Updated: Jul 23, 2023

Step 1: Update appsettings.json

  1. Update your appsettings.json file with the necessary configurations for RelFast:


{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "RelFastOauthOptions": {
    "AuthorityTokenEndpoint": "https://relfastidp.convolute.ai/connect/token",
    "ClientId": "YOUR_RELFAST_CLIENT_ID_ENV_VARIABLE",
    "ClientSecret": "YOUR_RELFAST_CLIENT_SECRET_ENV_VARIABLE"
  },
  "RelFastOptions": {
    "EnvironmentId": "YOUR_RELFAST_ENVIRONMENT_ID",
    "ProjectId": "YOUR_RELFAST_PROJECT_ID",
    "CoreApiEndpoint": "https://relfastapi.convolute.ai",
    "ApplicationId": "YOUR_RELFAST_APPLICATION_ID"
  },
  "RelFastConfigurationOptions": {
    "ConfigurationId": "YOUR_RELFAST_CONFIGURATION_ID",
    "CacheIntervalInMilliSeconds": 0,
    "EnableCaching": false,
    "FallBack": 1
  }
}

Step 2: Install NuGet Packages

Add the following NuGet packages to your .NET project:

  • RelFast.SDK

  • RelFast.SDK.OAuth

Step 3: Create an Extension Method for RelFastClient

  1. Create an extension method to bootstrap the RelFastClient in your project. Add the following code to ServiceCollectionExtensions.cs:


using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using RelFast.SDK.Abstract.Configuration;
using RelFast.SDK.Core.Configuration;
using RelFast.SDK.OAuth.Concrete;
using RelFast.SDK.OAuth.Model;
using System.Net.Http;

namespace YourProjectNamespace
{
    public static class ServiceCollectionExtensions
    {
        public static void BootStrapRelFastClient(this IServiceCollection services, IConfiguration configuration)
        {
            // Create a http client
            var client = new HttpClient();
            // Create an in-memory cache. This is required to store information for up to 30 seconds.
            var cache = new MemoryCache(new MemoryCacheOptions());

            // Create the RelFastOauthOptions
            var relFastOauthOptions = new RelFastOauthOptions();
            configuration.GetSection("RelFastOauthOptions").Bind(relFastOauthOptions);
            services.AddSingleton(Options.Create(relFastOauthOptions));

            // Create the RelFastOptions
            var relFastOptions = new RelFastOptions();
            configuration.GetSection("RelFastOptions").Bind(relFastOptions);
            services.AddSingleton(Options.Create(relFastOptions));

            // Create the RelFastClient
            services.AddSingleton<IRelFastClient>(sp =>
                new RelFastClient(sp.GetRequiredService<IOptions<RelFastOptions>>(),
                                  new OAuthService(cache, sp.GetRequiredService<IOptions<RelFastOauthOptions>>(), client)));
        }
    }
}

Step 4: Use RelFastClient in Startup

  1. Modify your Program.cs to use the RelFastClient and RelFastConfigurationOptions from appsettings.json. Add the following code:


using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using YourProjectNamespace.FlagOptions;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Bootstrap the RelFastClient
builder.Services.BootStrapRelFastClient(builder.Configuration);

// Configure options for DI. Pass the type based on what is configured in appsettings.json.
builder.Services.Configure<RelFastConfigurationOptions<int>>(builder.Configuration.GetSection("RelFastConfigurationOptions"));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Step 5: Use RelFastConfigurationOptions in Your Code

  1. Now you can use the RelFastConfigurationOptions in your controllers or other classes like this:


using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using YourProjectNamespace.FlagOptions;
using RelFast.SDK.Abstract.Configuration;

namespace YourProjectNamespace.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class SampleController : ControllerBase
    {
        private readonly IRelFastClient _relFastClient;
        private readonly RelFastConfigurationOptions<int> _configurationOptions;

        public SampleController(IRelFastClient relFastClient, IOptions<RelFastConfigurationOptions<int>> configurationOptions)
        {
            _relFastClient = relFastClient;
            _configurationOptions = configurationOptions.Value;
        }

        [HttpGet]
        public IActionResult Get()
        {
            // Retrieve the configuration value using the RelFastClient
            var configurationValue = _relFastClient.GetConfigurationValueAsync<int>(_configurationOptions).Result;

            // Your logic here...
            return Ok(configurationValue);
        }
    }
}

Now you have successfully integrated the RelFast SDK into your .NET application, and you can use it to fetch configurations from the RelFast service. Remember to replace the placeholders (e.g., YOUR_RELFAST_CLIENT_ID_ENV_VARIABLE) with actual values from your environment.



Recent Posts

See All

Application

In the context of the example(Insurance company) provided, an "application" in Relfast refers to a logical grouping of related...

Create a SDK client

To create a new client ID and secret to be used as part of SDK calls in the RelFast portal, follow the step-by-step instructions below....

Secret rotation

To create a configuration that is a secret and use RelFast to easily rotate it, follow the step-by-step instructions below. Please ensure...

bottom of page