这些问题中有一些很好的信息:How to generate xs:Date in WCF OperationContract parameter和Best practice for DateTime serialization in .NET 3.5。
正如亚历克斯在对问题的评论中所说,WCF 不支持xs:date
类型。不过,也许更准确的说法是默认DataContractSerializer
不支持该类型,而上面的问题表明XmlSerializer
可以处理。
请参阅此链接进行DataContractSerializer
对照XmlSerializer
比较。
如果我运行:
svcutil http://my_web_site?wsdl /ser:XmlSerializer /d:C:\temp
然后是这样的 WSDL 片段:
<s:complexType name="Contact">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="BirthDate" type="s:date" />
</s:sequence>
</s:complexType>
是否生成了此类:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public partial class Contact
{
private System.DateTime birthDateField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="date", Order=0)]
public System.DateTime BirthDate
{
get
{
return this.birthDateField;
}
set
{
this.birthDateField = value;
}
}
}
该svcutil
调用产生两个文件:Service1.cs
和output.config
. 如果我在项目中包含代码文件并将这些system.serviceModel
位添加到配置文件(即 web.config 或 app.config)中,我就可以正常调用该服务。例如:
Service1SoapClient client = new Service1SoapClient("Service1Soap");
var contact = client.GetContact();
这种方法并非没有缺点。如果在没有参数的情况下生成该文件,则该Service1.cs
文件明显不同/ser:XmlSerializer
,您将在其中获得其他类,例如WebMethodNameRequest
、WebMethodNameRequestBody
、WebMethodNameReponse
等WebMethodNameReponseBody
。如果这些类在您与服务的交互中很重要,那么我的方法可能不适合您。
编辑:
在可空属性方面,这个问题中有一些很好的信息:svcutil.exe - Proxy generated not allowed for nullable fields
要在生成的代理类中获取可为空的属性,nillable
需要在 WSDL 中设置该字段。所以是这样的:
<s:element minOccurs="0" maxOccurs="1" name="SomeProperty" type="s:date" nillable="true" />
将生成public System.Nullable<System.DateTime> SomeProperty
在代理类中调用的属性。
但是,在您的情况下,您可以使用该SomePropertySpecified
属性来指示该属性的存在或不存在。这些类型的属性是在您拥有时生成的minOccurs="0"
。
在日期格式方面我不确定。xs:date
值应为带有可选时区信息 ( w3.org ) 的 yyyy-mm-dd。如果 Oracle 期望日期采用不同的格式,那么我想知道它们是如何成为xs:date
值的。
您是否可以提供有关您尝试使用的服务的任何文档或其他信息?
编辑2:
我不太清楚“日期必须是数据库格式”的确切含义。在 Oracle 文档中表示。如果类型是 anxs:date
然后将它们序列化为数据库格式肯定意味着它不再是xs:date
?
尽管如此,在这方面您还是会尝试一些事情:
您可能需要简单地尝试向 Web 服务发送一些查询,以了解此日期业务如何影响事物。
你确定那些*IsSpecified
参数不存在吗?以我Contact
上面的类为例,minOccurs=0
在BirthDate
属性上会给Contact
类一个额外的属性,称为BirthDateIsSpecified
.