License Compiler (LC.exe) 是 .NET Framework SDK 內建工具,專責將 **授權許可證資訊 (.licx 文字檔)** 編譯為 **二進位 `.licenses` 資源**,嵌入組件中。執行階段 `LicenseManager` 讀取嵌入資源驗證授權,保護商業控制項/元件智慧財產權。
# 每行一個受授權型別,格式:完整型別名稱, 組件完整名稱
MyCompany.Controls.GridControl, MyCompany.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234567890abcdef
MyCompany.Charts.ChartEngine, MyCompany.Charts, Version=2.0.0.0, Culture=neutral, PublicKeyToken=1234567890abcdef
| 參數 | 說明 | 範例 |
|---|---|---|
| /complist:file | 指定 .licx 檔案 (可多個) | /complist:Controls.licx |
| /i:assembly | 參考組件路徑 (可多個,供型別解析) | /i:bin\MyControls.dll |
| /target:exe | 目標輸出組件檔名 (決定 .licenses 命名) | /target:MyApp.exe |
| /outdir:path | 輸出目錄 | /outdir:obj\Debug |
| /v | 詳細輸出 | /v |
| /nologo | 隱藏版權資訊 | /nologo |
public class LicenseCompiler
{
private ArrayList _compLists; // .licx 檔案清單
private ArrayList _assemblies; // 參考組件清單
private string _targetPE; // 目標組件名
private string _outputDir; // 輸出目錄
public int Main(string[] args)
{
if (!ProcessArgs(args)) return 1; // 解析失敗
// 註冊組件解析事件 (處理 /i 指定的參考組件)
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
// 建立設計時授權上下文
var context = new DesigntimeLicenseContext();
// 處理每個 .licx 檔案
foreach (string licxFile in _compLists)
{
ProcessLicxFile(licxFile, context);
}
// 產出 .licenses 資源檔
WriteLicensesResource();
return 0;
}
}
private void ProcessLicxFile(string licxPath, DesigntimeLicenseContext context)
{
using (var reader = new StreamReader(licxPath, Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (string.IsNullOrEmpty(line) || line.StartsWith("#")) continue; // 註解支援
// 解析 "TypeName, AssemblyFullName"
var type = Type.GetType(line, throwOnError: true, ignoreCase: false);
// 關鍵:觸發 LicenseProvider 建立授權
// 此時 LicenseProvider.LicenseProviderAttribute 指定的 Provider 會被實例化
// 並呼叫 GetLicense(LicenseContext, Type, object, bool)
LicenseManager.CreateWithContext(type, context);
// 授權物件會被 context 內部收集 (ICollection _licenses)
}
}
}
private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
// 依 /i 指定的組件清單嘗試載入
foreach (string asmPath in _assemblies)
{
try
{
var asmName = new AssemblyName(args.Name);
if (AssemblyName.GetAssemblyName(asmPath).FullName == args.Name)
return Assembly.LoadFrom(asmPath);
}
catch { /* 忽略 */ }
}
return null; // 讓 CLR 繼續嘗試預設探索
}
private void WriteLicensesResource()
{
string outputPath = Path.Combine(_outputDir ?? ".",
Path.GetFileNameWithoutExtension(_targetPE) + ".licenses");
using (var stream = new FileStream(outputPath, FileMode.Create))
using (var writer = new BinaryWriter(stream, Encoding.UTF8))
{
// 1. Magic Header
writer.Write("Microsoft.NET Licenses"); // 識別標記
// 2. 授權數量
var licenses = context.GetLicenses(); // 內部收集的 License 物件集合
writer.Write(licenses.Count);
foreach (License lic in licenses)
{
// 3. 型別完整名稱
writer.Write(lic.GetType().FullName); // 實際為授權型別名稱
// 4. 授權金鑰字串 (LicenseKey)
string key = lic.LicenseKey;
writer.Write(key ?? "");
// 5. 額外資料 (可選,視 Provider 而定)
// ...
}
}
}
| 時代 | 授權機制 | 工具 |
|---|---|---|
| .NET Framework 1.x - 4.x | LC.exe + .licx + LicenseProvider | LC.exe, VS 建置整合 |
| .NET Core / .NET 5+ | **無內建對應機制** | 廠商自行實作 (License Key 驗證 API、簽章驗證、硬體指紋) |
| 現代商業元件 | NuGet 套件授權、離線授權檔、雲端驗證 | 自有 CLI/SDK、CI/CD 整合 |
LC.exe 屬於 **Legacy 技術**,新專案**不建議**採用。若維護舊專案需修改授權邏輯,理解其「編譯期產生授權資源 → 執行期驗證」雙階段架構,才能正確客製化 `LicenseProvider` 或繞過測試環境授權檢查。
理解 LC.exe 內部運作,不僅能解決舊專案授權建置問題,更能深入掌握 .NET 元件授權模型的設計哲學與局限,為現代軟體授權架構選型提供歷史參考。