Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send DateTime parameters to WCF DataService

Tags:

wcf

I have simple function on the DataService that get DateTime parameter

[WebGet]
public IQueryable<Job> LoadJobsByDate(DateTime startDate, DateTime endDate)
{
    var context = this.CurrentDataSource;

    var jobs = from j in context.Jobs
               where j.CreatedDate >= startDate && j.CreatedDate <= endDate
               select j;

    return jobs;
}

What ever syntax I'm try to send I get error.

I tried

var query1 = newContext.CreateQuery<Job>("LoadJobsByDate")
             .AddQueryOption("startDate", 
                 string.Format ("'{0:yyyy-MM-ddTHH:mm:ss}'", DateTime.Now));  

or:

var query1 = newContext.CreateQuery<Job>("LoadJobsByDate")
             .AddQueryOption("startDate", 
                 string.Format ("'datetime{0:yyyy-MM-ddTHH:mm:ss}'", DateTime.Now));  

Please advice

like image 721
shlomi Avatar asked Apr 03 '11 07:04

shlomi


2 Answers

You can submit a DateTime object to the service through a HTTP request using the ISO 8601 timestamp format, which expresses time as yyyy-mm-ddThh:mm:ss (example: 2011-06-02T12:24:34).

like image 76
Hal Avatar answered Oct 16 '22 02:10

Hal


I don't think (from my tests and experiments) that you can pass parameters of type DateTime directly. After all, in the end (after all the niceties of the WCF DataService client-side proxy and its LINQ-to-WCF goodness), WCF Data Services is always getting its parameters from the URL query string, so basically, it's all just strings....

(this is in stark contrast to using the SOAP bindings for WCF - there, you can definitely use strongly typed parameters - DateTime and anything you like. But WCF Data Services is REST, it's all URL- und thus string-based)

So my conclusion is: you need to change your code to use string parameters and then converts those to DateTime inside your method:

[WebGet]
public IQueryable<Job> LoadJobsByDate(string startDate, string endDate)
{
    // some error checking needs to be done here! If 'startDate' or 'endDate'
    // are NULL or emtpy string --> fall back to a default

    // also - you might want to check into .ParseExact and define a list of valid,
    // supported date formats that you want to offer your users
    DateTime startDt = DateTime.Parse(startDate);
    DateTime endDt = DateTime.Parse(endDate);

    var context = this.CurrentDataSource;

    var jobs = from j in context.Jobs
               where j.CreatedDate >= startDt && j.CreatedDate <= endDt
               select j;

    return jobs;
}

As I mentioned in the code snippet - having to get strings and parse them into DateTime requires that you do some additional error checking on your part - it's a pain, but I think it's necessary to make sure your code doesn't break the first time someone enters no date or an invalid date format....

like image 31
marc_s Avatar answered Oct 16 '22 01:10

marc_s