azure-api-management-policy-toolkit
Copilot instructions95 starsChanged 12 months agoMITView on GitHub
Azure API Management policy toolkit: a set of C# libraries that let users author APIM policy documents in C# instead of XML, compile them to XML, and unit-test them with a gateway emulator. Prefer the newest stable .NET and C# versions possible while preserving repository compatibility. Test framework: MSTest with FluentAssertions. Tests use [TestClass] / [TestMethod] / [DataRow]. The toolkit has a three-layer pipeline for each policy: Supporting projects: src/Analyzers/ (Roslyn analyzers), src/Compiling/ (CLI compiler tool), src/Generators/ (source generators), src/Templates/…
In this repository6 other files it gives its agents.
- policy-authoringSkill
- policy-codebase-referenceSkill
- policy-compilationSkill
- policy-emulatorSkill
- policy-emulator-testingSkill
- policy-testingSkill
The file
We split this copilot-instructions.md into the rules it gives an agent
6 rules. About 0 are common, 1 is distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 17
Test framework: MSTest with FluentAssertions.
- Very specific: line 39
Follow the guide in docs/AddPolicyGuide.md.
- Distinctive: line 50
Use structurally similar existing policies as references:
- Very specific: line 65
Must be public in namespace Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policy (auto-registration)
- Very specific: line 67
Use TryExtractingConfigParameter<T>() to extract config, AddAttribute() for XML attributes
- Very specific: line 82
Prefer var for local variables whenever possible.
≈ “Common” is an estimate: at least 1 in 5 of the 1,644 Copilot instructions files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Copilot Instructions
Project Overview
Azure API Management policy toolkit: a set of C# libraries that let users author APIM policy documents in C# instead of XML, compile them to XML, and unit-test them with a gateway emulator. Prefer the newest stable .NET and C# versions possible while preserving repository compatibility.
Build, Test, Lint
dotnet build # Build entire solution
dotnet test # Run all tests
dotnet test --project test/Test.Core # Compiler tests only
dotnet test --project test/Test.Testing # Emulator tests only
dotnet test --filter "FullyQualifiedName~RateLimitTests" # Single test classTest framework: MSTest with FluentAssertions. Tests use [TestClass] / [TestMethod] / [DataRow].
Architecture
The toolkit has a three-layer pipeline for each policy:
- Authoring (
src/Authoring/) — C# records and interfaces that users write against- Config records in
Configs/{PolicyName}Config.csdefine the policy shape - Section interfaces (
IInboundContext,IOutboundContext,IBackendContext,IOnErrorContext,IFragmentContext) expose policy methods to users
- Config records in
- Compilation (
src/Core/) — Roslyn-based C#-to-XML transpiler- Compiler classes in
Compiling/Policy/{PolicyName}Compiler.csimplementIMethodPolicyHandler - Auto-registered via reflection on the
Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policynamespace — no manual DI wiring needed
- Compiler classes in
- Emulator (
src/Testing/) — In-memory gateway emulator for unit testing- Handler classes in
Emulator/Policies/{PolicyName}Handler.csextendPolicyHandler<T> - Discovered via reflection +
[Section(nameof(IInboundContext))]attributes GatewayContextis the central mock with section proxies, stores, and expression contextTestDocumentorchestrates test execution across policy sections
- Handler classes in
Supporting projects: src/Analyzers/ (Roslyn analyzers), src/Compiling/ (CLI compiler tool), src/Generators/ (source generators), src/Templates/ (project templates).
Adding a New Policy
Follow the guide in docs/AddPolicyGuide.md. Each policy requires coordinated changes across layers:
| Artifact | Location | Naming |
| Config record | src/Authoring/Configs/{PolicyName}Config.cs | {PolicyName}Config |
| Section interface method | src/Authoring/I{Section}Context.cs | {PolicyName}(config) |
| Compiler | src/Core/Compiling/Policy/{PolicyName}Compiler.cs | {PolicyName}Compiler |
| Compiler tests | test/Test.Core/Compiling/{PolicyName}Tests.cs | {PolicyName}Tests |
| Emulator handler | src/Testing/Emulator/Policies/{PolicyName}Handler.cs | {PolicyName}Handler |
| Emulator tests | test/Test.Testing/Emulator/Policies/{PolicyName}Tests.cs | {PolicyName}Tests |
Use structurally similar existing policies as references:
- Simple attributes:
RateLimitByKey - Attributes + child elements:
RateLimit - String-list children:
ValidateJwt - Complex nested configs:
Cors - Direct parameters (no config):
SetMethod
Key Conventions
Config Records
- Public records with
required initproperties for required params, nullable for optional [ExpressionAllowed]attribute on properties that accept policy expressions- XML doc comments on the record and every property
Compilers
- Must be
publicin namespaceMicrosoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policy(auto-registration) MethodNameproperty returnsnameof(IInboundContext.{PolicyName})- Use
TryExtractingConfigParameter<T>()to extract config,AddAttribute()for XML attributes - Report missing required params via
CompilationErrors.RequiredParameterNotDefined
Emulator Handlers
internalclasses with[Section(nameof(I{Section}Context))]attribute(s) — stack multiple for multi-section- Extend
PolicyHandler<TConfig>(single config),PolicyHandler<T1, T2>(two params), orPolicyHandlerOptionalParam<T>(optional config) PolicyNameproperty returnsnameof(IInboundContext.{PolicyName})
Tests
- Compiler tests use
[TestClass]with[DataRow]patterns, usecode.CompileDocument().Should().BeSuccessful().And.DocumentEquivalentTo(expectedXml) - Cover: required-only, each optional field, expression values for
[ExpressionAllowed]properties, each applicable section - Emulator tests use
.AsTestDocument(),SetupInbound(),.RunInbound()etc.
Code Style
- Copyright header:
// Copyright (c) Microsoft Corporation.+// Licensed under the MIT License. - Prefer
varfor local variables whenever possible; use explicit types only when they improve clarity - Private fields:
_camelCase, private static fields:s_camelCase - 4-space indentation, CRLF line endings, Allman braces
- Root namespace:
Microsoft.Azure.ApiManagement.PolicyToolkit
Copilot Skills and Agents
This repo has custom Copilot skills in .github/skills/ and agents in .github/agents/ for policy implementation workflows.
When asked to add a new policy, always use the appropriate agent rather than implementing manually:
@implement-policy-compiled— use this agent to add a new policy's authoring config, context interface method, compiler, and compiler tests. It follows a mandatory phased workflow (information gathering → branch → codebase discovery → planning → tests → authoring → compilation → commit).@implement-emulator-policy— use this agent to add or fix a gateway emulator handler and its tests. It follows a mandatory phased workflow (discovery → branch → tests → handler → commit).
Supporting skills (used by the agents, also available directly):
policy-authoring— config record and context interface conventionspolicy-compilation— compiler class patternspolicy-emulator— emulator handler patternspolicy-testing/policy-emulator-testing— test conventionspolicy-codebase-reference— file inventory and reference policy selection
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

