1. Home
  2. Getting Started
  3. Licensing
  4. .NET Project
  5. Specify a Custom Location for a License File

Specify a Custom Location for a License File

◷ Reading Time: 2 minutes

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.

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

Embedded License

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

[assembly: LicenseProvider(typeof(CustomLicenseProvider))]
namespace MyService
{
     public class CustomLicenseProvider : ILicenseProvider
    {
        public string ReadLicense()
        {
            // return your license context, shown below as a string, copy and paste from your license file.
            return "xxxxxxxxxxx";
        }
    }
}

Writing Custom Provider

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

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

Unit Testing

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

Updated on August 8, 2019

Was this article helpful?

Related Articles