Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Remote party has closed the transport stream on calling RESTful API via managed code AX2012

Posted on by 5

Hello,

I am trying to do a Proof of concept call of RESTful API on our customer DEV server (AX 2012 R3).

Following this suggestion I created a managed DLL to call the web service. I based it on this tutorial, calling a public RESTful API (due to environment I am limited to .Net Framework 4.5)

using System;
using System.Collections.Generic;
using System.Net;
using System.IO;

using System.Runtime.Serialization;

namespace ConsumeRestApiTest
{
    public class TestWebAPI
    {
        public static String request()
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://api.github.com/repos/restsharp/restsharp/releases"));
            request.Method = "GET";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            var content = string.Empty;

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(stream))
                    {
                        content = sr.ReadToEnd();
                    }
                }
            }

            return content;
        }
    }
}

Now, directly on the server where environment is located, when I call the code, everything works fine, API is called and response is returned. Just for the sake of completeness, here is the code calling .Net DLL from C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestSuite
{
    class Program
    {
        static void Main(string[] args)
        {
            String str = ConsumeRestApiTest.SPLTestWebAPI.request();

            Console.WriteLine(str);
        }
    }

However, when I try to add this DLL to AX and call 

request()
method I am getting a following error (innermost Exception I was able to dig up):

System.IO.IOException: Authentication failed because the remote party has closed the transport stream.
at System.Net.Security.SslState.StartReadFrame(Byte[ buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[ buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[ incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[ buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.TlsStream.CallProcessAuthentication(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[ buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[ buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)

Here is code calling the DLL:

static void testRestfulApi(Args _args)
{
    CodeAccessPermission permission;
    System.Collections.IEnumerable ienumerable;
    ConsumeRestApiTest.SPLTestWebAPI test;
    System.Exception ex;
    str rslt;
    ;

    permission = new InteropPermission(InteropKind::CLRInterop);

    permission.assert();

    try
    {
        rslt = ConsumeRestApiTest.SPLTestWebAPI::request();
    } catch(Exception::CLRError)
    {
        ex = ClrInterop::getLastException();
         if (ex != null)
         {
            ex = ex.get_InnerException();
            if (ex != null)
            {
                error(ex.ToString());
            }
        }
    }
    info(rslt);
}

From my web search, most suggestions were about TLS/SSL issues, but since my encryption and networking knowledge is limited I cannot say if this is something AX is doing (perhaps I am missing some setup) or if this is something in AX-OS interaction or anything else.

Anyone have any suggestions?

  • Suggested answer
    Radomir Skrabal Profile Picture
    Radomir Skrabal 5 on at
    RE: Remote party has closed the transport stream on calling RESTful API via managed code AX2012

    Hello,

    not a direct solution, but after some trial and error I called the API directly from X via managed code:

    public static void test2()
    {
        CodeAccessPermission permission;
        System.Net.HttpWebRequest request;
        System.String content;
        System.Net.HttpWebResponse response;
        System.IO.Stream stream;
        System.IO.StreamReader sr;
        ;
    
        permission = new InteropPermission(InteropKind::CLRInterop);
    
        permission.assert();
    
        System.Net.ServicePointManager::set_Expect100Continue(true);
        System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);
    
    
        request = System.Net.WebRequest::Create(@"https://api.github.com/repos/restsharp/restsharp/releases") as System.Net.HttpWebRequest;
        request.set_Method("GET");
        request.set_UserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
        request.set_AutomaticDecompression(System.Net.DecompressionMethods::Deflate);
        content = "";
    
        response = request.GetResponse();
        stream = response.GetResponseStream();
        sr = new System.IO.StreamReader(stream);
            
        content = sr.ReadToEnd();
        
        sr.Close();
        stream.Close();
        response.Close();
                
        info(content);
    }

    Which surprisingly works completely fine.

    So the issue is probably somewhere between AX and the DLL.

    Anyway this solution works for me just fine, perhaps it will help someone else with the same issue.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans