If you are applying a LicenseProviderAttribute to your .Net component or control class, and if you would like to use a custom license provider for it, then you might find the below information useful.
As a first step, derive your custom license provider class from the System.ComponentModel.LicenseProvider base class and implement its GetLicense() method.
class MyLicenseProvider : System.ComponentModel.LicenseProvider { public override License GetLicense(LicenseContext context, Type type, Object instance, bool allowExceptions) { string str = type.Assembly.CodeBase; if (type.AssemblyQualifiedName == "<UnLicensed Control>") { // Use the below mechanism to return failures when required if (allowExceptions) throw new LicenseException(type, instance, "Well, you are using an unlicensed control, Aren't you?"); else return null; } else { // We can check for valid licenses here. // As part of the setup of our licensed control, we can associate some registry info or // hard disk info with its type and do the verification/validation of that information here. // For this example we just return a default MyLicense object without doing any checks. MyLicense lic = new MyLicense(); return lic; } } }
The LicenseProvider.GetLicense() method returns a System.ComponentModel.License object. However, System.ComponentModel.License is an abstract class and thus we need to implement our own License class to be able to create its objects. For this, we need to derive a class from the System.ComponentModel.License base class and implement the License.LicenseKey get property and the Dispose() methods on it.
class MyLicense : System.ComponentModel.License { public MyLicense() { } public override void Dispose() { } public override string LicenseKey { get { return "<Appropriate License String Here>"; } } }
Once we have these in place, all that we need to do is, just use the MyLicenseProvider on the .Net component or control as shown below:
[LicenseProvider(typeof(MyLicenseProvider))] public class MyLicensedControl : System.Windows.Forms.xxxxx { ... }
Now whenever we call LicenseManager.Validate() for the MyLicensedControl, .Net framework would route that call to our MyLicenseProvider.GetLicense() method, which returns a MyLicense object upon success and null (or an exception) upon failure. The returned MyLicense object can be released with the MyLicense.Dispose() call anytime once its use is over.
[LicenseProvider(typeof(MyLicenseProvider))] public class MyLicensedControl : System.Windows.Forms.xxxxx { private MyLicense license = null; public MyLicensedControl() { license = LicenseManager.Validate(typeof(MyLicensedControl), this); } ~MyLicensedControl() { if(license != null) { license.Dispose(); license = null; } } }
By
Palem Gopalakrishna