Bisher ist die einzige einigermaßen saubere Lösung, die ich gefunden habe, die Suche nach einem angeschlossenen Debugger. Das ist jedoch nicht ideal, da wir diese Anwendungen auch lokal ausführen, ohne dass der Debugger angeschlossen ist.
Eine andere Möglichkeit wäre, eine Middleware zu verwenden und dann die URL auf „localhost“ zu überprüfen, aber das erscheint übertrieben.
Alle Ideen wären willkommen!
Code: Select all
public void ConfigureServices(IServiceCollection services)
{
if (/*Some clean way to determine IsLocal*/)
{
//Need to provide proxy credentials for local development only
var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
}
}
Update Ich habe mich für die Verwendung von lauchSetting.json entschieden, was ebenfalls nicht perfekt ist, aber für meinen Fall am besten passt . Ich werde alle Optionen und ihre Vor- und Nachteile für jeden zukünftigen Entwickler in derselben Position skizzieren.
Option 1: Lokale Umgebung hinzufügen
Probleme: Wenn Sie bereits umgebungsspezifische Konfigurationen haben, werden Sie Zeit damit verbringen, die Konfiguration zu kopieren und einzufügen, wenn Sie „Staging“ aus Ihrer „lokalen“ Konfigurationsdatei als Ziel festlegen möchten .
Code
Code: Select all
public void ConfigureServices(IServiceCollection services)
{
if (HostingEnvironment.IsEnvironment("Local"))
{
//Need to provide proxy credentials for local development only
var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
}
}
Option 2: Erstellen Sie eine Middleware, nach der gesucht werden soll localhost in der Anfrage-URL
Probleme: Abhängig von der Implementierung wird es möglicherweise zu oft ausgeführt. Es funktioniert auch nicht, wenn Ihre lokale Version nicht „localhost“ in der URL verwendet.
Option 3: Verwenden Sie launchSettings.json, um eine Umgebungsvariable unterzubringen.
Probleme: Abhängig von Ihrer IDE funktioniert dies möglicherweise nicht. Es wird davon ausgegangen, dass Sie Ihre launchSettings.json-Datei festschreiben.
Code
Code: Select all
{
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"IsLocal": "True"
}
}
}
}
Code: Select all
public void ConfigureServices(IServiceCollection services)
{
if (Environment.GetEnvironmentVariable("IsLocal") == "True")
{
//Need to provide proxy credentials for local development only
var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
}
}
Probleme: Erfordert zusätzliche Entwicklereinrichtung über das Auschecken von Code und das Ausführen von
Option 4: Verwenden Sie Maschinennamen, um festzustellen, ob Entwickler oder Server
Probleme: Geht davon aus, dass alle Entwicklermaschinen irgendeine Art von Standard verwenden.
Code
Code: Select all
public void ConfigureServices(IServiceCollection services)
{
if (Environment.MachineName.StartsWith("DEV", StringComparison.OrdinalIgnoreCase))
{
//Need to provide proxy credentials for local development only
var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
}
}
Probleme: Debugger ist nicht verfügbar Es ist nicht immer angehängt, wenn Sie lokal
Code
ausführen
Code: Select all
public void ConfigureServices(IServiceCollection services)
{
if (Debugger.IsAttached)
{
//Need to provide proxy credentials for local development only
var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
}
}
Probleme: Möglicherweise nicht erwünscht zum Kompilieren im Debug in jedem Szenario
Code
Code: Select all
#if(DEBUG)
//Need to provide proxy credentials for local development only
var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
#endif