C # .NET WebService object returned

I am creating a web service using ASP.NET C #. I am posting different types of data from webservice, so I am using the following structure.

public enum WS_ServiceResponseResult
{
    Success,
    Failure,
}
public class WS_ServiceResponse
{
    public WS_ServiceResponseResult result { get; set; }
    public object data { get; set; }
}

public class WS_User
{
    public long id{ get; set; }
    public string name{ get; set; }
}

      

Example Webservice Method

    [WebMethod(EnableSession = true)]
    public WS_ServiceResponse LogIn(string username, string pasword)
    {
        WS_ServiceResponse osr = new WS_ServiceResponse();
        long userID = UserController.checkLogin(username, pasword);

        if (userID != 0)
        {
            osr.result = WS_ServiceResponseResult.Success;
            osr.data = new WS_User() { id = userID, name = username };
        }
        else
        {
            osr.result = WS_ServiceResponseResult.Failure;
            osr.data = "Invalid username/password!";
        }
        return osr;
    }

      

I am using two types of clients, javascript and C # .NET Windows Form. When I call from javascript I get no problem and osr.data is filled with WS_User. So I can easily use osr.data.id. But when I use C # .NET (the proxy is generated with "Add Web Link") I can successfully call, but when the result is reached I get a Soap exception

{System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: The server was unable to process the request. ---> System.InvalidOperationException: An error occurred while generating XML document .......

What am I missing? The Guess object is not well defined and causes problems. What are the workarounds?

thank

Maqsud

Addition:

If you add the following dummy method, it works well. Hope it helps to get a solution.

    [WebMethod]
    public WS_User Dummy()
    {
        return new WS_User();
    }

      

+2


source to share


1 answer


I had a similar problem returning an "object" (multiple classes possible) Here is a sample code:

[Serializable()]
[XmlRoot(ElementName="Object")]
public sealed class XMLObject
{

    private Object _Object;

    [XmlElement(Type=typeof(App.Projekte.Projekt), ElementName="Projekt")]
    [XmlElement(Type=typeof(App.Projekte.Task), ElementName="Task")]
    [XmlElement(Type=typeof(App.Projekte.Mitarbeiter), ElementName="Mitarbeiter")]
    public Object Object
    {
        get
        {
            return _Object;
        }
        set
        {
            _Object = value;
        }
    }
}

      



I think you should change your code this way:

[XmlRoot(ElementName="ServiceResponse")]
public class WS_ServiceResponse
{
    public WS_ServiceResponseResult result { get; set; }

    [XmlElement(Type=typeof(WS_User), ElementName="WS_User")]
    [XmlElement(Type=typeof(string), ElementName="Text")]
    public object data { get; set; }
}

      

+4


source







All Articles