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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Call external Webservice from Plugin --> Error

(0) ShareShare
ReportReport
Posted on by 181

hi,

I need to call an external webservice from within a Dynamics CE Online Plugin.

The code just works fine when I run it in a console-app.

But when I run it inside the plugin (on create) I get the following error:

System.AggregateException: One or more errors occurred. ---> 
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> 
System.Net.WebException: Unable to connect to the remote server ---> 
System.Net.Sockets.SocketException: 
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection 
failed because connected host has failed to respond xxx.xxx.xxx.xxx:443

This is the code to consume the webservice:

private async Task PostAsync(string relativeUrl, object data, string mediaType)
{
    var jsonString = JsonSerializer.Serialize(data);
    var content = new StringContent(jsonString, Encoding.UTF8, mediaType);
    var result = await _httpClient.PostAsync(relativeUrl, content).ConfigureAwait(false);

    var resultString = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
    Log("result="   resultString);
    result.EnsureSuccessStatusCode();
    var resultObject = JsonSerializer.Deserialize(resultString);

    return resultObject;
}

Its an https-webservice.

Any ideas?

I have the same question (0)
  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Try converting your relativeUrl from string to a URI object and see if that fixes your issue

    var result = await _httpClient.PostAsync(relativeUrl, content).ConfigureAwait(false);

    Eg : Uri myUri = new Uri(URLInStringFormat, UriKind.Absolute);

  • Community Member Profile Picture
    on at

    You need to debug this.. If you can't hit your breakpoint, it means your interface doesn't match the request you're trying to make, or your web.config file could be incorrect as well. What are you trying to accomplish here?

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Can you please modify your code based on below sample code from Microsoft.

    using (HttpClient client = new HttpClient())

             {

               client.Timeout = TimeSpan.FromMilliseconds(15000); //15 seconds

               client.DefaultRequestHeaders.ConnectionClose = true; //Set KeepAlive to false

               HttpResponseMessage response =  client.GetAsync(webAddress).Result; //Make sure it is synchonrous

               response.EnsureSuccessStatusCode();

               string responseText = response.Content.ReadAsStringAsync().Result; //Make sure it is synchonrous

               tracingService.Trace(responseText);

               //Log success in the Plugin Trace Log:

               tracingService.Trace("HttpClientPlugin completed successfully.");

             }

    Set below two properties in your code

    client.Timeout = TimeSpan.FromMilliseconds(15000); //15 seconds

    client.DefaultRequestHeaders.ConnectionClose = true; //Set KeepAlive to false

    Make sure your call to external web service is synchronous.

    github.com/.../HttpClientPlugin.cs

    Please mark my answer verified if i were helpful

  • TKoller Profile Picture
    181 on at

    thanks for your suggestions but in the end it was just a firewall problem on the webservice-provider-side.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans