Skip to content

.NET quickstart

This page is for whoever adds Monaiq to a .NET app, you or your AI assistant. It gets the Monaiq .NET client installed, configured and registered, and shows you where the licensing calls come from afterwards.

The package is Sidub.Licensing.Client, and it targets netstandard2.1.

Terminal window
dotnet add package Sidub.Licensing.Client

The client is configured through LicensingServiceOptions and registered with AddSidubLicensing(). In a minimal hosting Program.cs:

var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<LicensingServiceOptions>(options =>
{
options.LicenseServiceUri = "https://api.monaiq.com/licensing";
options.ConsumptionServiceUri = "https://api.monaiq.com/consumption";
options.EncodedCredential = builder.Configuration["Licensing:EncodedCredential"];
options.IssuerClientId = Guid.Parse("<your account client id>");
options.ProductId = Guid.Parse("<your product id>");
});
builder.Services.AddSidubLicensing();
var app = builder.Build();

That is the whole setup: AddSidubLicensing() registers everything the SDK needs, including the service connectors built from the options above; there is no separate registration step. If an option is wrong, the application fails at startup with a message naming the option (LicenseServiceUri must be an absolute URI, for example) rather than failing on the first licensing call.

Five things are being set:

  • LicenseServiceUri is the licensing API, which answers the authorization and feature questions.
  • ConsumptionServiceUri is the consumption API, which handles metered usage. Consumption and metering covers when you need it.
  • EncodedCredential is your credential, read from configuration rather than written into the file.
  • IssuerClientId is your account’s client id, from the portal.
  • ProductId is this product’s id, from the portal. With both set, a credential of another seller or another product is refused, so a credential bought for a different product cannot unlock yours because the feature keys match.

Both addresses are the production ones and are listed in Endpoints.

With the client registered, resolve the licensing context and ask the service two questions in a scope:

using Sidub.Licensing;
using Sidub.Licensing.Context;
using Sidub.Licensing.Features;
using Sidub.Licensing.Services;
using var scope = app.Services.CreateScope();
var context = await scope.ServiceProvider.GetRequiredService<LicensingContextAccessor>().GetContextAsync();
var licensing = scope.ServiceProvider.GetRequiredService<ILicensingService>();
if (context is null)
{
Console.WriteLine("No license code configured yet.");
}
else
{
var authorization = await licensing.GetAuthorization(LicensingServiceReference.ServiceReference, context);
Console.WriteLine($"Seat {authorization.SeatId} of license {authorization.LicenseId} is licensed.");
var included = await licensing.AssertLicense(LicensingServiceReference.ServiceReference, context, ServiceAccessLicenseAssertion.Create("<your feature key>"));
Console.WriteLine(included ? "The feature is included." : "The feature is not included.");
}

GetAuthorization answers whether this seat is licensed at all, and throws when it is refused. AssertLicense with ServiceAccessLicenseAssertion.Create answers whether one feature, named by its key, is included. GetContextAsync returns null until a credential is configured. In the portal, Developers > Connect your app shows this code with your account id and your product’s id already filled in.

EncodedCredential takes the portable credential string that starts SIDUB_LIC_. The example reads it from the configuration key Licensing:EncodedCredential, which in appsettings.json looks like this:

{
"Licensing": {
"EncodedCredential": "SIDUB_LIC_..."
}
}

The string carries a token minted for one seat, so it authorizes and meters that seat and reaches nothing else. Do not decode it and do not read the token inside it; configure it and the SDK calls the runtime operations with it. The two service URIs above stay exactly as they are: the SDK appends /runtime itself.

Where the value comes from, how to issue another and how to revoke one, is in Credentials and keys.

AddSidubLicensing() registers ILicensingService, which you take from dependency injection wherever you need to make a licensing decision. Its members are:

  • GetAuthorization for the current authorization, which is the starting point for everything else.
  • GetLicenseFeature<T> for a single feature of the license. It returns null when the license does not carry the feature; treat that as “not licensed”, don’t suppress it.
  • PerformOperation<TFeature, TState> for work that runs against a feature.
  • AssertLicense for enforcing a condition rather than inspecting one.
  • GetLicenseFeatureState<TFeature, TState> and SetLicenseFeatureState<TFeature, TState> for reading and writing a feature’s local state (a rate-limited feature’s consumption, for example).

Two supporting types come with it: LicensingContextAccessor for the current licensing context, and LicensingServiceReference for identifying the service itself. .NET client reference lists the full surface.

When an API call fails, the SDK throws LicensingApiException carrying the server’s error contract: the machine-readable Code to branch on, and the CorrelationId to quote when contacting support. The .NET client reference describes the fields.

When the application stops, the SDK settles with the platform by itself: it delivers the uses it buffered and returns the allowance units it holds. On a Microsoft.Extensions.Hosting 8.0 or later host that happens when the host stops; otherwise when the service provider is disposed. Nothing to wire. .NET client reference has the detail.

The same package carries IPurchaseClient, for an application you distribute that sells your offerings from inside itself. It is registered separately, with AddSidubLicensingPurchaseClient, and it holds no key of yours: the buyer signs in with their Monaiq identity and buys with their own token, the app reads your catalog anonymously, and the credential that comes back is what EncodedCredential above takes. In-app purchase covers the flow, the sign-in choice and the client.

If you would rather not work out the call sites by hand, the AI-assisted path does exactly that: its implement_base and implement_product_feature tools return the wiring and the feature check for your own project, and provision_api_key_config returns the commands and file edits that plumb your account API key and client id into a backend project. See AI-assisted setup.