如何在SOAP请求消息中验证日期

时间:2014-05-07 13:33:18

标签: wcf soap soapui

我有一个wcfservice,它获取请求并发送响应。 在请求对象中,我的一个元素是DateTime类型。 当我传递正确的DateTime格式时(例如:2012-12-30),响应对象工作正常并且没有问题。 但是当我以正确的格式传递元素时(例如:201-12-30),服务会提供异常并抛出无效的日期格式异常。

更正请求:

<req:paymentDateDate>2012-12-12</req:paymentDateDate>

错误请求

<req:paymentDateDate>2012-12-12</req:paymentDateDate>

,响应将是

<InnerException i:nil="true"/>
                 <Message>String was not recognized as a valid DateTime.</Message>

如何从代码验证它,我正在验证以下内容:

  else if (request.paymentRequest.payment.paymentDateDate == DateTime.MinValue )
            {
                FaultException fx = BillPayFaultResponse(request, "Schema Validation Failed", "Invalid content,element 'paymentDateDate' is required,but not found.");
                throw fx;
            }  

如何验证我的服务是否正在接收有效的日期时间字符串

1 个答案:

答案 0 :(得分:2)

如果您只是需要检查某个日期范围内的日期,最好是实施 IParameterInspector 。它允许您检查客户端和服务端的参数。

以下是日期时间验证的示例

IParameterInspector&amp; IOperationBehavior实现:

public class DateTimeParameterInspector: IParameterInspector
{
    private static readonly ILog Logger = LogManager.GetLogger(typeof (DateTimeParameterInspector));
    private static readonly DateTime MinDateTime = new DateTime(1900, 1, 1);
    private static readonly DateTime MaxDateTime = new DateTime(2100, 1, 1);

    private readonly int[] _paramIndecies;
    public DateTimeParameterInspector(params int[] paramIndex)
    {
        _paramIndecies = paramIndex;
    }

    public object BeforeCall(string operationName, object[] inputs)
    {
        try
        {
            foreach (var paramIndex in _paramIndecies)
            {
                if (inputs.Length > paramIndex)
                {
                    var dt = (DateTime) inputs[paramIndex];

                    if (dt < MinDateTime || dt > MaxDateTime)
                    {
                        var errorMessage = String.Format(
                            "Invalid date format. Operation name: {0}, param index{1}", operationName, paramIndex);
                        Logger.Error(errorMessage);
                        throw new FaultException(errorMessage);
                    }
                }
            }
        }
        catch(InvalidCastException exception)
        {
            Logger.Error("Invalid parameter type", exception);
            throw new FaultException(
                   "Invalid parameter type");
        }
        return null;
    }

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    { }
}

public class DateTimeInspectorAttrubute: Attribute, IOperationBehavior
{
    private readonly int[] _paramIndecies;

    public DateTimeInspectorAttrubute(params int[] indecies)
    {
        _paramIndecies = indecies;
    }

    public void Validate(OperationDescription operationDescription)
    {
    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        dispatchOperation.ParameterInspectors.Add(new DateTimeParameterInspector(_paramIndecies));
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
        clientOperation.ParameterInspectors.Add(new DateTimeParameterInspector(_paramIndecies));
    }

    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {

    }
}

以下是我们的参数检查器用法示例:

[ServiceContract]
public interface IPricingService
{
    [OperationContract]
    [DateTimeInspectorAttrubute(0, 1)]
    string GetPrice(DateTime startDate, DateTime endDate);

}
public class PricingService : IPricingService
{
    public string GetPrice(DateTime startDate, DateTime endDate)
    {
        return "result";
    }
}

如果您可以将您的肥皂信息保密至日期,并且想要查看它,则必须参考 IDispatchMessageInspector

要使代码成为compliled,您必须引用System.ServiceModel,log4net(您可以在nuget存储库中找到它)