Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

call external web service from c#

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

HI,

I have external service referenced in c#class library. i have  added  dll  in dynamics ax  folder  and  added  dll references to dynamics ax.  I have global variable    ServiceReference1.NtosServiceSoapClient se;

if I add  code  se = new ServiceReference1.NtosServiceSoapClient() in c# class library;  dynamics ax  throws error CLR Object couldn't be created , on that line.

I tried to use different approach and  initialized variable like this

 var remoteAddress = new System.ServiceModel.EndpointAddress("www.revenue.mof.ge/.../ntosservice.asmx");
           
 System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
 binding.MaxReceivedMessageSize = Int32.MaxValue;
 se= new ServiceReference1.NtosServiceSoapClient(binding, remoteAddress);

if I declare variable like this , code works, But It throws exception after calling service methods too many times in a little timespan
SERVER closes connection , because too many http requests are made. Production server uses ForeFront TMG to restrics service calls;

i am interested if there is any chance to declare variaible without specifying http service reference in variable initialization;

I need to call c# class methods from x++ , that calls service methods in c# . I don't want to call service methods from x++;

can anyone tell me  why  this error might occuring ?CLR Object couldn't be created  .  I have a guess, that  code  doesn't see config file , but  I have added  config file with dll , in bin folder

*This post is locked for comments

  • Martin Dráb Profile Picture
    Martin Dráb 230,900 Most Valuable Professional on at
    RE: call external web service from c#

    It seems that the constructor of your C# class throws an exception. Debug the C# code called from X++.

    I can't comment on the error about crashing AX without more information about what you're doing.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: call external web service from c#

    I tried  , deploying  service from visual studio .  but  that  was not  an issue .  I am   just  testing  things in my server .

    System.Reflection.TargetInvocationException: Exception has been thrown by 
        the target of an invocation. ---> 
        System.ServiceModel.CommunicationException: The underlying connection was 
      closed: The connection was closed unexpectedly. ---> System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
       at System.Net.HttpWebRequest.GetResponse()
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       --- End of inner exception stack trace ---
    Server stack trace: 
       at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)


    Above is  the error  which  is thrown  by  Code  after  calling  service methods  about  500 - 600 times. 

    I have  written 

    se = new ServiceReference1.NtosServiceSoapClient()

    in  C# class constructor .  so  if i   write   new  Myclass()(c# class referenced in dax)  in x++ it  says  clr object  couldn't be created .  when i debug  with visual studio  it doesnt't   give me more  information regarding  this  error,  just  stops process on  above line .

    I have  written  solution in x++ .But  I want  to  rewrite  solution in c# , because  there  is  very unusual case , which  I cant  figure out .  if  I  put  breakpoint  in x++  on service call   , dynamics  ax  crashes  .  i   am  pressing (debug  to visual studio  button)  and   in visual studio  error  is saying  

    Unhandled exception at 0x76FF5934 (ntdll.dll) in ax.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77011378).

     i  hit continue button and  everything works fine after that .  in  user side ,  it can crash  unexpectedly  and   with same error.   this is the reason   I want to write  solution in c#  . 

  • Martin Dráb Profile Picture
    Martin Dráb 230,900 Most Valuable Professional on at
    RE: call external web service from c#

    A better approach than dealing with the DLL file manually (imagine how you'll deploy it to clustered AOS servers in production...) is adding the VS project to AOT and configure it for automatic deployment.

    Regarding the error about too many requests, it would help if you mentioned the exact type of exception and message, but it sounds like that you're not closing the channel after making requests. I suggest you review you code in this area.

    Regarding your other error, you said that you added "se = new ServiceReference1.NtosServiceSoapClient() in c# class library" and then you got an error in AX, but you didn't tell us anything about your code in AX. Please add this information; we can't find bugs in completely unknown X++ code.

    It's possible to load information about endpoint address and binding from the configuration file, you just have to create the client in the recommended way, i.e. via AifUtil::createServiceClient(). You may want to look at an example in documentation: Walkthrough: Calling an External Web Service from X++.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,074 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,900 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans