C#.NET的WebService返回对象 [英] C#.NET WebService returning object

查看:1061
本文介绍了C#.NET的WebService返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建ASP.NET使用C#的Web服务。我从WebService所以我用以下结构发送各种数据类型。

 公共枚举WS_ServiceResponseResult 
{
成功,
失败,
}
公共类WS_ServiceResponse
{
公共WS_ServiceResponseResult结果{搞定;组; }
公共对象数据{搞定;组; }
}

公共类WS_User
{
众长ID {搞定;组; }
公共字符串名称{;组; }
}



Webservice的抽样方法。



  [的WebMethod(EnableSession =真)] 
公共WS_ServiceResponse登录(用户名字符串,字符串pasword)
{
WS_ServiceResponse OSR =新WS_ServiceResponse() ;
长期用户ID = UserController.checkLogin(用户名,pasword);

如果(用户ID!= 0)
{
osr.result = WS_ServiceResponseResult.Success;
osr.data =新WS_User(){ID =用户ID,NAME =用户名};
}
,否则
{
osr.result = WS_ServiceResponseResult.Failure;
osr.data =无效的用户名/密码!;
}
返回OSR;
}



我使用两种类型的客户端,JavaScript和C#.NET Windows窗体。当我从JavaScript调用我没有得到任何问题,而osr.data充满WS_User。所以我可以很容易地使用osr.data.id。但是,当我从C#.NET中使用我可以成功调用(使用添加Web引用生成的代理),但是当结果到达我得到一个SOAP异常




{服务器无法处理
服务器无法处理
服务器无法处理请求。
---> System.InvalidOperationException:有一个错误生成XML
文档。 ......




我在想什么?我猜对象不明确,造成的问题。什么解决方法?



感谢



Maksud



增加:



如果添加下面的虚拟方法,那么它工作得很好。希望它能帮助,才能得到解决。

  [的WebMethod] 
公共WS_User假()
{
返回新WS_User();
}


解决方案

我有类似的问题返回一个对象(多类可能)
下面是一个示例代码:

  [序列化()] 
[XmlRoot(的ElementName =对象)]
公共密封类XMLObject组成
{

私有对象_Object;

[XmlElement的(类型= ty​​peof运算(App.Projekte.Projekt)的ElementName =Projekt的)]
[XmlElement的(类型= ty​​peof运算(App.Projekte.Task)的ElementName =任务)]
[XmlElement的(类型= ty​​peof运算(App.Projekte.Mitarbeiter)的ElementName =Mitarbeiter)]
公共对象对象
{
的get
{
返回_Object;
}

{
_Object =价值;
}
}
}



我想你应该改变你代码是这样的:

  [XmlRoot(的ElementName =ServiceResponse)] 
公共类WS_ServiceResponse
{
公共WS_ServiceResponseResult结果{搞定;组; }

[XmlElement的(类型= ty​​peof运算(WS_User)的ElementName =WS_User)]
[XmlElement的(类型= ty​​peof运算(字符串),的ElementName =文本)]
酒店的公共对象数据{搞定;组; }
}


I am creating a Web Service using ASP.NET C#. I am sending various data types from the webservice so I use 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; }
}

Webservice Sample 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 client types, javascript and C#.NET Windows Form. When I call from javascript I get no problem and the osr.data is filled with WS_User. So i can use osr.data.id easily. But when I use from C#.NET (proxy is generated using "Add Web Reference") I can successfully call but when the result arrives I get a Soap Exception

{System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ... ...

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

Thanks

Maksud

Addition:

If add the following dummy method, then it works nicely. Hope it helps, to get the solution.

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

解决方案

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; }
}

这篇关于C#.NET的WebService返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆