繁体   English   中英

不同客户端上的WCF服务日期时间解析错误

[英]WCF Service Datetime parse error on different clients

关于使用WCF服务,我有一个问题:

接口:

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

请求:

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

响应:

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

类:

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; }
}

实现方式:

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解析器:

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;
    }

我尝试在DateTime.ParseExact上使用CultureInfo.InvariantCulture ,但仍然没有结果。 更奇怪的是,当我在笔记本电脑上本地运行它时,它可以工作,但在我的办公室PC上却显示错误消息。 我从另外两台笔记本电脑进行了测试,一台没有错误,另一台有相同错误。 我不知道我是否没有正确使用DateTime.ParseExact或是否缺少其他东西。

堆栈跟踪:

System.FormatException:字符串未被识别为有效的DateTime。 在System.DateTime.ParseExact(String s,字符串格式,IFormatProvider提供程序)在System.DateTimeParse.ParseExact(String s,字符串格式,DateTimeFormatInfo dtfi,DateTimeStyles样式)

我敢打赌发生的事情是,给您带来麻烦的计算机具有不同的日期格式-请参见控制面板->区域->格式

最好的办法是尝试TryParseExact该计算机的格式。

更好的选择是执行类似date.ToString("o") ,这将强制DateTime字符串采用通用格式,因此解析时应该没有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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