mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
e5f7b9c260
* support reflection for discovery of resources and scripts in class-based skills * fix format issues * refactor samples to use reflection * Validate resource member signatures during discovery Add discovery-time validation in AgentClassSkill.DiscoverResources() to fail fast when [AgentSkillResource] is applied to members with incompatible signatures: - Reject indexer properties (getter has parameters) - Reject methods with parameters other than IServiceProvider or CancellationToken Throws InvalidOperationException with actionable error messages instead of allowing silent runtime failures when ReadAsync invokes the AIFunction with no named arguments. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * prevent duplicates --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
e5f7b9c260
·
2026-04-10 11:56:28 +01:00
History
Agent Skills with Dependency Injection
This sample demonstrates how to use Dependency Injection (DI) with Agent Skills. It shows two approaches side-by-side, each handling a different conversion domain:
- Code-defined skill (
AgentInlineSkill) — converts distances (miles ↔ kilometers) - Class-based skill (
AgentClassSkill) — converts weights (pounds ↔ kilograms)
Both skills resolve the same ConversionService from the DI container. When prompted with a question spanning both domains, the agent uses both skills.
What It Shows
- Registering application services in a
ServiceCollection - Defining a code-defined skill (distance converter) with resources and scripts that resolve services from
IServiceProvider - Defining a class-based skill (weight converter) with resources and scripts that resolve services from
IServiceProvider - Passing the built
IServiceProviderto the agent so skills can access DI services at execution time - Running a single prompt that exercises both skills to show they work together
How It Works
- A
ConversionServiceis registered as a singleton in the DI container - Code-defined skill: An
AgentInlineSkillfor distance conversions declaresIServiceProvideras a parameter in itsAddResourceandAddScriptdelegates — the framework injects it automatically - Class-based skill: A
WeightConverterSkillclass extendsAgentClassSkillfor weight conversions and usesCreateResource/CreateScriptfactory methods withIServiceProviderparameters - Both skills resolve
ConversionServicefrom the provider — one for distance tables, the other for weight tables - A single agent is created with both skills registered, and the service provider flows through to skill execution
Tip: Class-based skills can also accept dependencies through their constructor. Register the skill class in the
ServiceCollectionand resolve it from the container instead of callingnewdirectly. This is useful when the skill itself needs injected services beyond what the resource/script delegates use.
How It Differs from Other Samples
| Sample | Skill Type | DI Support |
|---|---|---|
| Step02 | Code-defined (AgentInlineSkill) |
No — static resources |
| Step03 | Class-based (AgentClassSkill) |
No — static resources |
| Step05 (this) | Both code-defined and class-based | Yes — DI via IServiceProvider |
Prerequisites
- .NET 10
- An Azure OpenAI deployment
Configuration
Set the following environment variables:
| Variable | Description |
|---|---|
AZURE_OPENAI_ENDPOINT |
Your Azure OpenAI endpoint URL |
AZURE_OPENAI_DEPLOYMENT_NAME |
Model deployment name (defaults to gpt-5.4-mini) |
Running the Sample
dotnet run
Expected Output
Converting units with DI-powered skills
------------------------------------------------------------
Agent: Here are your conversions:
1. **26.2 miles → 42.16 km** (a marathon distance)
2. **75 kg → 165.35 lbs**