Формат для значений DateTime
в строке URI - это любой формат, который может быть распознан с помощью DateTime.Parse
. Что-то вроде startDate.ToString("yyyy-MM-dd")
отлично работает.
public class StackOverflow_8472985
{
[ServiceContract]
public class Service
{
[WebGet]
public int DiffDates(DateTime startDate, DateTime endDate)
{
Console.WriteLine("[service] startDate: {0}", startDate);
Console.WriteLine("[service] endDate: {0}", endDate);
return (int)endDate.Subtract(startDate).TotalDays;
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
WebClient c = new WebClient();
DateTime startDate = new DateTime(2011, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime endDate = new DateTime(2011, 12, 12, 0, 0, 0, DateTimeKind.Utc);
string uri = string.Format(baseAddress + "/DiffDates?startdate={0}&endDate={1}",
startDate.ToString("yyyy-MM-dd"),
endDate.ToString("yyyy-MM-dd"));
Console.WriteLine("URI: {0}", uri);
Console.WriteLine(c.DownloadString(uri));
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}