En net 4.5 estamos trabajando con proxy como este:
<system.net> <!-- --> <defaultProxy enabled="true" useDefaultCredentials="false"> <proxy usesystemdefault="True" proxyaddress="http://192.168.1.1:8888" bypassonlocal="True" autoDetect="False" /> <module type="CommonLibrary.Proxy.MyProxy, CommonLibrary, Version=1.0.0.0, Culture=neutral" /> </defaultProxy> <settings> <httpWebRequest useUnsafeHeaderParsing="true" /> <servicePointManager expect100Continue="false" /> </settings> </system.net>
pero en asp.net core o test no podemos encontrar una solución como la anterior. ¿Alguien podría ayudarme?
Realmente aprecio tu ayuda
Gracias y Saludos
Si bien la configuración manual del proxy funciona cuando es posible usar un HttpClientHander
, actualmente no es posible establecer de forma predeterminada todas las solicitudes para hacerlo sin código, como podría hacerlo en .NET Framework. Lo cual es un fastidio si está utilizando una biblioteca que no expone esta funcionalidad.
Afortunadamente, a partir de .NET Core 3.0, esto será posible simplemente configurando variables de entorno (es decir, se comporta exactamente como Linux siempre ha funcionado): https://github.com/dotnet/corefx/issues/37187
Alternativamente, https://github.com/dotnet/corefx/issues/36553 agregó una nueva propiedad estática DefaultWebProxy
en HttpClient
que le permitirá lograr lo mismo a través del código. Esto también estará disponible en Core 3.0.
Puede configurar el proxy explícitamente en web.config como variables de entorno, así como los dominios que debe omitir. Por ejemplo:
<?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\Your.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess"> <environmentVariables> <environmentVariable name="http_proxy" value="http://yourproxy.ins.local"/> <environmentVariable name="https_proxy" value="http://yourproxy.ins.local"/> <environmentVariable name="no_proxy" value=".local,.applicationinsights.azure.com,.applicationinsights.microsoft.com,.services.visualstudio.com"/> </environmentVariables> </aspNetCore> </system.webServer> </location> </configuration>
Para usar un proxy HTTP en .net core, debe implementar la interfaz IWebProxy
. Esto es del ensamblado System.Net.Primitives.dll
. Puede agregarlo a project.json
si aún no está allí
p.ej
"frameworks": { "dotnet4.5": { "dependencies": { "System.Net.Primitives": "4.3.0" } } }
La implementación es muy trivial.
public class MyHttpProxy : IWebProxy { public MyHttpProxy() { //here you can load it from your custom config settings this.ProxyUri = new Uri(proxyUri); } public Uri ProxyUri { get; set; } public ICredentials Credentials { get; set; } public Uri GetProxy(Uri destination) { return this.ProxyUri; } public bool IsBypassed(Uri host) { //you can proxy all requests or implement bypass urls based on config settings return false; } } var config = new HttpClientHandler { UseProxy = true, Proxy = new MyHttpProxy() }; //then you can simply pass the config to HttpClient var http = new HttpClient(config)
consulte https://msdn.microsoft.com/en-us/library/system.net.iwebproxy(v=vs.100).aspx