whiskytangofoxtrot
whiskytangofoxtrot

Reputation: 987

How do I add code that was previously in Startup.cs -->Configure method when using Azure Functions v3 in .NET 5 isolated process

I am working with a new C# Function App using the .NET 5 dotnet-isolated where the examples only show the Program.cs and there is no Startup.cs class. I have an issue where I need to register a SyncFusion license, which I typically do in the Startup.cs -->Configure Method by using this line;

Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("...");

But I do not know where to add this line in the new approach in a .NET 5 isolated process function where there is no Startup.cs file.

Can anyone provide an example of where I would add things I would typically put in a Startup.cs --> Configure method?

Is there some extension method you can daisy chain similar to the...

.ConfigureServices(services =>
{
     ...
     ...
     ...
 }

... used with the HostBuilder in the .NET 5 isolated process version of the program.cs file?

Upvotes: 1

Views: 1054

Answers (1)

Kzryzstof
Kzryzstof

Reputation: 8402

Option 1

I would follow the existing pattern and create an extension method to IServiceCollection in the appropriate project, like this:

public static class ServiceCollectionExtensions
{
    public static void AddLicense(this IServiceCollection services)
    {
        Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("...");
    }
}

Then, in the program.cs file of your Function project, you can call it like that:

static Task Main(string args[])
{
    var hostBuilder = new HostBuilder()
          ...
          .ConfigureServices(services => 
          {
              services.AddLicense();
          });
}

Option 2

Now, it is possible you may need more information to register the actual license. Let's say it is a serial number; it might not be a good idea to hard code it. Instead, we put it in a configuration (which could be even be linked to a key vault for better security).

First, let's define our configuration class:

public class LicenseConfiguration
{
    // I chose a serial number by it could be something else.
    public string SerialNumber { get; set;}
}

Now, you could define an IHostedService that would run every time your function starts:

 public class LicenseRegistrationService : IHostedService
    {
        private readonly LicenseConfiguration _licenseConfiguration;

        public LicenseRegistrationService(IOptions<LicenseConfiguration> licenseConfiguration) 
        {
            _licenseConfiguration = licenseConfiguration.Value;
        }
    
        public Task StartAsync(CancellationToken cancellationToken)
        { 
 Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(_licenseConfiguration.SerialNumber);
              return Task.CompletedTask;
        }
    
        public Task StopAsync(CancellationToken cancellationToken)
        { 
              return Task.CompletedTask;
        }
    }

Now let's hook everything up in the Program class:

   static Task Main(string args[])
    {
        var hostBuilder = new HostBuilder()
        ...
        .ConfigureServices(services => 
        {
            // Register you hosted service 
            services.AddHostedService<LicenseRegistrationService >();
    
            // Bind your configuration parameters  
            services.AddOptions<LicenseConfiguration>()
                    .Configure<IConfiguration>((settings, configuration) => 
                    { 
   configuration.GetSection(nameof(LicenseConfiguration)).Bind(settings); 
                    });
        });
    }

In your local.settings.json file, you can put the proper configuration to validate everything locally:

{
    "IsEncrypted": false,
    "Values": {
        ...
        "LicenseConfiguration__SerialNumber": "this-is-your-serial-number"
    }
}

Upvotes: 1

Related Questions