Gewusst wie: Lizenzieren von Komponenten und Steuerelementen

Aktualisiert: November 2007

.NET Framework bietet ein Lizenzierungsmodell, das für alle Komponenten und Steuerelemente (einschließlich Windows Forms-Steuerelemente und ASP.NET-Serversteuerelemente) identisch und mit der Lizenzierung von Microsoft ActiveX®-Steuerelementen vollständig kompatibel ist.

Die Lizenzierung dient Ihnen als Autor von Komponenten oder Steuerelementen dem Schutz Ihres geistigen Eigentums, indem überprüft wird, ob ein Entwickler zur Verwendung der Komponente oder des Steuerelements autorisiert ist. Diese Überprüfung ist zur Entwurfszeit, wenn die Komponente oder das Steuerelement in eine Anwendung eingebunden wird, von größerer Bedeutung als zur Laufzeit. Wenn ein Entwickler die von Ihnen lizenzierte Komponente oder das Steuerelement zur Entwurfszeit rechtmäßig nutzt, erhält die Anwendung des Entwicklers eine Laufzeitlizenz, die vom Entwickler ohne Einschränkung verteilt werden kann.

Mit dem Lizenzierungsmodell stehen Ihnen noch zahlreiche andere Möglichkeiten der Lizenzunterstützung zur Verfügung. Bei diesem Modell ist die Validierungslogik von der Komponente oder dem Steuerelement getrennt. Durch einen Lizenzanbieter werden Lizenzen erteilt und die Validierungslogik ausgeführt. Bei dem Anbieter handelt es sich um eine Klasse, die sich von System.ComponentModel.LicenseProvider ableitet. Die Schritte, die Sie zum Aktivieren der Lizenzierung durchführen müssen, sind sehr einfach.

Wenn Sie die von LicFileLicenseProvider bereitgestellte Standardimplementierung von LicenseProvider verwenden, sieht das Format der Lizenzdatei wie folgt aus:

  • Bei dem Dateinamen muss es sich um einen voll qualifizierten Namen der Klasse mit der Dateinamenerweiterung LIC handeln, einschließlich des Namespaces. Beispiel:

    Namespace1.Class1.LIC

  • Die Lizenzdatei sollte die folgende Textzeichenfolge enthalten:

    "myClassName ist eine lizenzierte Komponente."

    myClassName ist der vollqualifizierte Name der Klasse. Beispiel:

    "Namespace1.Class1 ist eine lizenzierte Komponente."

Im folgenden Beispiel ist ein Windows Forms-Steuerelement und ein ASP.NET-Serversteuerelement dargestellt, durch die eine einfache Lizenzierung implementiert wird.

So aktivieren Sie die Lizenzierung für die Komponente oder das Steuerelement

  1. Wenden Sie auf die Klasse LicenseProviderAttribute an.

  2. Rufen Sie Validate oder IsValid im Konstruktor auf.

  3. Rufen Sie im Finalizer der Klasse oder vor dem Aufruf des Finalizers Dispose für die gewährte Lizenz auf.

In den folgenden Codebeispielen wird die integrierte LicFileLicenseProvider-Lizenzanbieterklasse verwendet, die die Verwendung von Textlizenzdateien ermöglicht und deren Verhalten dem der COM-Lizenzierung (ActiveX) entspricht. Für komplexere Lizenzierungsszenarien wie dem Aufrufen eines XML-Webdienstes zum Begrenzen der Anzahl von Instanzen einer Komponente sind andere Lizenzanbieter erforderlich.

Beispiel

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;
      }
   }
};
import System.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;


// Adds the LicenseProviderAttribute to the control.
/** @attribute LicenseProvider(LicFileLicenseProvider.class)
 */
public class MyControl extends Control
{
    // Creates a new, null license.
    private License license = null;

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

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

    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            if (license != null) {
                license.Dispose();
                license = null;
            }
        }
    } 
} 
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();
    }    
}
import System.*;
import System.ComponentModel.*;
import System.Web.UI.*;

// Adds the LicenseProviderAttribute to the control.
/** @attribute LicenseProvider(LicFileLicenseProvider.class)
 */
public class MyControl extends Control
{
    // Creates a new, null license.
    private License license = null;

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

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

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

        super.Dispose();
    }
} 

Siehe auch

Referenz

LicenseProviderAttribute

LicenseProvider

Weitere Ressourcen

Erstellen von Komponenten