Reputation: 2505
I have a wcfservice where it gets request and sends a response. In the request object one of my element is of type DateTime. When i am passing correct format of DateTime(eg:2012-12-30),the response object works fine and there are no issues. But when i pass the element in correct format(eg: 201-12-30),the service gives exception and throws invalid date format exception.
Correct request:
<req:paymentDateDate>2012-12-12</req:paymentDateDate>
Bad request
<req:paymentDateDate>2012-12-12</req:paymentDateDate>
and the response will be
<InnerException i:nil="true"/>
<Message>String was not recognized as a valid DateTime.</Message>
how to validate it from code,I am validating for mindate as following:
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;
}
how to validate for whether my service is receiving valid datetime string or not
Upvotes: 1
Views: 1335
Reputation: 1120
If you just need to check your date for being in some date range, it is better for you to implement IParameterInspector. It will allow you to check parameters both on client and service sides.
Here is an example of DateTime Validation:
IParameterInspector & IOperationBehavior implementations:
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)
{
}
}
Here is an example of our parameter inspector usage:
[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";
}
}
In case you can have you your soap message invalide date, and you want to check it, you have to refer to IDispatchMessageInspector
To make the code to be compliled you have to reference System.ServiceModel, log4net (you can find it in nuget repository)
Upvotes: 2