Developing custom plugins for the Microsoft's Dev Proxy
For a training project I was working on, I needed to be able to intercept some API calls for some audit logging. To do this, I decided to use Microsoft’s Dev Proxy tool, which you use to simulate, mock, and test APIs.
As the Dev Proxy did not have the functionality I needed out of the box, I decided to develop a custom plugin with the help of Waldek Mastykarz.
In this blog post, I will show you how you can develop custom plugins for the Dev Proxy.
Things you need to get started
Before you can start developing custom plugins for the Dev Proxy, you need to have the following tools installed:
Similar to the dev-proxy-abstractions.dll configuration, make sure to exclude the new packages’ assets by adding the ExcludeAssets property to the PackageReference element in the DevProxyCustomPlugin.csproj file:
Create a new method, OnBeforeRequest to handle the event:
Add the OnBeforeRequest method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
privatevoidOnBeforeRequest(objectsender,BeforeRequestEventArgse){if(_urlsToWatchisnull||!e.HasRequestUrlMatch(_urlsToWatch)){// No match for the URL, so we don't need to do anythingreturnTask.CompletedTask;}// Replace the following lines with your custom logicif(e.Session.HttpClient.Request.RequestUri.AbsoluteUri.Contains("https://frontmatter.codes")){varurl=e.Session.HttpClient.Request.RequestUri.AbsoluteUri;e.Session.HttpClient.Request.RequestUri=newUri(url.Replace("https://frontmatter.codes","http://localhost:3000"));}returnTask.CompletedTask;}
In this example, the OnBeforeRequest method checks if the request URL contains https://frontmatter.codes. If it does, it replaces the URL with http://localhost:3000. That way, I can test the API calls locally while developing the project.
info
Change the code in the OnBeforeRequest method to fit your needs.
Once you have added the OnBeforeRequest method’s code, you can build the plugin by running the following command in the terminal:
Build the plugin
1
dotnet build
Testing the plugin
To test the plugin, add it to the Dev Proxy configuration. You can do this by adding the following lines to the devproxyrc.json file:
Start the Dev Proxy by running the following command in the terminal:
Start the Dev Proxy
1
devproxy
Now, you can test the plugin by requesting the URL specified in the urlsToWatch configuration. In my example, when I call the https://frontmatter.codes/api/stars API, the request gets redirected to http://localhost:5000/api/stars, and I can see the response from my local API.
Adding some plugin configuration
To make the plugin more flexible, you can add some configuration options. For instance, you can define the root URL and the local URL to redirect the API calls to.
To do this, you can add the following configuration to the devproxyrc.json file:
Add the plugin configuration to the Dev Proxy configuration
In the RedirectCalls plugin sections, I added a configSection property with the value redirectCalls. This configuration allows me to access the plugin’s configuration.
In the redirectCalls section, I added the fromUrl and toUrl properties, which define the root URL and the local URL to which the API calls should be redirected.
To access the configuration in the plugin, you need to bind the configuration. The whole code for the plugin class should look like this:
usingMicrosoft.DevProxy.Abstractions;usingMicrosoft.Extensions.Configuration;namespaceDevProxyCustomPlugin;publicclassRedirectCallsConfiguration{publicstring?FromUrl{get;set;}publicstring?ToUrl{get;set;}}publicclassRedirectCalls:BaseProxyPlugin{publicoverridestringName=>nameof(RedirectCalls);privatereadonlyRedirectCallsConfiguration_configuration=new();publicoverridevoidRegister(IPluginEventspluginEvents,IProxyContextcontext,ISet<UrlToWatch>urlsToWatch,IConfigurationSection?configSection=null){base.Register(pluginEvents,context,urlsToWatch,configSection);configSection?.Bind(_configuration);pluginEvents.BeforeRequest+=OnBeforeRequest;}privateTaskOnBeforeRequest(objectsender,ProxyRequestArgse){if(_urlsToWatchisnull||!e.HasRequestUrlMatch(_urlsToWatch)){// No match for the URL, so we don't need to do anythingreturnTask.CompletedTask;}varfromUrl=_configuration?.FromUrl??string.Empty;vartoUrl=_configuration?.ToUrl??string.Empty;if(string.IsNullOrEmpty(fromUrl)||string.IsNullOrEmpty(toUrl)){returnTask.CompletedTask;}if(e.Session.HttpClient.Request.RequestUri.AbsoluteUri.Contains(fromUrl)){varurl=e.Session.HttpClient.Request.RequestUri.AbsoluteUri;e.Session.HttpClient.Request.RequestUri=newUri(url.Replace(fromUrl,toUrl));}returnTask.CompletedTask;}}
With this configuration in place, you can test the plugin with the new configuration options.
Developing custom plugins for Microsoft’s Dev Proxy is a great way to extend the tool’s functionality. With the help of the Dev Proxy Abstractions DLL, you can easily create custom plugins that can intercept API calls, modify requests and responses, and much more.