【问题标题】:Date Format Issue with Web serviceWeb 服务的日期格式问题
【发布时间】:2016-04-21 21:13:43
【问题描述】:

我有一个带有输入参数的网络服务。 WSDL 中相关的 XSD 如下

当我在 Visual Studio 中将此 WSDL 作为服务引用添加时,它生成了一个类,其对应字段为System.DateTime。下面是为 WSDL 添加的 Reference 类中的字段示例

private System.Nullable<System.DateTime> startDateField;

我与创建客户端的服务的绑定是下面的 CustomBinding

 protected CustomBinding GetCustomBinding()
    {
        var customBinding = new CustomBinding() { Name = "CustomBinding" };

        customBinding.Elements.Add(new TextMessageEncodingBindingElement() { MessageVersion = MessageVersion.Soap11 });
        var securityBindingElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
        securityBindingElement.AllowInsecureTransport = true;
        securityBindingElement.EnableUnsecuredResponse = true;
        securityBindingElement.IncludeTimestamp = false;
        customBinding.Elements.Add(securityBindingElement);
        customBinding.Elements.Add(new HttpTransportBindingElement());

        return customBinding;
    }

我的 c# 代码分配输入

myobject.input.endDate = Convert.ToDateTime(endDate);

分配输入值后,我调用了一个 web 方法,在 Fiddler 中查看请求中缺少所有日期参数。

我尝试在 SoapUI 中进行测试。看起来服务需要yyyy-MM-dd 格式的日期,尽管类型是日期。仅当我以 yyyy-MM-dd 格式提供日期时,Web 服务才会返回数据。

我不确定这是否与 Web 服务的预期日期格式有关。显然,我不能以 yyyy-MM-dd 格式发送,因为 .Net 生成的参考类具有 DateTime 但不是 string 数据类型。

我试图强行将Specified设置为true

myobject.input.endDate = Convert.ToDateTime(endDate).Date;
myobject.input.endDateSpecified = true;

我收到以下错误:

A value was being set that exceeded the maximum allowable field length.

现在,我怀疑 Web 服务需要 Date,但 .Net 正在尝试发送可能会延长其长度的 DateTime

【问题讨论】:

    标签: c# web-services date


    【解决方案1】:

    看起来我的上一个代码有效,但错误是由另一个字段引起的。

    myobject.input.endDate = Convert.ToDateTime(endDate).Date;
    myobject.input.endDateSpecified = true;
    

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      相关资源
      最近更新 更多