Language

IAgentServiceRegistrar Interface

Definition

Defines dependency-injection registrations contributed by an Agents SDK extension assembly.

public interface IAgentServiceRegistrar
type IAgentServiceRegistrar = interface
Public Interface IAgentServiceRegistrar

Remarks

This contract lets an extension register its required services when the host calls AddAgentExtensionServices(IServiceCollection). ASP.NET Core hosting calls that method from AddAgentCore; other hosts can call it directly. The application therefore only needs to reference the extension package rather than invoke a package-specific service-registration method.

An extension opts in by implementing this interface and adding an assembly-level AgentServiceRegistrationAttribute:

[assembly: AgentServiceRegistration(typeof(MyExtensionRegistrar))]

public sealed class MyExtensionRegistrar : IAgentServiceRegistrar
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.TryAddSingleton<MyExtensionService>();
    }
}

Registrar types must be public, concrete, and have a public parameterless constructor so consuming applications can preload and instantiate them. A registrar is invoked at most once for each IServiceCollection instance. Registrars should use the Microsoft.Extensions.DependencyInjection.ExtensionsTryAdd methods when application registrations should take precedence, and must not build or resolve a service provider while registrations are being configured.

A custom host can apply all referenced extension registrations with:

services.AddAgentExtensionServices();

Methods

Name Description
ConfigureServices(IServiceCollection)

Adds the services required by the extension to the application service collection.

Applies to