Interface and heritage in Xamarin - System.Invalidcastexception: Specified cast is not Valid

Asked

Viewed 141 times

1

Greetings!

I’m developing a Cross Platform project in Xamarin. This project consists of consuming a web-service method that will return the script code and an address array that corresponds to the script.

But the return of the current Router method gives me the following exception: System.InvalidCastException: Specified cast is not valid.

This error happened to occur only after it includes all the necessary structure for the route interface operation.

Without the route interface the problem does not occur. I cannot understand why the conversion error is occurring. Where am I missing?! Every definition of code structure will be described below.

Inside my Portable I have the interfaces:

    public interface IRoteiro<TRota> where TRota : IRota
    {

        int Codigo { get; set; }

        TRota[] Rotas { get; set; }

    }

    public interface IRota
    {

        string Endereco { get; set; }

        string URL { get; set; }

    }

    public interface ITransportadorSoapService
    {
        Task<IRoteiro<IRota>> RoteiroAtual(int codigoTransportador);
    }

Already inside the platform I have the classes:

    public partial class Roteiro : IRoteiro<Rota>
    {
     
    }

    public partial class Rota : IRota
    {

    }

    public sealed class TransportadorSoapService : ITransportadorSoapService
    {

        public TransportadorSoapService()
        {

        }

        public async Task<IRoteiro<IRota>> RoteiroAtual(int codigoTransportador)
        {

            //Webservice referenciado na plataforma
            WSRoteiro.RT ws = new WSRoteiro.RT
            {
                Timeout = 60000
            };

            //Este return dispara a exceção.
            return (IRoteiro<IRota>)await Task.Run(() =>
            {
                return ws.RoteiroTransportador(codigoTransportador);
            });

        }

    }

Inside the Portable I have a page with a method that consumes the current Router method:

        private async void Inicializar()
        {

            try
            {
                var rotaAtual = await App.TransportadorSoapService.RoteiroAtual(transportadorLogado.Codigo);
            }
            catch (Exception ex)
            {
                //Exceção disparada pelo return do método RoteiroAtual vem para este catch.
            }

        }

Below is the format of the web-service consumed:

inserir a descrição da imagem aqui

  • Where exactly does the error occur? It seems to be a problem where you think you are assigning a type A to an instance A, but you are actually trying to assign B to an instance A.

  • Leandro. I think it’s something in this sense. But I’m not able to identify it in the code above. The location that triggers the error is the Return of the Current Route method that has a comment above its execution line.

  • You can debug the code?

  • Yeah. I can debug the code. This is the code snippet that triggers the exception: Return (Iroteiro<Irota>)await Task.Run(() => { Return Ws.Router(codeTransporter); }); The problem is that I need to return a Iroteiro<Irota> and that is what generates the exception. But if I store Ws return.Router(codeTransporter) the way below without trying the conversion I get the return information: var rt = Ws.Router(codeTransporter); Only trying the conversion in Return do I have the exception.

No answers

Browser other questions tagged

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