方法 : コンポーネントおよびコントロールのライセンス処理を行う

.NET Framework には、すべてのコンポーネントとコントロール (Windows フォーム コントロールや ASP.NET サーバー コントロールを含む) について同一のライセンス処理モデルが用意されています。このライセンス処理モデルは、Microsoft ActiveX® コントロールのライセンス処理と完全な互換性があります。

ライセンス処理を実行することで、コンポーネントまたはコントロールの作成者は、開発者がコンポーネントまたはコントロールの使用を承認されていることを確認することによって、知的所有権の保護に役立てることができます。 この確認は実行時よりも、開発者がコンポーネントやコントロールをアプリケーションに組み込むデザイン時に、より重要になります。 ライセンスが許可されたコンポーネントやコントロールをデザイン時に合法的に使用する場合、開発されたアプリケーションには自由な配布を許可するランタイム ライセンスが付与されます。

ライセンス処理モデルでは、異なるレベルでのライセンス処理が他にも多数サポートされています。 ライセンス処理モデルは、コンポーネントやコントロールから検証ロジックを分離しています。 ライセンス プロバイダーはライセンスを付与し、検証ロジックを実行します。 プロバイダーは、LicenseProvider から派生するクラスです。 ライセンス処理を有効にするために必要な手順は単純です。

LicFileLicenseProvider に用意されている LicenseProvider の既定の実装を使用すると、ライセンス ファイルは次のように設定されます。

  • ファイルの名前は、名前空間を含み、ファイル名の拡張子が .LIC というクラスの完全修飾名にする必要があります。 以下はその例です。

    Namespace1.Class1.LIC

  • ライセンス ファイルの内容には、次の文字列を含めるようにします。

    "myClassName is a licensed component."

    myClassName はクラスの完全修飾名です。 以下はその例です。

    "Namespace1.Class1 is a licensed component."

単純なライセンス処理を実装する Windows フォーム コントロールと ASP.NET サーバー コントロールのコード例を次に示します。

作成したコンポーネントまたはコントロールに対するライセンス処理を有効にするには

  1. クラスに LicenseProviderAttribute を適用します。

  2. コンストラクターで Validate または IsValid を呼び出します。

  3. クラスの終了処理で、または終了処理を呼び出す前に、許可したすべてのライセンスに対して Dispose を呼び出します。

ここに示した例では、組み込みライセンス プロバイダー クラスの LicFileLicenseProvider を使用しています。このクラスではテキストのライセンス ファイルを使用でき、また COM (ActiveX) のライセンス処理と同様の動作を行います。 より複雑なライセンス処理のシナリオ (XML Web サービスを呼び出して、コンポーネントのインスタンス数を制限する場合など) では、別の種類のライセンス プロバイダーが必要です。

使用例

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

参照

関連項目

LicenseProviderAttribute

LicenseProvider

その他の技術情報

コンポーネントの作成