0

I have built a Web API using MVC and it works as expected.

I am now trying to query the API from a console application and I am hitting an issue. I understand why I am getting the issue but I dont understand how to fix it.

My Code from the console application:

static HttpClient client = new HttpClient();
        static  void Main(string[] args)
        {

            RunAsync().GetAwaiter().GetResult();

        }

        static async Task RunAsync()
        {

            client.BaseAddress = new Uri(URL);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            List<TagDetail> tagDetail = new List<TagDetail>();

            tagDetail = await GetTagDetailAsync("api/tagdetail/?tagname=myTag&startdate=010120190000&enddate=020120190000");
            Console.WriteLine(tagDetail.value);
        }

            static async Task<TagDetail> GetTagDetailAsync(string path)
        {
            List<TagDetail> tagdetail = new List<TagDetail>();
            HttpResponseMessage response = await client.GetAsync(path);
            var test = response.StatusCode;
            var test2 = response.Headers;
            if (response.IsSuccessStatusCode)
            {
                tagdetail = await response.Content.ReadAsAsync<List<TagDetail>>(
            new List<MediaTypeFormatter>
            {
                new XmlMediaTypeFormatter(),
                new JsonMediaTypeFormatter()
            }); 
            }
            return tagdetail;
        }

The error I am getting is on the lines:

tagDetail = await GetTagDetailAsync("api/tagdetail/?tagname=99TOTMW&startdate=010120190000&enddate=020120190000");

And

return tagdetail;

The Web API returns the data in JSON format which looks like:

{  
   "tagname":"myTag",
   "value":"99.99",
   "description":"myDescription",
   "units":"£",
   "quality":"Good",
   "timestamp":"2019-08-01T17:32:30"
},
{  
   "tagname":"myTag",
   "value":"22.22",
   "description":"myDescription",
   "units":"£",
   "quality":"Good",
   "timestamp":"2019-08-01T17:33:30"
}

The TagDetail class is just declaration of each of the fields you see above.

The webapi provide the means of selecting a date range so I would get numerous TagDetails back as a List but it can also return just one (I can get this working by changing my code a bit). I need it to work for either one result or multiple.

1
  • 1
    could you change your code to this and see if your error goes away, static async Task<IEnumerable<TagDetail>> GetTagDetailAsync(string path). Your web API serves a single TagDetail object but you are passing back a List<TagDetail>. Handle the number of records returned on the client side. Also, what do you expect tagDetail.value to be? Commented Aug 5, 2019 at 17:09

1 Answer 1

1

As the comment has explained that you need to return List<TagDetail> for your GetTagDetailAsync.Then you could use foreach to loop the result.This will work for one or multiple TagDetail

static async Task RunAsync()
    {

        //other logic
        List<TagDetail> tagDetail = new List<TagDetail>();

        tagDetail = await GetTagDetailAsync("api/tagdetail/?tagname=myTag&startdate=010120190000&enddate=020120190000");

        foreach(var item in tagDetail)
        {
            Console.WriteLine(item.value);
        }
    }

        static async Task<List<TagDetail>> GetTagDetailAsync(string path)
    {
        List<TagDetail> tagdetail = new List<TagDetail>();
        HttpResponseMessage response = await client.GetAsync(path);
        var test = response.StatusCode;
        var test2 = response.Headers;
        if (response.IsSuccessStatusCode)
        {
            tagdetail = await response.Content.ReadAsAsync<List<TagDetail>>(
        new List<MediaTypeFormatter>
        {
            new XmlMediaTypeFormatter(),
            new JsonMediaTypeFormatter()
        }); 
        }
        return tagdetail;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.