Error calling web service (asmx)

1

I am creating a class that will be the structure that will always return all services (asynchronous calls)

public class RespuestaServicio
{
public bool IsSuccess { get; set; }
public string Error { get; set; }
public int Codigo { get; set; }
public string Excepcion { get; set; }
public Object Resulto { get; set; }
}

It is assumed that the variable Result must have the information that must be returned by the sevice method (List, string, bool, Object etc)

When I call the service and the result variable is a List for example give this error

  

Error generating the XML document. --- > The type System.Collections.Generic.List 'can not be used in this context.

Try with an array and also, when it is a string or bool it does not give problem, I do not know if this can be done.

How could I solve this? or what alternative could you use to return both the data and the error objection without using variable by reference ?

NOTE : In the client when calling service, it is locked in another method that uses async and await, so it can not use parameters by reference

There is something I do not specify and I think some of those who respond to me were confused.

The Service Response class is a class in webservice, it is where I store the response to send to the client. and when in the answer I assign a list gives error

    
asked by jose antonio arias rodriguez 22.02.2017 в 19:32
source

3 answers

1

You could create a result class, for example:

Public class Resulto{ 
List nombreLista { get; set; } 
string nombreString { get; set; } 
bool nombreBool { get; set; }
Object nombreObject { get; set; }
}

And in your model called ResponseService would be something like this:

public class RespuestaServicio
{
public bool IsSuccess { get; set; }
public string Error { get; set; }
public int Codigo { get; set; }
public string Excepcion { get; set; }
public Resulto resulto { get; set; }
}

And nothing else is calling the return you need, depending on the type of data that is.

Maybe I did not understand you very well and that is not what you are looking for, but I hope I have helped you, regards.

    
answered by 22.02.2017 в 19:56
0

The solution I got was the following:

Change:

public Object Resulto { get; set; }

a

public List<Object> Resulto { get; set; }

Also edit the methods that returned values not list (string, bool etc) to do so and so regardless of the type of data that returns in that variable "Result" does not give me problems to consume it in the client

    
answered by 22.02.2017 в 23:55
-1

You have to control the SOAP and XML Serialization .

Service:

using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Xml.Serialization;

namespace WebApplication1
{
    [Serializable]
    public class Resultado
    {
        [XmlElement(Type = typeof(Mascota), ElementName = "Mascota")]
        [XmlElement(Type = typeof(List<Persona>), ElementName = "Personas")]
        [XmlElement(Type = typeof(string), ElementName = "string")]
        public object Respuesta { get; set; }
    }

    [Serializable]
    public class Persona
    {
        public string Nombre { get; set; }
    }

    [Serializable]
    public class Mascota
    {
        public string Nombre { get; set; }
    }

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WebService1 : WebService
    {
        [WebMethod]
        public Resultado RespuestaCadena()
        {
            return new Resultado { Respuesta = "Hola Mundo" };
        }

        [WebMethod]
        public Resultado RespuestaMascota()
        {
            return new Resultado { Respuesta = new Mascota { Nombre = "Tobi" } };
        }

        [WebMethod]
        public Resultado DinamicoListaPersona()
        {
            return new Resultado { Respuesta = new List<Persona> { new Persona { Nombre = "David" } } };
        }
    }
}

Result:

When getting Resultado as Pet object

<Resultado xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Mascota>
<Nombre>Tobi</Nombre>
</Mascota>
</Resultado>

When getting Resultado as String object

<Resultado xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<string>Hola Mundo</string>
</Resultado>

When getting Resultado as an object a fix of type Person

<Resultado xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Personas>
<Persona>
<Nombre>David</Nombre>
</Persona>
</Personas>
</Resultado>
    
answered by 22.02.2017 в 21:13