如何从ASMX Webservice返回用户定义的对象

时间:2013-06-27 10:58:17

标签: c# web-services object asmx

    [WebMethod]
    public engineer getItem()
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        List<string> list = new List<string>();
        string tcNo="13609224456";
        var bilgiler = db.bilgilers.First(c => c.tc_kimlik_no ==tcNo);

        string adi = bilgiler.adi;
        string soyadi = bilgiler.soyadi;
        string universite = bilgiler.universite;
        engineer e = new engineer(tcNo, adi, soyadi, universite);

        return e;
    }

我有多个方法,我可以毫无错误地运行webservice但是当我调用getItem()方法时,我得到以下异常。我该如何解决这个问题?

[InvalidOperationException: Parametresiz oluşturucusu olmadığından MUHENDIS.Service1.engineer seri hale getirilemez.]
   System.Xml.Serialization.TypeDesc.CheckSupported() +3938411
   System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) +170
   System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +65
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +236

[InvalidOperationException: 'getItemResult' yansıtmada bir hata oluştu.]
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +843
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(String elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, XmlMappingAccess access) +130
   System.Web.Services.Protocols.SoapReflector.ImportMembersMapping(XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, Boolean serviceDefaultIsEncoded, Boolean rpc, SoapBindingUse use, SoapParameterStyle paramStyle, String elementName, String elementNamespace, Boolean nsIsDefault, XmlReflectionMember[] members, Boolean validate, Boolean openModel, String key, Boolean writeAccess) +223
   System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo methodInfo, Boolean client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, String defaultNs) +2989

编辑: Google会将有关例外的消息转换为以下内容:

  1. MUHENDIS.Service1.engineer不是无参数构造函数。
  2. 'getItemResult'以反映发生的错误。

2 个答案:

答案 0 :(得分:3)

您需要确保您的工程师类被标记为可序列化。

[Serializable]
public class engineer{ ....

答案 1 :(得分:1)

我认为,您需要像这样定义getItem方法。

[WebMethod]
[XmlInclude(typeof(engineer))]
public engineer getItem(){
//codes here
}
//engineer class here

没有XmlInclude属性,XmlSerializer将无法序列化工程师