This class provides methods for configuring the service, requesting and releasing licenses, activating and deactivating licenses, and validating licenses.
Constructors
BabelLicensing()
BabelLicensing class constructor
public BabelLicensing()
BabelLicensing(BabelLicensingConfiguration)
This is the constructor for the BabelLicensing class that takes a configuration object as a parameter. It initializes a new instance of the BabelLicensing class and sets the configuration property to the provided configuration object.
public BabelLicensing(BabelLicensingConfiguration configuration)
Parameters
Name
Description
The configuration object.
Examples
// Create a new configuration object
BabelLicensingConfiguration config = new BabelLicensingConfiguration() {
// Set the service URL
ServiceUrl = "http://localhost:5005",
// Set the public key used to verify the license signature
// See RSASignature for more information on how to export a public key
SignatureProvider = RSASignature.FromKeys("public key here"),
// Set a unique client ID
ClientId = "my custom client id"
};
// Create the client object used to communicate with the server
BabelLicensing client = new BabelLicensing(config);
' Create a new configuration object
Dim config As New BabelLicensingConfiguration() With {
' Set the service URL
.ServiceUrl = "http://localhost:5005",
' Set the public key used to verify the license signature
' See RSASignature for more information on how to export a public key
.SignatureProvider = RSASignature.FromKeys("public key here"),
' Set a unique client ID
.ClientId = "my custom client id"
}
' Create the client object used to communicate with the server
Dim client As New BabelLicensing(config)
Exceptions
Name
Description
Properties
Configuration
Reporting configuration
public BabelLicensingConfiguration Configuration { get; }
public Task<ActivateLicenseResult> ActivateLicenseAsync(string userKey, Type type = null, object instance = null, CancellationToken cancellationToken = default)
Parameters
Name
Description
The user key to activate the license for.
The type to activate the license for.
The instance to activate the license for.
The cancellation token.
Returns
Name
Description
The result of the license activation.
Examples
using Babel.Licensing;
// Create a new configuration object
BabelLicensingConfiguration config = new BabelLicensingConfiguration() {
// Set the service URL
ServiceUrl = "http://localhost:5005",
// Set the public key used to verify the license signature
SignatureProvider = RSASignature.FromKeys("<public key>"),
// Set the application name
// This will identify the application on the and server
ClientId = "<application name>"
};
// Create the client object used to communicate with the server
BabelLicensing client = new BabelLicensing(config);
// Use BabelServiceLicenseProvider to cache a local copy of the license for offline use
BabelServiceLicenseProvider provider = new BabelServiceLicenseProvider(client) {
// Refresh the license contacting the server every 5 days
// If set to TimeSpan.Zero, the license will be validated every time on the server
LicenseRefreshInterval = TimeSpan.FromDays(5)
};
// Register the license provider with BabelLicenseManager
BabelLicenseManager.RegisterLicenseProvider(typeof(Program), provider);
// Check if the user key is set
if (provider.UserKey == null)
{
// If the user key is not set, the license has not been activated yet.
// Ask the user to activate the license
Console.WriteLine("Please enter your activation license key: ");
string? userKey = Console.ReadLine();
try
{
var result = await client.ActivateLicenseAsync(userKey, typeof(Program));
Console.WriteLine($"License {result.License.Id} activated, client ({result.ActiveClientCount}/{result.MaxClientCount})");
}
catch (Exception ex)
{
Console.WriteLine($"Could not activate license: {ex.Message}");
return;
}
}
try
{
// Validate the license
// This will contact the server to validate the license if the local copy is expired
// You can customize the validation interval using the LicenseValidationInterval property
var license = await BabelLicenseManager.ValidateAsync(typeof(Program));
Console.WriteLine($"License {license.Id} valid.");
// Ask to deactivate the license
Console.WriteLine("Do you want to deactivate the license? (y/n)");
string? answer = Console.ReadLine();
if (answer?.ToLower() == "y")
{
try
{
await client.DeactivateLicenseAsync(provider.UserKey);
Console.WriteLine("License deactivated.");
}
catch (Exception ex)
{
Console.WriteLine($"Could not deactivate license: {ex.Message}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"License not valid: {ex.Message}");
return;
}
Imports Babel.Licensing
' Create a new configuration object
Dim config As New BabelLicensingConfiguration() With {
' Set the service URL
.ServiceUrl = "http://localhost:5005",
' Set the public key used to verify the license signature
.SignatureProvider = RSASignature.FromKeys("<public key>"),
' Set the application name
' This will identify the application on the and server
.ClientId = "<application name>"
}
' Create the client object used to communicate with the server
Dim client As New BabelLicensing(config)
' Use BabelServiceLicenseProvider to cache a local copy of the license for offline use
Dim provider As New BabelServiceLicenseProvider(client) With {
' Refresh the license contacting the server every 5 days
' If set to TimeSpan.Zero, the license will be validated every time on the server
.LicenseRefreshInterval = TimeSpan.FromDays(5)
}
' Register the license provider with BabelLicenseManager
BabelLicenseManager.RegisterLicenseProvider(GetType(Program), provider)
' Check if the user key is set
If provider.UserKey Is Nothing Then
' If the user key is not set, the license has not been activated yet.
' Ask the user to activate the license
Console.WriteLine("Please enter your activation license key: ")
Dim userKey As String = Console.ReadLine()
Try
Dim result = Await client.ActivateLicenseAsync(userKey, GetType(Program))
Console.WriteLine($"License {result.License.Id} activated, client ({result.ActiveClientCount}/{result.MaxClientCount})")
Catch ex As Exception
Console.WriteLine($"Could not activate license: {ex.Message}")
Return
End Try
End If
Try
' Validate the license
' This will contact the server to validate the license if the local copy is expired
' You can customize the validation interval using the LicenseValidationInterval property
Dim license = Await BabelLicenseManager.ValidateAsync(GetType(Program))
Console.WriteLine($"License {license.Id} valid.")
' Ask to deactivate the license
Console.WriteLine("Do you want to deactivate the license? (y/n)")
Dim answer As String = Console.ReadLine()?.ToLower()
If answer = "y" Then
Try
Await client.DeactivateLicenseAsync(provider.UserKey)
Console.WriteLine("License deactivated.")
Catch ex As Exception
Console.WriteLine($"Could not deactivate license: {ex.Message}")
End Try
End If
Catch ex As Exception
Console.WriteLine($"License not valid: {ex.Message}")
Return
End Try
Exceptions
Name
Description
Configure(Action<BabelLicensingConfiguration>)
Configures the BabelLicensing service with the provided configuration.
public static void Configure(Action<BabelLicensingConfiguration> configurer)
Parameters
Name
Description
The configuration action to apply.
Examples
BabelLicensing.Configure(config => {
// Set the service URL
ServiceUrl = "http://localhost:5005";
// To extract the public key from a .snk file use the following code
// var rsa = RSASignature.CreateFromKeyFile(@"Keys.snk");
// string publicKey = rsa.ExportKeys(true);
string publicKey = "public key here";
config.SignatureProvider = RSASignature.FromKeys(publicKey);
// Set client id to identify this application
config.ClientId = "my custom client id";
});
BabelLicensing.Configure(Sub(config)
' Set the service URL
config.ServiceUrl = "http://localhost:5005"
' To extract the public key from a .snk file use the following code
' Dim rsa = RSASignature.CreateFromKeyFile("Keys.snk")
' Dim publicKey As String = rsa.ExportKeys(True)
Dim publicKey As String = "public key here"
config.SignatureProvider = RSASignature.FromKeys(publicKey)
' Set client id to identify this application
config.ClientId = "my custom client id"
End Sub)
CreateDefaultConfiguration()
Creates a default configuration for the Babel Licensing service.
Requests a floating license for the specified user key, type and object instance.
public Task<RequestFloatingLicenseResult> RequestFloatingLicenseAsync(string userKey, Type type = null, object instance = null, CancellationToken cancellationToken = default)
Parameters
Name
Description
The user key.
The type.
The object instance.
The cancellation token.
Returns
Name
Description
A object representing the result of the request.
Examples
using Babel.Licensing;
// Create a new configuration object
BabelLicensingConfiguration config = new BabelLicensingConfiguration() {
// Set the service URL
ServiceUrl = "http://localhost:5005",
// Set the public key used to verify the license signature
SignatureProvider = RSASignature.FromKeys("<public key>"),
// Set a unique client ID for this application instance
ClientId = Guid.NewGuid().ToString()
};
// Create the client object used to communicate with the server
BabelLicensing client = new BabelLicensing(config);
// Ask the user to activate the license
Console.WriteLine("Please enter your floating license key: ");
string? userKey = Console.ReadLine();
try
{
var result = await client.RequestFloatingLicenseAsync(userKey, typeof(Program));
Console.WriteLine($"License {result.License.Id} requested.");
Console.WriteLine($"There are {result.ActiveClientCount}/{result.MaxClientCount} clients active.");
}
catch (Exception ex)
{
Console.WriteLine($"Could not request floating license: {ex.Message}");
return;
}
try
{
// Simulate a long task which requires the allocation of a license
for (int i = 0; i < 10; i++)
{
// Validate the license
// This will contact the server to validate the license
var result = await client.ValidateLicenseAsync(userKey, typeof(Program));
Console.WriteLine($"{i}) License {result.License.Id} valid.");
// Wait 1 second
await Task.Delay(1000);
}
try
{
// When the application is done with the license, it can release it
await client.ReleaseFloatingLicenseAsync(userKey);
Console.WriteLine("License released.");
}
catch (Exception ex)
{
Console.WriteLine($"Could not release license: {ex.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine($"License not valid: {ex.Message}");
return;
}
Imports Babel.Licensing
' Create a new configuration object
Dim config As New BabelLicensingConfiguration() With {
' Set the service URL
.ServiceUrl = "http://localhost:5005",
' Set the public key used to verify the license signature
.SignatureProvider = RSASignature.FromKeys("<public key>"),
' Set a unique client ID for this application instance
.ClientId = Guid.NewGuid().ToString()
}
' Create the client object used to communicate with the server
Dim client As New BabelLicensing(config)
' Ask the user to activate the license
Console.WriteLine("Please enter your floating license key: ")
Dim userKey As String = Console.ReadLine()
Try
Dim result = Await client.RequestFloatingLicenseAsync(userKey, GetType(Program))
Console.WriteLine($"License {result.License.Id} requested.")
Console.WriteLine($"There are {result.ActiveClientCount}/{result.MaxClientCount} clients active.")
Catch ex As Exception
Console.WriteLine($"Could not request floating license: {ex.Message}")
Return
End Try
Try
' Simulate a long task which requires the allocation of a license
For i As Integer = 0 To 9
' Validate the license
' This will contact the server to validate the license
Dim result = Await client.ValidateLicenseAsync(userKey, GetType(Program))
Console.WriteLine($"{i}) License {result.License.Id} valid.")
' Wait 1 second
Await Task.Delay(1000)
Next
Try
' When the application is done with the license, it can release it
Await client.ReleaseFloatingLicenseAsync(userKey)
Console.WriteLine("License released.")
Catch ex As Exception
Console.WriteLine($"Could not release license: {ex.Message}")
End Try
Catch ex As Exception
Console.WriteLine($"License not valid: {ex.Message}")
Return
End Try
Exceptions
Name
Description
ShutdownAsync()
Stops the heartbeat and shut down the communication with the service.