在 biztalk 映射内的 C# 脚本中使用 ParseExact 无法将字符串识别为 DateTime
String not recognized as DateTime using ParseExact in a C# Script inside a biztalk map
地图的上一个工作版本:
在此版本中,输入数据的类型为 xs:date
地图的目标版本:
在此版本中,我将平面文件中的数据类型更改为 xs:string。我还添加了以下在新地图中突出显示的 C# 脚本:
public static System.DateTime MyConcat(string param1)
{
string[] formats = { "yyyy\/MM\/dd", "dd-MM-yyyy","yyyy-MM-dd", "dd\/MM\/yyyy" };
return System.DateTime.ParseExact(param1, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
}
我将字符串“2018-03-15”传递给数据输入。
我得到的错误:
ABC.MW.BackEnds.CENTRAL.DocsInformacaoFinanceira.Balancete.Mappers.MapBalancete. Error:Transformation failed.. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Xml.Xsl.XslTransformException: An error occurred during a call to extension function 'DateFormat'. See InnerException for a complete description of the error. ---> System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
从错误中我推断问题是从字符串到日期时间的隐式转换。但我不知道它可能发生在哪里。
我找到了错误的根源。这是之前工作函数中的scriptoids调用的方法。
我假设 xs:date 和我的 scriptoid returns 不是一回事。
出现错误的函数代码:
public string DateFormat(string value, string FormatoOrigem, string FormatoDestino)
{
DateTime myDate;
if (string.IsNullOrEmpty(value))
return ""; //myDate = DateTime.MinValue;
else
myDate = System.DateTime.ParseExact(value,
FormatoOrigem,
System.Globalization.CultureInfo.InvariantCulture);
return myDate.ToString(FormatoDestino, System.Globalization.CultureInfo.InvariantCulture);
}
调用函数的脚本输入:
我们必须专门为 BizTalk 运行时解决这个问题。所以 DateFormat()
是此错误的来源,因此您需要从那里开始。
此外,请记住,有时 DateTime 是正确的,有时 returning a String 是正确的。所以,你们很多人需要在某处更改 return 类型。
DateFormat函数的参数是日期值和2种日期格式。
脚本 functoid 的调用首先传递格式,最后传递值。
当调用方法 ParseExact 使用的是格式而不是值时,这将导致函数 DateFormat 中出现错误。
感谢@Daisy Shipton 帮助我发现错误出在我开发的脚本之外。
感谢@Johns-305 帮助我找出错误的真正原因。
地图的上一个工作版本:
在此版本中,输入数据的类型为 xs:date
地图的目标版本:
在此版本中,我将平面文件中的数据类型更改为 xs:string。我还添加了以下在新地图中突出显示的 C# 脚本:
public static System.DateTime MyConcat(string param1)
{
string[] formats = { "yyyy\/MM\/dd", "dd-MM-yyyy","yyyy-MM-dd", "dd\/MM\/yyyy" };
return System.DateTime.ParseExact(param1, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
}
我将字符串“2018-03-15”传递给数据输入。
我得到的错误:
ABC.MW.BackEnds.CENTRAL.DocsInformacaoFinanceira.Balancete.Mappers.MapBalancete. Error:Transformation failed.. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Xml.Xsl.XslTransformException: An error occurred during a call to extension function 'DateFormat'. See InnerException for a complete description of the error. ---> System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
从错误中我推断问题是从字符串到日期时间的隐式转换。但我不知道它可能发生在哪里。
我找到了错误的根源。这是之前工作函数中的scriptoids调用的方法。
我假设 xs:date 和我的 scriptoid returns 不是一回事。
出现错误的函数代码:
public string DateFormat(string value, string FormatoOrigem, string FormatoDestino)
{
DateTime myDate;
if (string.IsNullOrEmpty(value))
return ""; //myDate = DateTime.MinValue;
else
myDate = System.DateTime.ParseExact(value,
FormatoOrigem,
System.Globalization.CultureInfo.InvariantCulture);
return myDate.ToString(FormatoDestino, System.Globalization.CultureInfo.InvariantCulture);
}
调用函数的脚本输入:
我们必须专门为 BizTalk 运行时解决这个问题。所以 DateFormat()
是此错误的来源,因此您需要从那里开始。
此外,请记住,有时 DateTime 是正确的,有时 returning a String 是正确的。所以,你们很多人需要在某处更改 return 类型。
DateFormat函数的参数是日期值和2种日期格式。 脚本 functoid 的调用首先传递格式,最后传递值。 当调用方法 ParseExact 使用的是格式而不是值时,这将导致函数 DateFormat 中出现错误。
感谢@Daisy Shipton 帮助我发现错误出在我开发的脚本之外。 感谢@Johns-305 帮助我找出错误的真正原因。