1

我使用 ServiceStack 开发了一个 Rest Service。我的模型包含一个 DateTime 属性,问题从它开始。如果客户端发布/获取格式错误的日期值作为字符串,ServiceStack 会触发异常“字符串未被识别为有效的日期时间”。

Stack Trace: 


[FormatException: String was not recognized as a valid DateTime.]
   System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles) +6364458
   ServiceStack.Text.Common.DeserializeBuiltin`1.<GetParseFn>b__b(String value) in C:\src\ServiceStack.Text\src\ServiceStack.Text\Common\DeserializeBuiltin.cs:58
   ServiceStack.ServiceModel.Serialization.StringMapTypeDeserializer.PopulateFromMap(Object instance, IDictionary`2 keyValuePairs) in C:\src\ServiceStack\src\ServiceStack.Common\ServiceModel\Serialization\StringMapTypeDeserializer.cs:79

[SerializationException: KeyValueDataContractDeserializer: Error converting to type: String was not recognized as a valid DateTime.]
   ServiceStack.ServiceModel.Serialization.StringMapTypeDeserializer.PopulateFromMap(Object instance, IDictionary`2 keyValuePairs) in C:\src\ServiceStack\src\ServiceStack.Common\ServiceModel\Serialization\StringMapTypeDeserializer.cs:95
   ServiceStack.ServiceHost.RestPath.CreateRequest(String pathInfo, Dictionary`2 queryStringAndFormData, Object fromInstance) in C:\src\ServiceStack\src\ServiceStack\ServiceHost\RestPath.cs:319
   ServiceStack.WebHost.Endpoints.RestHandler.GetRequest(IHttpRequest httpReq, IRestPath restPath) in C:\src\ServiceStack\src\ServiceStack\WebHost.Endpoints\RestHandler.cs:104
   ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName) in C:\src\ServiceStack\src\ServiceStack\WebHost.Endpoints\RestHandler.cs:80
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +625
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +270

我知道它是一个错误,我知道它一定是捕获但我想用我的错误代码和错误描述(人性化)来实现它。

任何建议都可能对如何在异常写入响应(DebeugMode=true)或服务向客户端触发异常之前捕获异常非常有帮助。

谢谢

4

1 回答 1

3

添加您的自定义请求绑定器(或更高级的选项是在您的 DTO 上实现 IRequiresRequestStream)以使用您自己的绕过 ServiceStack 的反序列化。

有关这方面的更多信息包含在ServiceStack 的 wiki 文档中

于 2012-07-24T16:06:59.630 回答