ภาษา

AgentHostExtensions Class

Definition

Extension methods on IHostApplicationBuilder and WebApplication for configuring agent services and the request pipeline.

public static class AgentHostExtensions
type AgentHostExtensions = class
Public Module AgentHostExtensions
Inheritance
AgentHostExtensions

Remarks

These methods provide a streamlined way to configure an agent application. The minimal setup is:

builder.AddAgentDefaults()
    .AddAgent<MyAgent>()
    .AddAgentAuthorization(b => b.AddAgentAspNetAuthentication());

var app = builder.Build();

app.UseAgents();
app.MapDefaultAgentEndpoints();

For advanced scenarios (custom HttpClient configuration, non-standard middleware ordering, custom endpoint routing, etc.), use ServiceCollectionExtensions for DI registration and AgentEndpointExtensions for endpoint mapping directly. The equivalent manual setup for the above is:

builder.Services.AddHttpClient();
builder.Services.AddControllers();
builder.AddAgent<MyAgent>();
// Configure authentication (e.g., AddAgentAspNetAuthentication, MISE, or custom)

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();
app.MapAgentRootEndpoint();
app.MapAgentApplicationEndpoints(requireAuth: true);

Methods

Name Description
AddAgent(IHostApplicationBuilder, Func<IServiceProvider,IAgent>)

Adds an Agent via lambda construction.

builder.Services.AddSingleton<IStorage, MemoryStorage>();
builder.AddAgent(sp =>
{
   var options = new AgentApplicationOptions()
   {
      TurnStateFactory = () => new TurnState(sp.GetService<IStorage>());
   };

   var app = new AgentApplication(options);

   ...

   return app;
});
AddAgent<TAdapter>(IHostApplicationBuilder, Func<IServiceProvider,IAgent>)

This is the same as AddAgent(IHostApplicationBuilder, Func<IServiceProvider,IAgent>), except allows the use of any CloudAdapter subclass.

AddAgent<TAgent,TAdapter>(IHostApplicationBuilder)

Same as AddAgent<TAgent>(IHostApplicationBuilder) but allows for use of any CloudAdapter subclass.

builder.Services.AddSingleton<IStorage, MemoryStorage>();
builder.AddAgent<MyAgent, MyCustomAdapter>();
AddAgent<TAgent>(IHostApplicationBuilder)

Adds an Agent which subclasses AgentApplication

builder.Services.AddSingleton<IStorage, MemoryStorage>();
builder.AddAgent<MyAgent>();
AddAgentApplicationOptions(IHostApplicationBuilder, AutoSignInSelector)

Registers AgentApplicationOptions for AgentApplication-based Agents.

AddAgentAttachmentDownloader(IHostApplicationBuilder)

Registers the AttachmentDownloader for non-Teams channels. This downloader handles standard HTTP attachment URLs.

AddAgentAuthorization(IHostApplicationBuilder, Action<IHostApplicationBuilder>, Nullable<Boolean>)

Configures token validation for inbound requests. The provided action should register authentication services (e.g., call AddAgentAspNetAuthentication). When enabled, endpoints mapped by MapDefaultAgentEndpoints(IEndpointRouteBuilder, String) will automatically require authorization.

AddAgentCore(IHostApplicationBuilder)

Adds the core agent services.

  • IConnections, which uses IConfiguration for settings.
  • IChannelServiceClientFactory for ConnectorClient and UserTokenClient creations.
  • CloudAdapter, this is the default adapter that works with Azure Bot Service and Activity Protocol Agents.
AddAgentCore<TAdapter>(IHostApplicationBuilder)

Adds the core agent services using a derived CloudAdapter.

  • IConnections, which uses IConfiguration for settings.
  • IChannelServiceClientFactory for ConnectorClient and UserTokenClient creations.
  • CloudAdapter, this is the default adapter that works with Azure Bot Service and Activity Protocol Agents.
AddAgentDefaults(IHostApplicationBuilder)

Registers default services required by the Agents SDK.

AddAgentFileDownloader(IHostApplicationBuilder, IInputFileDownloader)

Registers an IInputFileDownloader instance for use by AgentApplicationOptions. Can be called multiple times to add additional downloaders.

AddAgentM365AttachmentDownloader(IHostApplicationBuilder, M365AttachmentDownloaderOptions)

Registers the M365AttachmentDownloader for Teams/M365 channels. This downloader handles M365 attachment URLs using the configured token provider.

AddAgentMiddleware(IHostApplicationBuilder, IMiddleware)

Registers an IMiddleware instance to be used by the CloudAdapter pipeline. Can be called multiple times to add additional middleware in order.

AddCloudAdapter(IHostApplicationBuilder)

Add the default CloudAdapter.

AddCloudAdapter<T>(IHostApplicationBuilder)

Add a derived CloudAdapter.

IsAgentAuthorizationConfigured(IEndpointRouteBuilder)

Indicates whether agent authorization was configured via AddAgentAuthorization(IHostApplicationBuilder, Action<IHostApplicationBuilder>, Nullable<Boolean>).

IsAgentAuthorizationConfigured(IServiceProvider)

Indicates whether agent authorization was configured via AddAgentAuthorization(IHostApplicationBuilder, Action<IHostApplicationBuilder>, Nullable<Boolean>).

MapDefaultAgentEndpoints(IEndpointRouteBuilder, String)

Maps the default agent endpoints: a root health endpoint (GET /) and agent message endpoints for all registered AgentApplication types.

UseAgents(WebApplication, Boolean, Boolean)

Adds authentication and authorization middleware to the request pipeline.

UseHeaderPropagation(IApplicationBuilder)

Adds a middleware that collects headers to be propagated.

UseHeaderPropagation(IHostApplicationBuilder)

Registers the HeaderPropagationMiddleware so that incoming request headers are propagated to outgoing requests. This builder-phase overload allows header propagation to be configured fluently alongside the other agent registration calls:

builder.AddAgentDefaults()
    .AddAgent<MyAgent>()
    .UseHeaderPropagation()
    .AddAgentAuthorization(b => b.AddAgentAspNetAuthentication());

Applies to