Licensing in .NET explained - Part 1
Authoring licensed .NET components is explained in several places over the web. But I was not able to find an article that would cover all the aspects of licensing in .NET. That’s why I decided to write this article, and I hope things will be clearer after reading it.
First, what is the purpose of licensing ? It’s a mechanism that makes sure that only users with a valid licence can use a component. The .NET framework has a built-in licensing mechanism that makes it easy to author licensed components. From the component user point of view, it also has a great support in Visual Studio, which makes the use of licensed components easy. In this first article, we’ll focus on how to use licensed components.
Using Licensed Components with Visual Studio
Using licensed components inside Visual Studio is completely transparent for the user. The usual way to use components in Visual Studio is to drag and drop them from the toolbox onto a design surface: When a toolbox item is dropped onto a design surface, Visual Studio creates an instance of the component represented by the toolbox item and adds it to the designer. If the component created is a licensed component, Visual Studio also adds a licences.licx file to the Visual Studio project.
The licences.licx file contains the type names of the licensed components used in a Visual Studio project. Here is an example:
ILOG.Views.Gantt.Windows.Forms.ActivityTable, ILOG.Gantt ILOG.Views.Gantt.Windows.Forms.ResourceTable, ILOG.Gantt ILOG.Views.Gantt.Windows.Forms.GanttChart, ILOG.Gantt
The licences.licx file is updated each time a new licensed component is added to the designer. You can also create or edit this file by hand. You must do this for example if you don’t create the components by dragging them from the toolbox.
The .licx files are associated in Visual Studio with the licence compiler tool (lc.exe). When building your project, the license compiler is run with the licence file as parameter. Compiling the licences produces a resource file that will be used at runtime by the application. Let’s see now in details how the compiled licences are used at runtime, depending on the application type you’re building.
Assembly-based Applications
This category of applications regroups all the applications that are assembly based, that is, applications whose ouput produce an assembly. It contains Windows Forms applications, WPF applications, Silverlight applications, as well as class libraries. It also contains Web project applications. For these applications, the compiled licences are automatically embedded in the resources of the application assembly when the project gets compiled. For example, let’s say I have a Windows Forms application called GanttEditor.exe that’s using licensed components. After compiling the application, we can check that a licence resource has been added to the application resources. To do this, I use Reflector, my favorite .NET disassembler :
The licences.licx file has been compiled into a resource called GanttEditor.exe.licenses.
Web Sites Applications
In Visual Studio, a Web Site is a special project, as there’s no real project file. Instead, all the files located under the web site directory are part of the web site. When a licensed component is dropped into a Web Form designer, Visual Studio creates a licenses.licx file and adds it to the web site directory. This licence file is compiled into an assembly named App_Licenses.dll, located in the web site Bin directory. This assembly will be loaded at runtime to locate the licences. Note that you can force the generation of the App_Licenses.dll assembly by right-clicking the licenses.licx file in the solution explorer, and choosing the “Build Runtime Licenses” menu item.
Using Licensed Components without Visual Studio
For some reasons, you may not use Visual Studio to create your application. In this case, you’ll have to integrate the licence into your application by yourself. These are the steps to follow:
- Create and edit you own licenses.licx file.
- Compile the licenses.licx file using the licence compiler (lc.exe).
- Integrate the compiled licence file into the application.
The last step depends on the type of application you’re building. For example, in a Windows Forms application, you have to put the compiled licence as an emdedded resource of the application (using the /resource option of the c# compiler if it’s a c# application). When hosting Windows Forms controls in IE, the process is different, and is described in the next section.
Windows Forms controls hosted in Internet Explorer
Embedding a Windows Forms control in Internet Explorer can be painful for two reasons:
- Code Access Security : When running into Internet Explorer, the hosted control doesn’t have the same permissions as if it was run as a desktop application. Configuring the CAS is not a simple task, as well as finding the minimum permissions required by a control to work properly inside IE.
- Licence Management : You have to compile the licences by yourself.
Let’s illustrate how to manage licences with a very simple example: You want to embed a GanttChart (located in the ILOG.Gantt.dll assembly) into the default.htm page. As the GanttChart is a licensed component, you’ll have to provide an entry for it in the licenses.licx file:
ILOG.Views.Gantt.Windows.Forms.GanttChart, ILOG.Gantt
Then, you’ll have to compile this file using the licence compiler:
lc.exe /target:default.htm /complist:licenses.licx /i:ILOG.Gantt.dll
the /target parameter must be the page in which the licence will be used,
the /complist parameter is the .licx file to compile,
the /i parameter indicates the assemblies needed to compile the licence.
The licence compiler will produce a file named default.htm.licenses. Now, we need to use this file in the HTML page as shown below:
<html> ... <HEAD> ... <LINK REL="licenses" href="default.htm.licenses" mce_href="default.htm.licenses" /> ... </HEAD> ... <OBJECT id="ganttControl" height="300" width="800" classid="ILOG.Gantt.dll#ILOG.Views.Gantt.Windows.Forms.GanttChart" /> ... </html>
The <OBJECT> tag is used to insert the control in the HTML page, and the <LINK> tag is used to indicate where the licence can be found.
Conclusion
We’ve seen two different ways to use licensed components in .NET: With or without Visual Studio. When using Visual Studio, it takes care of everything. When not using it, you need to do things by yourself.
Now, from a developper point of view, it would be interesting to understand how the licence compiler works, and how to author licensed components. That will be the subject of a second article.
Tags: lc.exe, licenses.licx, Licensing









May 27th, 2008 at 7:28 am
What do I need to do, If I need to create a tool that can create licenses for any given web application. The license will go along with the web application and at runtime the web app. needs to check if the number of users in its DB is within the limit set in the license. Also how can the number of users be set as part of the license. The license generation tool will take this number as a user input before it generates a license for the web application in question.
Please guide.
May 29th, 2008 at 5:31 am
I would simply create a License subclass that would update a counter (in a file, a database, in the application session, …). The counter should be modified in the constructor and in the Dispose method of the License. When the counter reaches a specific value, you can throw an exception in the License constructor.
Of course, to be able to use your License, you should create the appropriate LicenseProvider. See my second article on this subject for details.
Then, to control the number of users allowed to use your web app, you simply have to set the counter to the right value. You can imagine to set this value directly in the web.config file as a custom property.
Hope this helps