Custom location for license file

From FlexRule Wiki
Jump to: navigation, search

Relocating License File

Default license file name is flexrule.license.lic which will be loaded during initialization of the framework. There are also some scenarios where you may need to relocate the license file (e.g., loading license from other path or loading the license from an satellite assembly).

  • Hosting rule service (i.e. IIS, WCF, WebAPI, etc.)
  • Integrating into a test runner (i.e., nunit test, ms test, etc.)

In such cases, you can simply implement interface named ILicenseProvider. This interface allows you to return the content of a valid license during the initialization of the license.

LicenseProvider

After implementing interface ILicenseProvider, then you need to mark the assembly with your own custom implementation using LicenseProviderAttribute. When you apply this pattern, you cannot have more than one LicenseProvider per assembly.

  1. // YOUR_CUSTOM_TYPE implements ILicenseProvider
  2. [assembly: LicenseProvider(typeof(YOUR_CUSTOM_TYPE))]


Embedded License

To write a custom licence provider you just need to implement one interface called ILicenseProvider.

  1. [assembly: LicenseProvider(typeof(CustomLicenseProvider))]
  2. namespace MyService
  3. {
  4.      public class CustomLicenseProvider : ILicenseProvider
  5.     {
  6.         public string ReadLicense()
  7.         {
  8.             // return your license context, shown below as a string, copy and paste from your license file.
  9.             return "xxxxxxxxxxx";
  10.         }
  11.     }
  12. }

Writing Custom Provider

To write a custom licence provider you just need to implement one interface called ILicenseProvider.

  1. [assembly: LicenseProvider(typeof(CustomLicenseProvider))]
  2. namespace MyService
  3. {
  4.      public class CustomLicenseProvider : ILicenseProvider
  5.     {
  6.         public string ReadLicense()
  7.         {
  8.             // Any location you need to read as a file
  9.             var pathToLicense=@"C:\Program Files (x86)\Pliant Framework\FlexRule\Framework\mylicense.lic"
  10.             var licenseKey = File.ReadAllText(pathToLicense);
  11.             // Or for example you can read the content from other places (e.g., assembly resource).
  12.             return licenseKey;
  13.         }
  14.     }
  15. }

Unit Testing

When you write a unit test for your project, the test runner will not be able to locate the licence file, so you need to embed a custom licence provider and read your licence content as explained in here. You can also have a look at how to write a custom license provider for unit testing.