为什么代码在c#Interactive(Roslyn)和debug\release模式下表现不同?

为什么代码在c#Interactive(Roslyn)和debug\release模式下表现不同?,c#,https,webclient,tls1.2,C#,Https,Webclient,Tls1.2,更新 实际上,在深入研究网络监控之后,我发现Roslyn执行的代码和正常的调试\发布模式发出请求的方式有所不同。 在调试\发布模式下,Fiddler跟踪错误: [Fiddler] The connection to 'host.com' failed. <br/>System.Security.SecurityException Failed to negotiate HTTPS connection with server.fiddler.network.https&gt

更新 实际上,在深入研究网络监控之后,我发现Roslyn执行的代码和正常的调试\发布模式发出请求的方式有所不同。 在调试\发布模式下,Fiddler跟踪错误:

[Fiddler] The connection to 'host.com' failed.  <br/>System.Security.SecurityException Failed to negotiate HTTPS connection with server.fiddler.network.https&gt; HTTPS handshake to host.com (for #76) failed. System.IO.IOException Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. &lt; An existing connection was forcibly closed by the remote host
其中GetUrl是一些包装器:

public bool GetURL(string URL)
        {
            try
            {
                web.Headers.Add("User-Agent", browserAgent);
                byte[] data = web.DownloadData(URL);
                string characterSet = web.Response.CharacterSet;
                string encoding = (characterSet == null || characterSet == "" || characterSet.ToUpper() == "NONE") ? "UTF-8" : web.Response.CharacterSet.ToUpper();
                html = Encoding.GetEncoding(encoding).GetString(data);
                return true;
            }
            catch (Exception ex)
            {
                lastException = web.LastException == null ? new WebException(ex.Message) : web.LastException;
            }
            return false;
        }
c#交互模式下的行为:

GetURL返回true,html包含数据

调试\发布模式下的行为:

GetURL返回false,ex包含以下错误:

内部异常:

 at System.Net.WebClient.DownloadBitsState.SetResponse(WebResponse response)
   at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)

请提供建议

解决方案

我假设C#interactive以某种方式预先配置,它能够将ServicePointManager.SecurityProtocol默认为旧版本。作为决议,我在代码中添加:

using System.Net;
....
ServicePointManager.SecurityProtocol = 
       SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
 at System.Net.WebClient.DownloadBitsState.SetResponse(WebResponse response)
   at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
using System.Net;
....
ServicePointManager.SecurityProtocol = 
       SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;