How to generate xs:Date in WCF OperationContract parameter

前端 未结 5 1700
感动是毒
感动是毒 2020-12-15 07:21

For parameters to an OperationContract that represent a date only (no time component or timezone designator), it is desirable to use xs:Date, to avoid any ambiguity or probl

相关标签:
5条回答
  • 2020-12-15 07:32

    You specified a requirement to have xs:Date as a parameter in the operation. The thing is, there's a formal way to specify such requirements: WSDL. If I were doing this I would use a WSDL First approach. Start with a WSDL that defines the contract you want, including an xs:date in the interface, wherever it is you want it. Then generate the service stub using svcutil.exe. Remember to pass in /serializer:xmlSerializer .

    0 讨论(0)
  • 2020-12-15 07:32

    WCF's defaut serializer (DataContractSerializer) does not support it. But XmlSerializer supports it.

    1 - Add the [XmlSerializerFormat] attribute to your contract...

    [XmlSerializerFormat]
    [ServiceContract]
    public interface IMyContract
    {
       MyType GetData();
    }
    

    2 - In the DataContract type, add the [XmlElement(DataType = "date")] to the member.

    public class MyType
    {
         [XmlElement(DataType = "date")]
         public DateTime BirthDate {get; set;}
    }
    

    Hope this helps

    0 讨论(0)
  • 2020-12-15 07:39

    I've also got described issue. I've developed my own solution - WcfDate custom type. It is published here: WCF Support for xs:date

    0 讨论(0)
  • 2020-12-15 07:47

    Now that this has come to my attention, I have created a new Suggestion in Connect, at Please Fully Support xs:Date for Date-Only Parameters and DataMembers. I rated this with four stars (important).

    If anyone reading this feels this is important (or else disagrees), then please use Connect to vote on it or comment on it.

    0 讨论(0)
  • 2020-12-15 07:50

    Unfortunately WCF doesn't support the xs:Date type. You'd have to create your own "DateOnly" struct, like:

    <DataContract()> _
    public struct DateOnly
       <DataMember()> public Month as Integer
       <DataMember()> public Day as Integer
       <DataMember()> public Year as Integer
    end struct
    
    0 讨论(0)
提交回复
热议问题