My service does not work to make update in the database

pnet1pnet1 USMember ✭✭✭✭
edited September 2017 in Xamarin.Forms

I made a service and when i call by url i have a page not found(404). My call

My service

public class AtualizaController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        PedidoLiberacao liberacao = new PedidoLiberacao();

        [AcceptVerbs("Put")]
        public void putItensLiberacao(int id, [FromBody]string value)
        {
            liberacao.AtualizaLiberacao(id, value);
        }
    }

My method called by service

[Route("atualiza/{id}/{value}")]
        public void AtualizaLiberacao(int id, string value)
        {
            var lista = contexto.Liberacoes
                        .Where(l => l.IdOrcamento == id).ToList();

            lista.ForEach(f =>
            {
                f.FlagLiberacao = 0;
                f.AutorizouReceberAtrazado = value;
            });

            contexto.SaveChanges();

        }

Answers

  • ChaseFlorellChaseFlorell CAInsider, University mod

    Lot's of issues with this

    1. your device when looking up "localhost" will be looking at its internal dns
    2. your device will need to be hosting your API internally on port 9078

    Basically you need to host your API on a machine that can expose the endpoint to your network. At minimum try to point to an ip address 192.168.0.xx and use the standard port 80. Otherwise your device will have no way of finding your API.

    Lastly, if you're testing, I suggest writing a Mock around your API call so that you can fake the data and not actually have to make the call to your API.

  • pnet1pnet1 USMember ✭✭✭✭
    edited September 2017

    Hi @ChaseFlorell, first i'm testing without App, only my service directly in the URL. I saw some errors in my code, like:
    Missing RoutePrefix in my service and Route above my call. I changed and now i have this:
    Service

    [RoutePrefix("api/Atualiza")]
        public class AtualizaController : ApiController
        {
            AutorizadorContext contexto = new AutorizadorContext();
            PedidoLiberacao liberacao = new PedidoLiberacao();
    
            [Route("{id}/{value}")]
            [AcceptVerbs("Put")]
            public void putItensLiberacao(int id, [FromBody]string value)
            {
                liberacao.AtualizaLiberacao(id, value);
            }
        }
    

    And the method called by service

    [Route("atualiza/{id}/{value}")]
            public void AtualizaLiberacao(int id, string value)
            {
                var lista = contexto.Liberacoes
                            .Where(l => l.IdOrcamento == id).ToList();
    
                lista.ForEach(f =>
                {
                    f.FlagLiberacao = 0;
                    f.AutorizouReceberAtrazado = value;
                });
    
                contexto.SaveChanges();
    
            }
    

    When I run my service, now i have other message. Original message(Portuguese)

    {
        "Message": "O recurso solicitado não suporta o método http 'GET'."
    }
    

    translate:

    the requested resource does not support http method: 'Get'

  • RavinderJangraRavinderJangra USMember ✭✭✭

    yes this is a common problem when calling the api in emulator/device, not in the same network environment and shows not found

  • pnet1pnet1 USMember ✭✭✭✭
    edited September 2017

    When I run postman, work. But when runs the lambda, i have this error:

    The 'DataLib' property on 'Liberacao' could not be set to a 'System.Double' value. You must set this property to a non-null value of type 'System.Single'.

    Problem is here:

    var lista = contexto.Liberacoes
                            .Where(l => l.IdOrcamento == id).ToList();
    

    I tried change data type to: float, Single, double and not work.

  • RavinderJangraRavinderJangra USMember ✭✭✭

    @pnet1 There is some mismatch of the variables in your object properties just try to make them similar and the problem will be solved.

  • pnet1pnet1 USMember ✭✭✭✭
    edited September 2017

    @RavinderJangra, i tried and nothing. Before, i had same issue with get method, and i create a DTO, transform all types in the DTO to string. So i got it run my service.

  • pnet1pnet1 USMember ✭✭✭✭
    edited September 2017

    I changed my lambda expression by this

    var lista = contexto.Liberacoes
                            .Where(l => l.IdOrcamento == id)
                            //.ToList()
                            .Select(s => new Liberacao
                            {
                                FlagLiberacao = s.FlagLiberacao,
                                AutorizouReceberAtrazado = s.AutorizouReceberAtrazado
                            }).ToList();
    

    and i have this error message

    The entity or complex type 'Inet.AutorizadorService.Infra.Data.Context.Liberacao' cannot be constructed in a LINQ to Entities query.

    I started the constructor of my entity model class with 0 value and the error persists. I put default value by attribute and samething.

  • pnet1pnet1 USMember ✭✭✭✭
    edited September 2017

    @ChaseFlorell and @RavinderJangra, i remember that when i made a get service, i passed by the same issue and the solution went, in my DTO class i change to string all types and when my App consume my service, then i make cast to true value. Well, i tried the samething. The service no raise error, but doesn't update into database. In my code lista is Liberacao and not LibaracaoDTO. This i made only for work the service, a cheating for the service. This is current code

     [Route("{id}/{value}")]
            public void AtualizaLiberacao(int id, string value)
            {
                var lista = contexto.Liberacoes
                            .Where(l => l.IdOrcamento == id)
                            .Select(libera => new LiberacaoDTO
                            {
                                TipoVenda = libera.TipoVenda,
                                IdOrcamento = libera.IdOrcamento,
                                Juros = libera.Juros.ToString(),
                                Entrada = libera.Entrada.ToString(),
                                Acrescimo = libera.Acrescimo.ToString(),
                                Desconto = libera.Desconto.ToString(),
                                Mensagem = libera.Mensagem,
                                DataLib = libera.DataLib.ToString(),
                                Vencimento = libera.Vencimento.ToString(),
                                Vendedor = libera.Vendedor,
                                Cliente = libera.Cliente,
                                Filial = libera.Filial
                            })
                            .ToList();
    
                lista.ForEach(f =>
                {
                    f.FlagLiberacao = 0;
                    f.AutorizouReceberAtrazado = "Testando";
                });
    
                contexto.SaveChanges();
    
            }
    

    Anything else, the parameter value is null. I made a value literal, for test only and nothing.

    Missed service code

    [RoutePrefix("api/Atualiza")]
        public class AtualizaController : ApiController
        {
            AutorizadorContext contexto = new AutorizadorContext();
            PedidoLiberacao liberacao = new PedidoLiberacao();
    
            [Route("{id}/{value}")]
            [AcceptVerbs("Put")]
            public void putItensLiberacao(int id, [FromBody]string value)
            {
                liberacao.AtualizaLiberacao(id, value);
            }
        }
    

    When I removed the [FromBody], the string parameter value is not a null. Now it receives value from URL

  • pnet1pnet1 USMember ✭✭✭✭
    edited September 2017

    This issue because i made a new DTO and my DbSet is Liberacao and not LiberacaoDTO. Say me to make a EntiyState.Modified and mapping from DTO to Liberacao, but i ask: How can I do?

Sign In or Register to comment.