gebeurtenis
Microsoft 365 Community Conference
6 mei, 14 - 9 mei, 00
Vaardigheid voor het tijdperk van AI op het ultieme Microsoft 365-evenement, 6-8 mei in Las Vegas.
Meer informatieDeze browser wordt niet meer ondersteund.
Upgrade naar Microsoft Edge om te profiteren van de nieuwste functies, beveiligingsupdates en technische ondersteuning.
In dit artikel leert u hoe u een aangepaste invoegtoepassing maakt voor de Dev Proxy. Door invoegtoepassingen voor Dev Proxy te maken, kunt u de functionaliteit uitbreiden en aangepaste functies toevoegen aan uw behoeften.
Voordat u begint met het maken van een aangepaste invoegtoepassing, moet u ervoor zorgen dat u aan de volgende vereisten voldoet:
Volg de volgende stappen om een nieuw project te maken:
Maak een nieuw klassebibliotheekproject met behulp van de dotnet new classlib
opdracht.
dotnet new classlib -n MyCustomPlugin
Open het zojuist gemaakte project in Visual Studio Code.
code MyCustomPlugin
Voeg het DLL-bestand Dev Proxy Abstractions (dev-proxy-abstractions.dll
) toe aan de projectmap.
Voeg het dev-proxy-abstractions.dll
bestand toe als verwijzing naar het projectbestand DevProxyCustomPlugin.csproj
.
<ItemGroup>
<Reference Include="dev-proxy-abstractions">
<HintPath>.\dev-proxy-abstractions.dll</HintPath>
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</Reference>
</ItemGroup>
Voeg de NuGet-pakketten toe die vereist zijn voor uw project.
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.Binder
dotnet add package Microsoft.Extensions.Logging.Abstractions
dotnet add package Unobtanium.Web.Proxy
Sluit de afhankelijkheids-DLL's uit van de build-uitvoer door een ExcludeAssets
tag per PackageReference
in het DevProxyCustomPlugin.csproj
bestand toe te voegen.
<ExcludeAssets>runtime</ExcludeAssets>
Maak een nieuwe klasse waarmee de BaseProxyPlugin
interface wordt geïmplementeerd.
using Microsoft.DevProxy.Abstractions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace MyCustomPlugin;
public class CatchApiCalls(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> UrlsToWatch, IConfigurationSection? configSection = null) : BaseProxyPlugin(pluginEvents, context, logger, UrlsToWatch, configSection)
{
public override string Name => nameof(CatchApiCalls);
public override async Task RegisterAsync()
{
await base.RegisterAsync();
PluginEvents.BeforeRequest += BeforeRequestAsync;
}
private Task BeforeRequestAsync(object sender, ProxyRequestArgs e)
{
if (UrlsToWatch is null ||
!e.HasRequestUrlMatch(UrlsToWatch))
{
// No match for the URL, so we don't need to do anything
Logger.LogRequest("URL not matched", MessageType.Skipped, new LoggingContext(e.Session));
return Task.CompletedTask;
}
var headers = e.Session.HttpClient.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.Session));
return Task.CompletedTask;
}
return Task.CompletedTask;
}
}
Bouw uw project.
dotnet build
Als u uw aangepaste invoegtoepassing wilt gebruiken, moet u deze toevoegen aan het configuratiebestand van de Dev Proxy:
Voeg de nieuwe configuratie van de invoegtoepassing toe aan het devproxyrc.json
bestand.
{
"plugins": [{
"name": "CatchApiCalls",
"enabled": true,
"pluginPath": "./bin/Debug/net8.0/MyCustomPlugin.dll",
}]
}
Voer de dev-proxy uit.
devproxy
De voorbeeldinvoegtoepassing controleert alle overeenkomende URL's voor de vereiste autorisatieheader. Als de koptekst niet aanwezig is, wordt er een waarschuwingsbericht weergegeven.
U kunt de logica van uw invoegtoepassing uitbreiden door aangepaste configuratie toe te voegen:
Voeg een nieuw _configuration
object toe en bind het in de Register
methode.
using Microsoft.DevProxy.Abstractions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace MyCustomPlugin;
public class CatchApiCallsConfiguration
{
public string? RequiredHeader { get; set; }
}
public class CatchApiCalls(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> UrlsToWatch, IConfigurationSection? configSection = null) : BaseProxyPlugin(pluginEvents, context, logger, UrlsToWatch, configSection)
{
public override string Name => nameof(CatchApiCalls);
// Define you custom configuration
private readonly CatchApiCallsConfiguration _configuration = new();
public override async Task RegisterAsync()
{
await base.RegisterAsync();
// Bind your plugin configuration
configSection?.Bind(_configuration);
// Register your event handlers
PluginEvents.BeforeRequest += BeforeRequestAsync;
}
private Task BeforeRequestAsync(object sender, ProxyRequestArgs e)
{
if (UrlsToWatch is null ||
!e.HasRequestUrlMatch(UrlsToWatch))
{
// No match for the URL, so we don't need to do anything
Logger.LogRequest("URL not matched", MessageType.Skipped, new LoggingContext(e.Session));
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.Session));
return Task.CompletedTask;
}
var headers = e.Session.HttpClient.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.Session));
return Task.CompletedTask;
}
return Task.CompletedTask;
}
}
Bouw uw project.
dotnet build
Werk uw devproxyrc.json
bestand bij om de nieuwe configuratie op te nemen.
{
"plugins": [{
"name": "CatchApiCalls",
"enabled": true,
"pluginPath": "./bin/Debug/net8.0/MyCustomPlugin.dll",
"configSection": "catchApiCalls"
}],
"catchApiCalls": {
"requiredHeader": "Authorization" // Example configuration
}
}
Voer de dev-proxy uit.
devproxy
Dev Proxy-feedback
Dev Proxy is een open source project. Selecteer een koppeling om feedback te geven:
gebeurtenis
Microsoft 365 Community Conference
6 mei, 14 - 9 mei, 00
Vaardigheid voor het tijdperk van AI op het ultieme Microsoft 365-evenement, 6-8 mei in Las Vegas.
Meer informatieTraining
Module
Build a declarative agent for Microsoft 365 Copilot optimized for a specific scenario. Bring actions to your agent with API plugins to access external data in real-time.
Documentatie
MockGeneratorPlugin - Dev Proxy
MockGeneratorPlugin-verwijzing
Gesimuleerde antwoorden - Dev Proxy
API-antwoorden simuleren
MockResponsePlugin - Dev Proxy
Referentie voor MockResponsePlugin