web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

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

(0) ShareShare
ReportReport
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?

I have the same question (0)
  • Suggested answer
    Radomir Skrabal Profile Picture
    5 on at

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 676

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 462 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 335 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans