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 :
Microsoft Dynamics AX (Archived)

Json service response gives an exception in C# code.

(0) ShareShare
ReportReport
Posted on by 272

Hi,

I have a serviceContract Class in which i have a function which returns a string "HelloWorld".
I've made a service which refers to this class and the service operation is that of the method which returns the srting. And added this service to the service group and set its AutoDeploy to YES.

Now, on the client side, I've given the URI and other authorization details in the code which are working fine but it gives an exception at this part of the code where I need to get the response string from the method.

Here is the code snippet:

using AuthenticationUtility;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;

namespace OAuthXppConsoleApplication
{
class Program
{
public static string GetUserSessionOperationPath = ClientConfiguration.Default.UriString + "api/services/EgxCompanyInfoServiceGroup/EgxCompanyInfoService/getHello";

static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ClientConfiguration.Default.UriString);
client.DefaultRequestHeaders.Add(OAuthHelper.OAuthHeader, OAuthHelper.GetAuthenticationHeader());

using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "api/services/EgxCompanyInfoServiceGroup/EgxCompanyInfoService/getHello"))
{
request.Content = new StringContent(String.Empty, Encoding.UTF8, "application/json");
var httpResponse = client.SendAsync(request).Result;
httpResponse.EnsureSuccessStatusCode();

var result = httpResponse.Content.ReadAsStringAsync().Result;
Console.WriteLine(result.ToString());

}

Console.ReadLine();

}
}
}


I've tried with both methods, post and get but they return different exceptions and not the result.
What is wrong in the code? Can anyone explain?

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    You forgot to give us the most important piece of information - what exception (type, message) you got. Please add it.

    Let me also make your code a bit more readable, before we discuss it.

    using AuthenticationUtility;
    using Newtonsoft.Json;
    using System;
    using System.IO;
    using System.Net;
    using System.Net.Http;
    using System.Text;
    
    namespace OAuthXppConsoleApplication
    {
        class Program
        {
            public static string GetUserSessionOperationPath = ClientConfiguration.Default.UriString + "api/services/EgxCompanyInfoServiceGroup/EgxCompanyInfoService/getHello";
    
            static void Main(string[] args)
            {
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(ClientConfiguration.Default.UriString);
                client.DefaultRequestHeaders.Add(OAuthHelper.OAuthHeader, OAuthHelper.GetAuthenticationHeader());
    
                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "api/services/EgxCompanyInfoServiceGroup/EgxCompanyInfoService/getHello"))
                {
                    request.Content = new StringContent(String.Empty, Encoding.UTF8, "application/json");
                    var httpResponse = client.SendAsync(request).Result;
                    httpResponse.EnsureSuccessStatusCode();
    
                    var result = httpResponse.Content.ReadAsStringAsync().Result;
                    Console.WriteLine(result.ToString());
                }
    
                Console.ReadLine();
            }
        }
    }

    One thing I noticed is that you correctly construct the URL and store in GetUserSessionOperationPath variable (which is a wrong name, by the way), but then you ignore the variable and what you put into HttpRequestMessage is invalid, because it doesn't contain the host name of your AX instance.

  • Suggested answer
    Muhammad Ozair Naeem Profile Picture
    272 on at

    Hi Martin, thanks for replying.
    I forgot to tell about the exceptions.
    Here they are.

    This one is for the Get Method.
    post_5F00_error.JPG

    and this one for post method.
    post_5F00_error.JPG

  • Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    Forget the attempt with GET and collect more information about why the sever returned an error when using POST. First of all, open the exception details, maybe you'll learn a bit more there. Then consult event logs. If there isn't anything useful either, try Failed Request Tracing on IIS. It helped me quite a few times.

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans