.NET Framework 4 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 controlApply a LicenseProviderAttribute to the class. Call Validate or IsValid in the constructor. 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
|
.NET Framework 4 Cómo: Obtener licencia para componentes y controles .NET Framework dispone de un modelo de licencias que es idéntico para todos los componentes y controles (incluidos los controles de formularios Windows Forms y los controles de servidor ASP.NET), y es totalmente compatible con las licencias de los controles Microsoft ActiveX®. Con la concesión de licencias, usted, como el creador del componente o el control, puede proteger su propiedad intelectual comprobando que un desarrollador está autorizado a usar el componente o control. Esta comprobación es más importante en tiempo de diseño, cuando el desarrollador incorpora el componente o control a una aplicación, que en tiempo de ejecución. Si un desarrollador usa legalmente el componente o control con licencia en tiempo de diseño, la aplicación del desarrollador obtiene una licencia en tiempo de ejecución que se puede distribuir gratuitamente. Existen muchos otros niveles de compatibilidad con el modelo de licencias. El modelo separa la lógica de validación del componente o control. Un proveedor de licencias las concede y realiza la lógica de validación. El proveedor es una clase que deriva de System.ComponentModel..::.LicenseProvider. Los pasos que debe realizar para habilitar las licencias son sencillos. Cuando utiliza la implementación predeterminada de LicenseProvider proporcionada por LicFileLicenseProvider, se da formato al archivo de licencia de la siguiente manera: El nombre del archivo debe ser el nombre completo, incluido el espacio de nombres, de la clase con la extensión de nombre de archivo .LIC. Por ejemplo: Espacio de nombres1.Clase1.LIC El contenido del archivo de licencia debería contener la cadena de texto siguiente: "miNombreDeClase es un componente con licencia". myClassName el nombre completo de la clase. Por ejemplo: "Espacio de nombres1.Clase1 es un componente con licencia".
Los ejemplos de código siguientes muestran un control de formularios Windows Forms y un control de servidor de ASP.NET que implementan un caso simple de concesión de licencias. Para habilitar las licencias para su componente o controlAplique un atributo LicenseProviderAttribute a la clase. Llame a Validate o a IsValid en el constructor. Llame al método Dispose de cualquiera de las licencias concedidas en el finalizador de la clase o antes de llamar al finalizador.
Los ejemplos de código siguientes utilizan la clase de proveedor de licencias integrada LicFileLicenseProvider, que habilita el uso de archivos de texto de licencia y simula el comportamiento de las licencias COM (ActiveX). Otros casos de licencias más complejos, como llamar a un servicio Web XML para limitar el número de instancias de un componente, requieren otros proveedores de licencias.

Ejemplo
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();
}
}

Vea también
|