Showing posts with label Security Protocol. Show all posts
Showing posts with label Security Protocol. Show all posts

Wednesday, March 27, 2019

how to fix a console app implemented with lower version .net framework cannot comsume a web api developed with higher version of .net host in the Azure?

currently we have app implmented with a .net framework 4.5, it is running fine in the local desktop during the development. however the exceptions had thrown after it deployed to the host server.

"System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at DealerETL.Program.Main(String[] args)

Inner Exception 1:
CommunicationException: An error occurred while receiving the HTTP response to http://dax:8080/MicrosoftDynamicsAXAif60/SubDealerQRService/xppservice.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

Inner Exception 2:
WebException: The underlying connection was closed: An unexpected error occurred on a receive.

Inner Exception 3:
IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

Inner Exception 4:
SocketException: An existing connection was forcibly closed by the remote host
"

the root cause of this issue stems from the Web Api had been implemented with higher version of .net framework 4.6.2 and the console had been implemented with .net framwork 4.5.

by default the security protocol had been set to the Tls which has value 192.


namespace System.Net
{
    [System.Flags]
    public enum SecurityProtocolType
    {
       Ssl3 = 48,
       Tls = 192,
       Tls11 = 768,
       Tls12 = 3072,
    }
}

after we manually set the security protocol in the code. the issue is fixed.

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072 |(SecurityProtocolType)768;





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;