How to: License Components and Controls
The .NET Framework provides a licensing model that is identical for all components and controls, including Windows Forms controls and ASP.NET server controls, and is fully compatible with licensing for Microsoft ActiveX® controls.
With licensing, you, as the component or control author, can help protect your intellectual property by verifying that a developer is authorized to use your component or control. This verification is more important at design time, when a developer incorporates your component or control into an application, than at run time. When a developer legally uses your licensed component or control at design time, the developer's application gets a run-time license that the developer can freely distribute.
You have many other levels of licensing support with the licensing model. The model separates the validation logic from the component or control. A license provider grants licenses and performs the validation logic. The provider is a class that derives from System.ComponentModel.LicenseProvider. The steps that you must take to enable licensing are straightforward.
The name of the file must be the fully qualified name, including the namespace, of the class with the file name extension .LIC. For example:
The content of the license file should contain the following text string:
"myClassName is a licensed component."
myClassName is the fully qualified name of the class. For example:
"Namespace1.Class1 is a licensed component."
The following code examples show a Windows Forms control and an ASP.NET server control that implement a simple case of licensing.
To enable licensing for your component or control
Apply a LicenseProviderAttribute to the class.
Call Dispose on any granted license in the finalizer of the class or before the finalizer is called.
The following code examples use the built-in license provider class LicFileLicenseProvider, which enables you to use text license files and mimics the behavior of COM (ActiveX) licensing. More complex licensing scenarios, such as calling an XML Web service to limit the number of instances of a component, require different kinds of license providers.
Imports System Imports System.ComponentModel Imports System.Windows.Forms ' Adds the LicenseProviderAttribute to the control. <LicenseProvider(GetType(LicFileLicenseProvider))> _ Public Class MyControl Inherits Control ' Creates a new, null license. Private license As License = Nothing Public Sub New() ' Adds Validate to the control's constructor. license = LicenseManager.Validate(GetType(MyControl), Me) ' Insert code to perform other instance creation tasks here. End Sub Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If (license IsNot Nothing) Then license.Dispose() license = Nothing End If End If End Sub End Class
Imports System Imports System.ComponentModel Imports System.Web.UI ' Adds the LicenseProviderAttribute to the control. <LicenseProvider(GetType(LicFileLicenseProvider))> Public Class MyControl Inherits Control ' Creates a new, null license. Private license As License Public Sub New() ' Adds Validate to the control's constructor. license = LicenseManager.Validate(GetType(MyControl), Me) ' Insert code to perform other instance creation tasks here. End Sub Public Overrides Sub Dispose() If (license IsNot Nothing) Then license.Dispose() license = Nothing End If MyBase.Dispose() End Sub End Class