An unhandled exception occured. I can not understand this message, no detail comes

2

Personal this message:

  

An unhandled exception occured.

It always happens when I try to consume my service REST . I first made an example, copying it from Macoratti and it worked. So I adapted to my service and it did not work, giving that mistake. I'm reviewing the Macoratti project and it still does not work. Whenever it arrives on that line it does not 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 (Keep up 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 DTO name, in this project it's just a very name, just to maintain similarity or equality, as you please.

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 this: 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 error message

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

    
asked by anonymous 03.09.2017 / 15:25

1 answer

2

It tries to send a request to the url ( link ) using some http client (for example: link ). It can give you a clue as to what problem you are having in the answer.

    
04.09.2017 / 16:11