If you try to build an application that hosts a licensed
class, and the application does not have a default (parameter-less)
constructor, you receive an error message that is similar to the
following:
Could not transform licenses file
'licenses.licx' into a binary resource. (1) : error LC0004 : Exception occured
creating type 'System.MissingMethodException'
Note In this error message, the word "occured" is a misspelling of the
word "occurred."
To embed a license key of a class library in Microsoft
Visual Studio .NET, you must define a default public constructor in the class
library. This default constructor is used only for licensing. If the licensed
class has no default constructor, you receive a "MissingMethodException" error
message when you compile an application that includes the licenses.licx file.
Note You must manually create the licenses.licx file.
On the File menu, point to
New, and then click Project.
In the New Project dialog box, click
Visual C# Projects under Project Types, and
then click Class Library under
Templates.
In the Name text box, type
LicensedClass, and then click OK. By
default, Class1.cs is created.
In Solution Explorer, right-click
Class1.cs, and then click Rename.
Type LicensedClass.cs for the new
file name.
In the LicensedClass.cs file, replace the existing code
with the following sample code:
using System;
using System.ComponentModel;
namespace Licensing
{
/// <summary>
/// Summary description for LicensedClass.
/// </summary>
///
[LicenseProviderAttribute(typeof(LicFileLicenseProvider))]
public class LicensedClass : IDisposable
{
private License license = null;
// Uncomment the following code to resolve the error.
// public LicensedClass()
// {
// license = LicenseManager.Validate(typeof(LicensedClass), this);
// }
public LicensedClass(int i)
{
license = LicenseManager.Validate(typeof(LicensedClass), this);
Console.WriteLine("Hello from the licensed class.");
}
public void Dispose()
{
if (license != null)
{
license.Dispose();
license = null;
}
}
}
}
In Solution Explorer, right-click
LicensedClass, point to Add, and then click
Add New Item.
In the Add New Item - LicensedClass dialog
box, click Text File under Templates, and
then click Open.
In Solution Explorer, rename the new text file
Licensing.LicensedClass.lic. In the Microsoft
Development Environment message box, click Yes to
rename the file.
In Solution Explorer, click
Licensing.LicensedClass.lic, and the press F4 to open the
Properties window.
In the drop-down list box for the Build
Action property, click Embedded Resource.
Paste the following text in the Licensing.LicensedClass.lic
file:
Licensing.LicensedClass is a licensed
component.
Save all the files in the LicensedClass project, and then
build the project.
Note You must put the LIC file of the class library into the same
folder as the assembly DLL file. If the reference is a project reference, the
LIC file should be included in the obj\bin folder. If it is a file reference,
it should be included in the debug\bin or release\bin folder.
Create a host for the licensed class
In Visual Studio .NET, create a Visual C# .NET
Console Application project that is named
LicensedClassHost.
In Solution Explorer, right-click
Class1.cs, and then click Rename.
Type LicensedClassHost.cs for the
new file name.
In the LicensedClassHost.cs file, replace the existing code with the following sample code:
using System;
using System.ComponentModel;
// If the following statement causes a compiler error, add a project reference to
// the LicensedClass.dll file. LicensedClass.dll is located in the LicensedClass\bin\debug folder.
using Licensing;
namespace LicensingHost
{
/// <summary>
/// Summary description for LicensedClassHost.
/// </summary>
class LicensedClassHost
{
static void Main(string[] args)
{
int i = 0;
LicensedClass LicensedClass1 = new LicensedClass(i);
}
}
}
In the LicensedClassHost project, add a project reference
to the LicensedClass.dll file that you created in the "Create a Licensed Class" section of
this article.
In Solution Explorer, right-click
LicensedClassHost, point to Add, and then
click Add New Item.
In the Add New Item - LicensedClassHost
dialog box, click Text File under Templates,
and then click Open.
In Solution Explorer, rename the new text file
licenses.licx. In the Microsoft Development
Environment message box, click Yes to rename the
file.
In Solution Explorer, click licenses.licx,
and the press F4 to open the Properties window.
In the drop-down list box for the Build
Action property, click Content.
Paste the following text in the licenses.licx file:
Licensing.LicensedClass,LicensedClass
Save all the files in the LicensedClassHost project, and
then build the project.
Note You must put the LIC file of the class library into the same
folder as the assembly DLL file. If the reference is a project reference, the
LIC file should be included in the obj\bin folder. If it is a file reference,
it should be included in the debug\bin or release\bin folder.