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.

When you use the default implementation of LicenseProvider provided by LicFileLicenseProvider, the license file is formatted in the following manner:

  • 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:

    Namespace1.Class1.LIC

  • 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

  1. Apply a LicenseProviderAttribute to the class.

  2. Call Validate or IsValid in the constructor.

  3. 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.

Example

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
using System;
using System.ComponentModel;
using System.Windows.Forms;


// Adds the LicenseProviderAttribute to the control.
[LicenseProvider(typeof(LicFileLicenseProvider))]
public class MyControl : Control 
{

   // Creates a new, null license. 
   private License license = null;

   public MyControl () 
   {

      // Adds Validate to the control's constructor.
      license = LicenseManager.Validate(typeof(MyControl), this);

      // Insert code to perform other instance creation tasks here.
   }

   protected override void Dispose(bool disposing) 
   {
      if(disposing)
      {
         if (license != null) 
         {
            license.Dispose();
            license = null;
         }
      }
   }

}
// Adds the LicenseProviderAttribute to the control.

[LicenseProvider(LicFileLicenseProvider::typeid)]
public ref class MyControl: public Control
{
   // Creates a new, null license. 
private:
   License^ license;

public:
   MyControl()
   {

      // Adds Validate to the control's constructor.
      license = LicenseManager::Validate( MyControl::typeid, this );

      // Insert code to perform other instance creation tasks here.
   }

public:
   ~MyControl()
   {
      if ( license != nullptr )
      {
         delete license;
         license = nullptr;
      }
   }
};
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
using System;
using System.ComponentModel;
using System.Web.UI;

// Adds the LicenseProviderAttribute to the control. 
public class MyServerControl : Control 
{
    // Creates a new, null license. 
    private License license = null;

    public MyServerControl() 
    {
        // Adds Validate to the control's constructor.
        license = LicenseManager.Validate(typeof(MyServerControl), this);

        // Insert code to perform other instance creation tasks here.
    }

    public override void Dispose() 
    {      
        if (license != null) 
        {
            license.Dispose();
            license = null;
        }

        base.Dispose();
    }    
}

See Also

Reference

LicenseProviderAttribute

LicenseProvider

Other Resources

Component Authoring