"License cannot be granted" exception when you add a licensed user control to the Windows Form in the same solution (814342)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

When you create a licensed user control in a project, and then add it to the Windows form in the same solution, you receive the following exception:
An exception occurred while trying to create an instance of 'Projectname.Controlname'. The exception was "An instance of type 'Projectname.Controlname' was being created, and a valid license could not be granted for the type 'Projectname.Controlname'. Please, contact the manufacturer of the component for more information."
However, when you create a new solution, and then add the licensed user control to the Windows form, the licensed user control is added.

CAUSE

Any user control that is implicitly added to the toolbox refers the assembly file in the obj\Debug folder. Therefore, the licensed user control that is created refers to the WindowsControlLibrary1.dll file in the obj\Debug folder. Generally, the license (.lic) files are put in the Bin folder. When you add an instance of a licensed user control to the Windows form in the same solution, the license is validated. Because the .lic file is not located in the obj\Debug folder, you receive the exception.

RESOLUTION

To resolve this problem, move the .lic file to the obj\Debug folder. Follow these steps to resolve the problem described in the "More Information" section of this article:
  1. On the Project menu, click Show All Files.
  2. In Solution Explorer, open the bin folder.
  3. Right-click WindowsControlLibrary1.UserControl1.lic, and then click Copy.
  4. Expand obj, right-click Debug, and then click Paste.
  5. From the toolbox, drag UserControl1 to Form1.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, create a new Windows Control Library project by using Visual Basic 2005, Visual Basic .NET, Visual C# 2005, or Visual C# .NET.

    By default, UserControl1 is created.
  2. On the View menu, click Code.
  3. Replace the existing code with the following code:

    Visual Basic .NET or Visual Basic 2005 Code
    Imports System.ComponentModel
    'Create a license on the User Control. 
    <LicenseProvider(GetType(LicFileLicenseProvider))> Public Class UserControl1
        Inherits System.Windows.Forms.UserControl
    
    #Region " Windows Form Designer generated code "
        Private license As license
        Public Sub New()
    
            MyBase.New()
            ' Validate whether or not the License can be granted. 
            license = LicenseManager.Validate(GetType(UserControl1), Me)
            'The Windows Form Designer requires this call.
            InitializeComponent()
    
        End Sub
    
        'UserControl1 overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    
            If Not (license Is Nothing) Then
                license.Dispose()
                license = Nothing
            End If
    
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
    
        End Sub
    
        'Required by Windows Form designer. 
        Private components As System.ComponentModel.IContainer
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
            components = New System.ComponentModel.Container()
    
        End Sub
    
    #End Region
    End Class
    
    
    
    Visual C# .NET or Visual C# 2005 Code
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    
    namespace WindowsControlLibrary1
    {
    	
    	//Create the license on the User Control.
    	[LicenseProviderAttribute(typeof(LicFileLicenseProvider))]
    	public class UserControl1 : System.Windows.Forms.UserControl
    	{
    		private System.ComponentModel.Container components = null;
    		private License license = null;
    
    		public UserControl1()
    		{
    			// This call is required by the Windows.Forms Form Designer.
    			InitializeComponent();
    			
    			// Validate whether or not the license can be granted.
    			license = LicenseManager.Validate(typeof(UserControl1), this);
    		}	
    		
    		protected override void Dispose( bool disposing )
    		{
    			// Destroy the license.
    			if (license != null) 
    			{
    				license.Dispose();
    				license = null;
    			}
    			// Clean up any resources that are being used.
    		
    			if( disposing )
    			{
    				if( components != null )
    					components.Dispose();
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Component Designer generated code
    		private void InitializeComponent()
    		{
    			components = new System.ComponentModel.Container();
    		}
    		#endregion
    	}
    }
    
  4. Locate the bin folder in WindowsControlLibrary1 by following the corresponding path on your computer. Open the Debug folder in the bin folder.
  5. In the Debug folder, create a new WindowsControlLibrary1.UserControl1.lic text document.
  6. In the Rename dialog box, click Yes.
  7. Open the WindowsControlLibrary1.UserControl1.lic file in Notepad, and then paste the following code:
    WindowsControlLibrary1.UserControl1 is a licensed component.
  8. Save the file, and then close it.
  9. On the Build menu, click Build Solution.
  10. On the File menu, point to Add Project, and then click New Project.
  11. Under Project Types, click Visual Basic Projects or Visual C# Projects.
  12. Under Templates, click Windows Application.

    By default, Form1 is created.
  13. From the toolbox, drag the UserControl1 to Form1.

    You may receive the exception described in the "Symptoms" section of this article.
Note This problem occurs only when you use the Microsoft .NET Framework default license provider LicFileLiscenseProvider. When you write your own custom license providers, this problem does not occur.

REFERENCES

For more information, visit the following MSDN Web site:

LicFileLicenseProvider Class
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemcomponentmodellicfilelicenseproviderclasstopic.asp

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbLicensing kbWindowsForms kbCtrl kbControl kbprb KB814342 kbAudDeveloper