Hello, I'm migrating a .NET Framework 4.8 web API to .NET6. For creating PDFs I'm using an external library called ABCpdf. It needs a license key to work, which should be initialized only once, when the app is starting.
Method which is installing/getting the license:
private void InstallAbcPdfLicense()
{
string license = ServiceLocator.Current.GetInstance<IAbcPdfOptions>().AbcPdfLicense;
if (string.IsNullOrEmpty(license))
{
logger.LogWarning("Unable to find the license for the .pdf generator in web.config. Pdf generation will fail.");
}
if (!XSettings.InstallLicense(license))
{
logger.LogWarning("Unable to install the license for the .pdf generator. Pdf generation will fail. " + XSettings.LicenseDescription ?? string.Empty);
}
}
So far, when the app has been running on .NET Framework 4.8, that method was initialized in Global.asax file, in Application_Start function:
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
InitializeViewEngine();
InitAppInsights();
InitWhitelistedIps();
InstallAbcPdfLicense();
SetMinThreads();
}
And it worked fine. But now that I have to move everything to Startup.cs file, I have a question what is the best solution to make sure that it is executed only once, when the app starts?
What I have tried:
So far the best solution I have came up with is initializing the
InstallAbcPdfLicense in Startup file's Configure method by using lifetime
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IUrlRewriteService urlRewriteService, IHostApplicationLifetime lifetime)
{
lifetime.ApplicationStarted.Register(InstallAbcPdfLicense);
}
But is there better approach?