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 some scenarios 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...
  • Integrating into a test runner (nunit test, ms test...)

In this case you simply can implement interface named ILicenseProvider. This interface allows you to return the content of a valid license during 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 applying this pattern make sure you cannot have more than on 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 as a string bellow, copt-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 runnier will not be able to locate the licence file. So you need to embed custom licence provider and read your licence content as explained in here. You can also have a look at how to wrote custom license provider for unit testing.