将日期作为对象属性传递给Web服务

时间:2012-12-19 19:42:56

标签: .net web-services c#-4.0 wsdl

我是网络服务的新手,我试图找到解决方案,但没有找到任何解决方案。

我正在尝试将对象作为参数发送到Web服务。

    [WebMethod]
    public bool CreatePatientService(Patient p)
    {
        AdminService AsRef = new AdminService();
        return AsRef.CreatePatient(p);
    }

我的病人课程如下:

[Serializable]
public class Patient
{

    public string NIC { get; set; }
    public string FullName { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public string Title { get; set; }
    public string Gender { get; set; }
    public string CivilStatus { get; set; }
    public DateTime DOB { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
}

然后我使用SOAPUi来调用Web服务。

我将请求调用如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:CreatePatientService>
         <!--Optional:-->
         <tem:p>
            <!--Optional:-->
            <tem:NIC>15487236</tem:NIC>
            <!--Optional:-->
            <tem:FullName>awdss</tem:FullName>
            <!--Optional:-->
            <tem:FirstName>qewretr</tem:FirstName>
            <!--Optional:-->
            <tem:Surname>qscv</tem:Surname>
            <!--Optional:-->
            <tem:Title>Mr</tem:Title>
            <!--Optional:-->
            <tem:Gender>M</tem:Gender>
            <tem:CivilStatus>S</tem:CivilStatus>
            <tem:DOB>01/02/2002</tem:DOB>
            <!--Optional:-->
            <tem:Address1>nikhno</tem:Address1>
            <!--Optional:-->
            <tem:Address2>asdf</tem:Address2>
            <!--Optional:-->
            <tem:Address3>125</tem:Address3>
         </tem:p>
      </tem:CreatePatientService>
   </soapenv:Body>
</soapenv:Envelope>

然后它给出以下错误。我怎么解决呢?

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Client</faultcode>
         <faultstring>System.Web.Services.Protocols.SoapException: Server was unable to read request. ---> System.InvalidOperationException: There is an error in XML document (19, 49). ---> System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&amp; number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseUInt32(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.UInt16.Parse(String s, NumberStyles style, NumberFormatInfo info)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read2_TblPatient(Boolean isNullable, Boolean checkType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read7_CreatePatientService()
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer8.Deserialize(XmlSerializationReader reader)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</faultstring>
         <detail/>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

如果我删除了DOB(日期),那么它的工作正常。

我参考以下帖子。但是没有工作。

Proper DateTime Format for a Web Service

请帮助..谢谢...

1 个答案:

答案 0 :(得分:4)

DateTime需要有时间值和日期,所以你错过了小时,分钟,秒等事实就是打破它。

yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz is the expected format.

e.g. 2002-02-01T00:00:00.0

XML deserialize DateTime Format