Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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:
C#
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
C#
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IUrlRewriteService urlRewriteService, IHostApplicationLifetime lifetime)
{
  //other methods
  lifetime.ApplicationStarted.Register(InstallAbcPdfLicense);
}


But is there better approach?
Posted
Comments
PIEBALDconsult 18-Aug-22 15:59pm    
Will a static constructor work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900