Service stopped working when generating the chart

Asked

Viewed 56 times

0

I was having trouble generating a graph, which is solved. The code below was ok and suddenly stopped, IE, the service is not running on account do not know what. At the time I call the Getservice nothing comes, the var itensgrid remains void.

public class DataModelGrid
    {
        DataService dataService = new DataService();
        public List<LiberacaoItensGrid> itensGrid = new List<LiberacaoItensGrid>();
        public List<GeraGrafico> GeraChart { get; set; }
        public double IdOrcamento { get; set; }
        public double TotalVenda { get; set; }
        public double TotalLucro { get; set; }
        public DataModelGrid(double id)
        {
            GetService(id);
            GeraChart = new List<GeraGrafico>();
            GeraChart.Add(new GeraGrafico() { Assunto = "Vendas", Total = TotalVenda });
            GeraChart.Add(new GeraGrafico() { Assunto = "Lucro", Total = TotalLucro });
        }        
        public async void GetService(double id)
        {
            itensGrid = await dataService.GetDataGrid(id);
            foreach(var item in itensGrid)
            {
                this.IdOrcamento = item.IdOrcamento;
                this.TotalVenda = item.TotalVenda;
                this.TotalLucro = item.TotalLucro;
            }
        }
        public class GeraGrafico
        {
            public string Assunto { get; set; }
            public double Total { get; set; }
        }
    }

That’s the code I use to get my service

public async Task<List<LiberacaoItensGrid>> GetDataGrid(double id)
        {
            try
            {
                string url = $"http://www.inetglobal.com.br/autorizador/api/getliberaitens/{id}";
                var response = await client.GetStringAsync(url);
                var itenslib = JsonConvert.DeserializeObject<List<LiberacaoItensGrid>>(response);
                return itenslib.ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

The problem that was working and I don’t know what I did. I already gave Ctrl+Z until I came back to nothing, I was doing Ctrl+Shift+Z and testing every step passed and I did not succeed.

Why am I no longer getting information that comes from my service with these codes? What’s wrong? When I hit one thing, I miss another. One in the harpsichord and one in the horseshoe.

EDIT1

You’re making this mistake

Newtonsoft.Json.Jsonreaderexception: Unexpected Character encountered while Parsing value: S. Path '', line 0, position 0. at Newtonsoft.Json.Jsontextreader.Parsevalue () [0x002b3] in :0 at Newtonsoft.Json.Jsontextreader.Read () [0x0004c] in :0 at Newtonsoft.Json.Jsonreader.Readandmovetocontent () [0x00000] in :0 at Newtonsoft.Json.JsonReader.ReadForType (Newtonsoft.Json.Serialization.Jsoncontract Contract, System.Boolean hasConverter) [0x00043] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.Jsonreader Reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in :0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.Jsonreader Reader, System.Type objectType) [0x00053] in :0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader Reader, System.Type objectType) [0x00000] in :0 at Newtonsoft.Json.Jsonconvert.Deserializeobject (System.String value, System.Type type type, Newtonsoft.Json.Jsonserializersettings Settings) [0x0002d] in :0 at Newtonsoft.Json.Jsonconvert.Deserializeobject[T] (System.String value, Newtonsoft.Json.Jsonserializersettings Settings) [0x00000] in :0 at Newtonsoft.Json.Jsonconvert.Deserializeobject[T] (System.String value) [0x00000] in :0 at Authorizer.Service.Dataservice+d__4.Movenext () [0x0005b] in C: Labs Authorizing Authorizing Authorizing Service Dataservice.Cs:60 }

For that code

public async Task<List<LiberacaoItensGrid>> GetDataGrid(double id)
        {
            try
            {
                //string url = $"http://www.inetglobal.com.br/autorizador/api/getliberaitens/{id}";
                //var response = await client.GetStringAsync(url);
                //var itenslib = JsonConvert.DeserializeObject<List<LiberacaoItensGrid>>(response);
                var client = new HttpClient();
                string url = $"http://www.inetglobal.com.br/autorizador/api/getliberaitens/{id}";
                var response = client.GetStringAsync(url);

                response.Wait(); // use assim ou com o while ....
                var itenslib = JsonConvert.DeserializeObject<List<LiberacaoItensGrid>>(response.ToString());

                while (response.Status != System.Threading.Tasks.TaskStatus.RanToCompletion)
                {

                }

                return itenslib.ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
  • If you call the api url directly with your ID the result appears?

  • @Leandroangelo, then, I put a break in the service call and it’s like this. He mounts the URL correctly, passing the correct parameter. But when you get here var response = await client.GetStringAsync(url); then it comes out of the method. I just want to understand why if it already worked and now it doesn’t. It doesn’t get in the catch, it just comes out of the method.

  • I believe this is giving "dick": await client.GetStringAsync(url); I tried to call directly and keeps leaving the method.

  • try using client.Getstringasync(url). Configureawait(false);

  • @Marconciliosouza, I had already done this and still still has problems. I do not understand, came to work, for three times I entered the break and now nothing.

  • Your API is working ?

  • Yes, I press the Postman and the result comes. It’s OK.

Show 2 more comments

1 answer

2


amend your Wait as follows.

var client = new HttpClient();
string url = $"http://www.inetglobal.com.br/autorizador/api/getliberaitens/{id}";
var response = client.GetStringAsync(url);

response.Wait(); // use assim ou com o while ....

while(response.Status != System.Threading.Tasks.TaskStatus.RanToCompletion)
{

}

inserir a descrição da imagem aqui

Solution in Git

  • Marconcilio, you marked the method as void. How I would load my model from it?

  • I just marked it as void because I don’t have your template you have to simply use jsonconvert for your class.. how was making var itenslib = Jsonconvert.Deserializeobject<List<Liberacaoitensgrid>>(Answer);

  • Or that’s not working either ?

  • It is that I took to the letter your example, but I will change to return a Task<T> and see what happens. Only one doubt: inside your while there is nothing, I must keep so?

  • Wait(); but if you want to do something else while waiting for the return you can use while. is at your discretion.

  • I made an edit to show the error

  • 1

    I think in my previous code, that was missing: var client = new HttpClient();. That way it worked with my original code.

  • You gave me the problem again, it stopped working. I don’t feel safe taking this to my client tomorrow. He returned to the same problem he was giving, that is, in Sponse he aborts. Can this have to do with debug mode? Will it be?

  • I don’t know what it can be.

  • Marconcilio, your code gives an error. On this line var itenslib = JsonConvert.DeserializeObject<List<LiberacaoItensGrid>>(response); it asks to re-sponse to be converted to a string and when I do so, gives unrecognized character error

  • var itenslib = Jsonconvert.Deserializeobject<List<Liberacaoitensgrid>>(Response.Result.Tostring());

  • The error is: Cannot convert System.Threading.Tasks.Task<string> para string. How to solve this mistake?

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.