简体   繁体   中英

WCF Service Datetime parse error on different clients

I have an issue regarding consumption of a WCF service:

Interface:

[OperationContract]
[WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    UriTemplate = "list_LogUser")]
 DTO.Responses.List_LogUser list_LogUser(DTO.Requests.Token token);

Request:

public class Token
{
    public string ApiToken { get; set; }
}

Response:

public class List_LogUsuario
{
    public List<Select.LogUser> LogUsers { get; set; }
}

Class:

public class LogUser
{
   public int IdLogUser { get; set; }
   public string User { get; set; }
   public string Action { get; set; }
   public string Class { get; set; }
   public DateTime Date { get; set; }
}

Implementation:

public DTO.Responses.List_LogUser list_LogUser(DTO.Requests.Token token)
{
    try
    {
        DTO.Responses.List_LogUsuario response = new DTO.Responses.List_LogUsuario();

        if (BBSessionManager.Instance.validateAPIKEY(token.ApiToken))
        {
            response.LogUsers = new List<Select.LogUser>();
            Select.LogUser l = new Select.LogUser();

            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter();
            string ConnString = ConfigurationManager.ConnectionStrings[clientConnString].ConnectionString;
            using (SqlConnection SqlConn = new SqlConnection(ConnString))
            {
                SqlConn.Open();

                SqlCommand sqlCmd = new SqlCommand(spName, SqlConn);
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sda.SelectCommand = sqlCmd;
                sda.Fill(dt);
                SqlConn.Close();
                sqlCmd.Dispose();
                sda.Dispose();
            }

            DataRow[] rows = dt.Select();

            for (int i = 0; i < rows.Length; i++)
            {
                l = Utils.logUser_parse(rows[i]);
                response.LogUsers.Add(l);
            }

            response.Response = Constants.OK;
        }
        else
        {
            response.Response = Constants.ERROR;
        }

        return response;
    }
    catch (Exception ex)
    {
        ...
    }

}

BD Parser:

public static Select.LogUser logUser_parse(DataRow r)
    {
        Select.LogUser l = new Select.LogUser();
        ...
        l.Date = DateTime.ParseExact(r["Date"].ToString(), "M/d/yyyy h:mm:ss ttt", CultureInfo.InvariantCulture);
        ...

        return l;
    }

I have tried using CultureInfo.InvariantCulture on DateTime.ParseExact but still no results. What is more curious is that when I locally run it on my laptop it works but from my office PC it gives me the ERROR. I tested it from two another laptops, one with no error another with same error. I do not know if I am not using DateTime.ParseExact correctly or if I'm missing something else.

StackTrace:

System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style) at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)

I bet what is happening is that the computer that is giving you trouble has has a different date format - see Control Panel -> Region -> Formats

The best thing for you to do is try to TryParseExact the format of that computer.

A better option would be to do something like date.ToString("o") which will force the DateTime string to be in a universal format that you should have no problem parsing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM