An unhandled Exception occured. I cannot understand this message, no detail comes

Asked

Viewed 338 times

2

Personal that message:

An unhandled Exception occured.

It always happens when I try to consume my service REST. I first made an example, copying from Macoratti and it worked. Then I adapted it to my service and it didn’t work, giving this error. I’m reviewing the project of Macoratti and yet, it doesn’t work. Every time you get on that line it doesn’t work: var response = await client.GetStringAsync(url);. The problem is that the error message has nothing else, no Inner Exception, details, nothing, just the message. This is my Dataservice(I kept up to the class name)

public class DataService
    {
        HttpClient client = new HttpClient();

        public async Task<List<LiberacaoDTO>> GetLiberaAsync()
        {
            try
            {
                string url = "http://localhost:9078/api/liberacao";
                var response = await client.GetStringAsync(url);
                var liberacao = JsonConvert.DeserializeObject<List<LiberacaoDTO>>(response);
                return liberacao;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

This is my Mainpage.xaml.Cs

public partial class MainPage : ContentPage
    {
        DataService dataService;
        List<LiberacaoDTO> libera;
        public MainPage()
        {
            InitializeComponent();
            dataService = new DataService();
            AtualizaDados();
        }
        async void AtualizaDados()
        {
            libera = await dataService.GetLiberaAsync();
            listaLibera.ItemsSource = libera.OrderBy(item => item.Cliente).ToList();
        }

        private void listaLibera_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var libera = e.SelectedItem as LiberacaoDTO;

            txtNome.Text = libera.Cliente;
            txtCategoria.Text = libera.Vendedor;
            txtPreco.Text = libera.Juros.ToString();
        }
    }

The Mainpage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Autorizador"
             x:Class="Autorizador.MainPage">

    <StackLayout Orientation="Vertical">
        <StackLayout Padding="5,5,0,0">
            <Label Text="Adicionar um Produto" TextColor="Green" />
        </StackLayout>
        <StackLayout Padding="10,0,10,0">
            <Label x:Name="txtNome" Text="Nome do produto" HorizontalOptions="Start" 
                    VerticalOptions="StartAndExpand" HeightRequest="20" WidthRequest="300" FontSize="Small"/>
            <Label x:Name="txtCategoria" Text="Categoria do Produto" HorizontalOptions="Start" VerticalOptions="StartAndExpand"
                   HeightRequest="20" WidthRequest="300" FontSize="Small"/>
            <Label x:Name="txtPreco" Text="Preço do produto" HorizontalOptions="Start" VerticalOptions="StartAndExpand" 
                    HeightRequest="20" WidthRequest="300" FontSize="Small" />
            <!--<Button HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" HeightRequest="40" Text="Adicionar/Atualizar Produto" 
                    Clicked="btnAdicionar_Clicked" FontSize="Small"/>-->
        </StackLayout>

        <StackLayout Orientation="Vertical" Padding="10,5,10,0">
            <ListView x:Name="listaLibera" ItemSelected="listaLibera_ItemSelected" BackgroundColor="Aqua" SeparatorColor="Blue">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.ContextActions>
                                <MenuItem Clicked="OnAtualizar" CommandParameter="{Binding .}" Text="Atualizar" />
                                <MenuItem Clicked="OnDeletar" CommandParameter="{Binding .}" Text="Deletar" IsDestructive="True" />
                            </ViewCell.ContextActions>
                            <StackLayout Padding="10,10" Orientation="Horizontal">
                                <Label Text="{Binding Cliente}" HorizontalOptions="StartAndExpand"/>
                                <Label Text="{Binding Vendedor}" TextColor="Blue" HorizontalOptions="Center"/>
                                <Label Text="{Binding Juros}" HorizontalOptions="End"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </StackLayout>

</ContentPage>

The App.xaml.Cs

public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            //MainPage = new Autorizador.MainPage();
            MainPage = new NavigationPage(new Autorizador.MainPage());
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

I created a class similar to my service class. I even kept exactly the same name, Liberacaodto. Despite the name DTO, this project is just a name, just to maintain the similarity or equality, as you want.

public class LiberacaoDTO
    {
        public int IdLiberacao { get; set; }
        public byte FlagLiberacao { get; set; }
        [DefaultValue(0)]
        public int IdOrcamento { get; set; }
        [DefaultValue(0)]
        public int IdVendedor { get; set; }
        public string Vendedor { get; set; }
        public int IdFilial { get; set; }
        public string Filial { get; set; }
        public string DataLib { get; set; }
        public int IdCliente { get; set; }
        public string Cliente { get; set; }
        public string TipoVenda { get; set; }
        public string Juros { get; set; }
        public string Desconto { get; set; }
        public string Vencimento { get; set; }
        public string Acrescimo { get; set; }
        public string Entrada { get; set; }
        public decimal CustoDiario { get; set; }
        public string Mensagem { get; set; }
    }

I do so: I open two instances of VS2017, one for the service and one for the App. So, the service is running, that would not be the problem.

EDIT1 Screenshot of the error message inserir a descrição da imagem aqui

And this is the message caught in the catch: An error occurred while sending the request

  • Is this exception falling within the catch? Normally, unspecified exceptions are exceptions to the visual studio tool itself. Example: I have refactored an implementation so heavy, that if requested several times break the stack of execution of visual studio.

  • @Gabrielcoletta, no man, I didn’t put it on a block try..catch. It would be ideal to treat Exception, right? But, what kind of Exception to treat? I don’t know what to treat. If you put a generic Exception, it will fall into the same message.

  • 1

    Guy your code is breaking inside a Try.. catch, didn’t you notice? If it’s on a Try.. catch, and was not caught, this is an error of the internal tool of visual studio. Note that your application is even paused because of Exception. It’s gonna be hard to fix that mistake, it could be a lot of things.

  • @Gabrielcoletta, I know, the way it is gives dick and does not enter, but if I give throw new exception... and put a break in the catch, I can get the Exception that is: An error occurred while sending the request

1 answer

2

  • The service works. By Postman(json) I can and by the browser(xml) I have data in them. I don’t know if that’s what you’re suggesting. It just doesn’t work inside the App. But remember, by copying the Macoratti example, it works. If I keep the same example and change only the Model and of course, and point to my service, there is the error. So I think it’s the service, I don’t know, and I don’t have Macoratti’s service to compare it to mine. But my job is no problem, either with Postman or with Chrome and IE.

  • 1

    It’s working now. I’ve seen some things I want to share. I removed all packages from Xamarin and reinstalled. I saw that my internet was oscillating too much and as I was with my 4g off, this also generated the problem. This type of exception, is a generic exception of Xamarin, which is triggered to any problems that occur, from what I understand. It doesn’t have a detail of what’s going on, it just says that there’s a problem somewhere and it should be dealt with. Events in the shaman and not implemented in the Behind, lack of connection, anything triggers this exception.

Browser other questions tagged

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