Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
At a glance
Goal: Build a custom Dev Proxy plugin
Time: 30 minutes
Plugins: Custom plugin
Prerequisites: Set up Dev Proxy, .NET 11 SDK
In this article, you learn how to create a custom plugin for the Dev Proxy. By creating plugins for Dev Proxy, you can extend its functionality and add custom features to fit your needs.
HTTP plugins vs. stdio plugins
Dev Proxy supports two types of plugins depending on the traffic you want to intercept:
HTTP plugins intercept HTTP(S) requests and responses between your app and APIs. They inherit from
BasePluginand override methods likeBeforeRequestAsyncandBeforeResponseAsync. Use HTTP plugins when you want to simulate API errors, add mock responses, validate request headers, or otherwise inspect and modify HTTP traffic.Stdio plugins intercept messages sent over standard input/output (stdin, stdout, stderr) between a parent process and a child process. They implement the
IStdioPlugininterface (whichBasePluginalso implements) and override methods likeBeforeStdinAsync,AfterStdoutAsync, andAfterStderrAsync. Use stdio plugins when working with tools that communicate over stdio, such as Model Context Protocol (MCP) servers.
A single plugin class can handle both HTTP and stdio traffic by overriding methods from both sets.
Prerequisites
Before you start creating a custom plugin, make sure you have the following prerequisites:
Choose the target framework
The DevProxy.Abstractions package targets net10.0 and net11.0. Target the same .NET version as the Dev Proxy release that runs your plugin, or an earlier one. Dev Proxy can load a plugin built for an earlier .NET version, but not for a later one. Starting with v4.1.0, Dev Proxy runs on .NET 11, so you can build your plugin for either net10.0 or net11.0. If you have an existing plugin that targets net10.0, you don't need to change it.
The examples in this article use net11.0. If your plugin targets net10.0, use net10.0 in the pluginPath instead.
Create a new plugin
Follow the next steps to create a new project:
Create a new class library project using the
dotnet new classlibcommand.dotnet new classlib -n MyCustomPluginOpen the newly created project in Visual Studio Code.
code MyCustomPluginAdd the Dev Proxy Abstractions NuGet package to your project.
dotnet add package DevProxy.AbstractionsExclude the shared dependency dynamic-link libraries (DLLs) from the build output. Dev Proxy already loads
DevProxy.Abstractionsand theMicrosoft.Extensions.*packages that it depends on. If your plugin ships its own copy of these assemblies, Dev Proxy loads a second copy, and your plugin's types don't match Dev Proxy's types. In theMyCustomPlugin.csprojfile, add thePrivateandExcludeAssetstags to thePackageReferenceofDevProxy.Abstractionsand of every shared package that your plugin references.<ItemGroup> <PackageReference Include="DevProxy.Abstractions" Version="x.y.z"> <Private>false</Private> <ExcludeAssets>runtime</ExcludeAssets> </PackageReference> <PackageReference Include="Microsoft.Extensions.Configuration" Version="x.y.z"> <Private>false</Private> <ExcludeAssets>runtime</ExcludeAssets> </PackageReference> </ItemGroup>Create a new class that inherits from the
BasePluginclass.using DevProxy.Abstractions.Plugins; using DevProxy.Abstractions.Proxy; using Microsoft.Extensions.Logging; namespace MyCustomPlugin; public sealed class CatchApiCallsPlugin( ILogger<CatchApiCallsPlugin> logger, ISet<UrlToWatch> urlsToWatch) : BasePlugin(logger, urlsToWatch) { public override string Name => nameof(CatchApiCallsPlugin); public override Task BeforeRequestAsync(ProxyRequestArgs e, CancellationToken cancellationToken) { Logger.LogTrace("{Method} called", nameof(BeforeRequestAsync)); ArgumentNullException.ThrowIfNull(e); if (!e.HasRequestUrlMatch(UrlsToWatch)) { Logger.LogRequest("URL not matched", MessageType.Skipped, new(e.ProxySession)); return Task.CompletedTask; } var headers = e.ProxySession.Request.Headers; var header = headers.Where(h => h.Name == "Authorization").FirstOrDefault(); if (header is null) { Logger.LogRequest($"Does not contain the Authorization header", MessageType.Warning, new LoggingContext(e.ProxySession)); return Task.CompletedTask; } Logger.LogTrace("Left {Name}", nameof(BeforeRequestAsync)); return Task.CompletedTask; } }Build your project.
dotnet build
Use your custom plugin
To use your custom plugin, you need to add it to the Dev Proxy configuration file:
Add the new plugin configuration in the
devproxyrc.jsonfile.File: devproxyrc.json
{ "plugins": [{ "name": "CatchApiCallsPlugin", "enabled": true, "pluginPath": "./bin/Debug/net11.0/MyCustomPlugin.dll", }] }Run the Dev Proxy.
devproxy
The example plugin checks all matching URLs for the required Authorization header. If the header isn't present, it shows a warning message.
Tip
To verify that Dev Proxy can load your plugin and to see the plugin names to use in the configuration file, run devproxy plugin list --plugin-path ./bin/Debug/net10.0/MyCustomPlugin.dll. For more information, see plugin list.
Adding custom configuration to your plugin (optional)
You can extend your plugin's logic by adding custom configuration:
Inherit from the
BasePlugin<TConfiguration>class. Dev Proxy exposes at runtime parsed plugin configuration through theConfigurationproperty.using DevProxy.Abstractions.Plugins; using DevProxy.Abstractions.Proxy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; namespace MyCustomPlugin; public sealed class CatchApiCallsConfiguration { public string? RequiredHeader { get; set; } } public sealed class CatchApiCallsPlugin( HttpClient httpClient, ILogger<CatchApiCallsPlugin> logger, ISet<UrlToWatch> urlsToWatch, IProxyConfiguration proxyConfiguration, IConfigurationSection pluginConfigurationSection) : BasePlugin<CatchApiCallsConfiguration>( httpClient, logger, urlsToWatch, proxyConfiguration, pluginConfigurationSection) { public override string Name => nameof(CatchApiCallsPlugin); public override Task BeforeRequestAsync(ProxyRequestArgs e, CancellationToken cancellationToken) { Logger.LogTrace("{Method} called", nameof(BeforeRequestAsync)); ArgumentNullException.ThrowIfNull(e); if (!e.HasRequestUrlMatch(UrlsToWatch)) { Logger.LogRequest("URL not matched", MessageType.Skipped, new(e.ProxySession)); return Task.CompletedTask; } // Start using your custom configuration var requiredHeader = Configuration.RequiredHeader ?? string.Empty; if (string.IsNullOrEmpty(requiredHeader)) { // Required header is not set, so we don't need to do anything Logger.LogRequest("Required header not set", MessageType.Skipped, new LoggingContext(e.ProxySession)); return Task.CompletedTask; } var headers = e.ProxySession.Request.Headers; var header = headers.Where(h => h.Name == requiredHeader).FirstOrDefault(); if (header is null) { Logger.LogRequest($"Does not contain the {requiredHeader} header", MessageType.Warning, new LoggingContext(e.ProxySession)); return Task.CompletedTask; } Logger.LogTrace("Left {Name}", nameof(BeforeRequestAsync)); return Task.CompletedTask; } }Build your project.
dotnet buildUpdate your
devproxyrc.jsonfile to include the new configuration.File: devproxyrc.json
{ "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json", "plugins": [{ "name": "CatchApiCallsPlugin", "enabled": true, "pluginPath": "./bin/Debug/net11.0/MyCustomPlugin.dll", "configSection": "catchApiCalls" }], "catchApiCalls": { "requiredHeader": "Authorization" } }Run the Dev Proxy.
devproxy