I have proxy problems with my .NET app. When running it locally behind a corporate proxy I cannot access the Cosmos DB since it receives the calls from my VPN IP and not the whitelisted proxy-server IP.
I manage my proxy settings with proxydetox which is running on localhost:3128 without authentication. The localhost is also set in my MacOS proxy settings so using the browser or tools like curl the proxy-server IP is used.
How can I make my dotnet app use the proxy settings? In the documentation of .NET it states that it usually uses the system wide proxy settings but this does not seem to be the case.
One way of using proxy settings here could be using a HttpClient
with the WebProxy
property. How can I make the Cosmos or Logger service use this HttpClient
or the proxy settings? The Startup.cs
looks like this:
public class Startup
{
public IConfiguration Configuration { get; }
private readonly IWebHostEnvironment _env;
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
_env = env;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
// other services...
services.AddCosmosDb(Configuration);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
}
The appsettings.json contains this:
{
"CosmosOptions": {
"AccessKey": "<Your Cosmos DB Access Key Here>",
"EndpointUri": "<Your Cosmos DB Endpoint URI Here>",
"DatabaseName": "MyBackend",
"DirectConnection": true,
"HasBulkExecutionEnabled": false,
"MaxRetryWaitTimeInSeconds": 30,
"Documents": [
{
"Name": "Feature",
"TimeToLiveDays": -1,
"PartitionKeyName": "pk",
"DocumentClass": "Feature",
"SetOfferThroughputOnStartup": false
}
]
}
}
Using the Gateway ConnectionMode solved this issue for me. Now my proxy configuration is successfully used, simply by setting: "DirectConnection": false
Injecting the WebProxy was not needed anymore but it´s possible by an own implementation of AddCosmosDB
and using a HttpClient or WebProxy as options as described in the other answer.