当前位置: 首页 > 编程语言 > C#

C#.NETWebService返回对象共享

时间:2023-04-10 18:27:18 C#

C#.NETWebService返回对象我正在使用ASP.NETC#创建一个Web服务。我从Web服务发送各种数据类型,所以我使用以下结构。publicenumWS_ServiceResponseResult{Success,Failure,}publicclassWS_ServiceResponse{publicWS_ServiceResponseResultresult{get;放;}公共对象数据{得到;放;}}publicclassWS_User{publiclongid{get;放;}公共字符串名称{get;放;}}Web服务示例方法[WebMethod(EnableSession=true)]publicWS_ServiceResponseLogIn(stringusername,stringpasword){WS_ServiceResponseosr=newWS_ServiceResponse();longuserID=UserController.checkLogin(用户名,密码);if(userID!=0){osr.result=WS_ServiceResponseResult.Success;osr.data=newWS_User(){id=userID,name=username};}else{osr.result=WS_ServiceResponseResult.Failure;osr.data="无效的用户名/密码!";}返回操作系统;我使用两种客户端类型,javascript和C#.NETWindowsForm。当我从javascript调用时,我没有问题,osr.data充满了WS_User。所以我可以很容易地使用osr.data.id。但是,当我使用C#.NET(使用“添加Web引用”生成代理)时,我可以成功调用,但是当结果到达时,我得到一个Soapexception{System.Web.Services.Protocols.SoapException:System.Web.Services.Protocols.SoapException:服务器无法处理请求。—>System.InvalidOperationException:生成XML文档时出错。……我错过了什么?我想这个对象没有明确定义并导致问题。有哪些解决方案?感谢Maksud添加:如果我添加以下虚拟方法,那么它工作正常。希望它有助于获得解决方案。[WebMethod]publicWS_UserDummy(){returnnewWS_User();我在返回“对象”时遇到了类似的问题(可能有多个类)这是一个示例代码:[Serializable()][XmlRoot(ElementName="Object")]publicsealedclassXMLObject{privateObject_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")]publicObjectObject{get{return_Object;}设置{_Object=值;我认为你应该改变你的代码:[XmlRoot(ElementName="ServiceResponse")]publicclassWS_ServiceResponse{publicWS_ServiceResponseResultresult{get;放;}[XmlElement(Type=typeof(WS_User),ElementName="WS_User")][XmlElement(Type=typeof(string),ElementName="Text")]公共对象数据{get;放;我如何确定两个对象的类型是否兼容?我如何翻译newButton.Click+=delegate{window.IsOpen=false;};在vb.net中,以上是C#学习教程:C#.NETWebService返回对象共享的所有内容,如果对大家有用还需要了解了解更多C#希望大家多多关注教程——本文整理自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: