title: How to configure asp.net webforms WebMethod Date format
body: <p>I have a webmethod that receives an object that contains a date property.
  I am passing a json that represents the object to the webmethod.
  asp.net automatically converts the json string to an object so my method looks like: </p>
  
  <pre><code>[WebMethod]
  public static Object Save(MyObject item)
  {
     ....
  }
  
  public class MyObject 
  {
       DateTime date;
       .....
  }
  </code></pre>
  
  <p>This all works well when the date is sent in USA format: <code>'MM/dd/yyyy'</code>
  When the date is sent in European format: <code>'dd/MM/yyy'</code> I receive an error.</p>
  
  <p>I am setting the culture of the thread in global asax:</p>
  
  <pre><code>void Application_AcquireRequestState(object sender, EventArgs e)
  {
       var s = GetSessionValues();
       System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(s.CultureID);
       System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(s.CultureID);
  }
  </code></pre>
  
  <p>This code runs correctly it reads the culture id from the session and sets it.
  The culture id is read into the session upon login from the database.</p>
  
  <p>But this seems to have no affect on the date format expected by the web method i receive the error:</p>
  
  <pre><code>"31/08/2012 is not a valid value for DateTime.",
  "StackTrace":"  
  at System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)\r\n  
  at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object&amp; convertedObject)\r\n  
  at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object&amp; convertedObject)\r\n  
  at System.Web.Script.Serialization.ObjectConverter.AssignToPropertyOrField(Object propertyValue, Object o, String memberName, JavaScriptSerializer serializer, Boolean throwOnError)\r\n  
  at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object&amp; convertedObject)\r\n  
  at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object&amp; convertedObject)\r\n  
  at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object&amp; convertedObject)\r\n  
  at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)\r\n  
  at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n  
  at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n  
  at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n  
  at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.FormatException"}
  </code></pre>
  
  <p>How can I set the expected date format for the webmethod (I have a lot of these web method so obviously I want a solution that would apply for all of them).</p>
  
  <p>Thanks</p>