microsoft / Dynamics-AX-Integration

Dynamics AX Integration samples and demos.
287 stars 356 forks source link

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. #12

Closed Sharran closed 7 years ago

Sharran commented 7 years ago

We are using AX Data job Processor the URL is pointed towards Production BOX(we are running this from DEV Box).

When we drop the file in the input path, the file is processed and moved into In process folder.

After that we are not getting response from the production server. getting the below stated error. "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." This happens only in production box, works fine in dev and sandbox.

Kindly help us to resolve this issue.

ffilardi commented 7 years ago

Same for me. Works fine only on environments deployed on my own subscription but I get the same error when connecting to environments deployed on Microsoft's subscription - including sandboxes.

InnerException {"An existing connection was forcibly closed by the remote host"}

ErrorCode 10054 int HResult -2147467259 int Message "An existing connection was forcibly closed by the remote host" string NativeErrorCode 10054 int

SocketErrorCode ConnectionReset System.Net.Sockets.SocketError StackTrace " at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)\r\n at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)" string TargetSite {Int32 EndReceive(System.IAsyncResult)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

Message "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." string StackTrace " at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)\r\n at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)\r\n at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)" string

RaphaelTagliani commented 7 years ago

What version of TLS do you use? I recently had this kind of issue, and it was due to a recent modification : it is now necessary to connect using TLS 1.2 (explained to me by MS).

You can check easily, by compiling with .Net framework 4.6 (it will use TLS 1.2 first).

RaphaelTagliani commented 7 years ago
public class OAuthHelper
    {
        /// <summary>
        /// Retrieves an authentication header from the service.
        /// </summary>
        /// <returns>The authentication header for the Web API call.</returns>
        public static async Task<string> GetAuthenticationHeader(WebServiceClientConfiguration wsc)
        {
            // enforce TLS 1.2 : this code could be executed only once, but I prefer to reset it before every call, to make sure that this setting will
            // not be altered by another framework component in the meantime
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            AuthenticationContext authenticationContext = new AuthenticationContext(wsc.ActiveDirectoryTenant);
            ClientCredential clientCredential = new ClientCredential(wsc.ClientAppId, wsc.ClientKey);
            UserPasswordCredential upc = new UserPasswordCredential(wsc.UserName, wsc.Password);

            // ADAL includes an in memory cache, so this call will only send a message to the server if the cached token is expired.
            AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(wsc.ActiveDirectoryResource, wsc.ClientAppId, upc);

            return authenticationResult.CreateAuthorizationHeader();
        }
    }
ffilardi commented 7 years ago

Thanks Raphael, compiling using framework 4.6 worked fine!

RaphaelTagliani commented 7 years ago

Nice Fabio, Thank you for letting me know :)

srpraveenkumar commented 7 years ago

Thank you @RaphaelTagliani for providing the solution. Yes, TLS 1.2 should be used to connect to Tier2+ environments.

demonspear commented 6 years ago

thank you @RaphaelTagliani . You made my day, bro. I've got same error with send request from httpclient. Then i set project to framework 4.6, it's work fine. Thanks again!