Thursday, November 29, 2018

how to consume a web api implemented by lastest .Net framework from a web applicaton from a older .Net framework?

when i try to call a web api that implemented by a .Net framework 4.6 and above.

i always receive the error message "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." in response immediately.




the root cause of this error is the Web API implemented with .Net Framework 4.6 and later use the Security Protocol 1.2 by default.and Web application implemented with .net framework 4.5 and earlier will use security protocol 1.1 by default..

the fix of this issue is very easy. we just have to setup the security protocol before calling the web api.

the following snippet of code will do the job

ServicePointManager.Expect100Continue = true;
               ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                      | SecurityProtocolType.Tls11
                      | SecurityProtocolType.Tls12
                      | SecurityProtocolType.Ssl3;