。NET Core 2.2时间。ToString(“ d”,区域性)异常

问题描述 投票:1回答:1

我有一个非常奇怪的情况,mydatetime.ToString(“ d”,文化)有时会引发异常。

这是在.Net Core 2.2上的Azure中运行的简单Azure Premium功能。这是存储队列上的触发器。此触发器每月处理约250万次。有时,ToString抛出“外部组件引发了异常”;

try {
 var culture = new System.Globalization.CultureInfo(order.Address.Country.CultureCode);

 var date = order.CreatedDate;

 var test = date.ToString("d", culture); //This throws exceptions.
} catch (Exception exception) {
 await StaticLoggers.HandleExceptionAsync(new BaseServices.Models.ExceptionModel {
  Environment = "CustomException",
   Message = $ "{order.CreatedDate.MyToDateString()} {order.Address.Country.CultureCode} " + exception.Message.MyToStringTrim(),
   StackTrace = exception.StackTrace.MyToStringTrim()
 });

 return null;
}

从日志来看,异常是完全随机的,有时一天没有错误,有时大约1000。我观察到的是,它在高峰时段的确会抛出更多信息,所以我最好的猜测是,这与Azure中的并行执行有关。另外,出现异常时,日期和区域会有所不同,因此我也怀疑这不是错误的原因。这是我在ToString引发异常时记录的消息。

2020/01/10 01:10:24 de-DE External component has thrown an exception.

日期永远不会为空,并且始终存在真实的文化。如果我注释ToString行,则不会发生错误。

堆栈跟踪:

at Interop.Kernel32.EnumCalendarInfoExEx(EnumCalendarInfoProcExEx pCalInfoEnumProcExEx, String lpLocaleName, UInt32 Calendar, String lpReserved, UInt32 CalType, Void* lParam) 
at System.Globalization.CalendarData.GetCalendars(String localeName, Boolean useUserOverride, CalendarId[] calendars) at System.Globalization.CultureData.get_CalendarIds() 
at System.Globalization.DateTimeFormatInfo.set_Calendar(Calendar value) 
at System.Globalization.CultureInfo.get_DateTimeFormat() at System.DateTimeFormat.Format(DateTime dateTime, String format, IFormatProvider provider, TimeSpan offset) 
at System.DateTimeFormat.Format(DateTime dateTime, String format, IFormatProvider provider) 
at Automation.ProcessOrderEvent.EventService.EventWorkflowService.GetOrderAsync(Int32 orderId) in C:\...\EventService\OrderUpdated.cs:line 328

更新1:上面的错误消息是完整的堆栈跟踪,出于安全原因,除了物理路径外,没有其他任何内容被删除或编辑。这是最顶层的样板。

using BusinessLogicLayer.Services;
using BusinessLogicLayer.Services.Interfaces;

[assembly: WebJobsStartup(typeof(Startup))]
namespace Automation.ProcessOrderEvent
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddMemoryCache();
            builder.Services.AddScoped<IOrderService, OrderService>();
        }
    }

    public class ProcessOrderEvent
    {
        private readonly IOrderService _orderService;

        public ProcessOrderEvent(IOrderService orderService) => _orderService = orderService;

        [FunctionName("ProcessAwesomeEvent")]
        public async Task Run([QueueTrigger("eventqueue", Connection = "AzureWebJobsStorage")] string myQueueItem, ExecutionContext context, ILogger logger)
        {
            await _orderService.ProcessOrderUpdate(myQueueItem);
        }
    }
}

这是订购服务中的代码:

public partial class EventWorkflowService
{
    public async Task ProcessOrderUpdate(orderId)
    {
        var order = await GetOrderAsync(orderId);
        if (order == null)
            return;

        var date = order.CreatedDate.ToString("d", new CultureInfo(order.Address.Country.CultureCode));
        //rest of the code
    }
}

GetOrder是一个简单的System.Data.SqlClient查询,它获取数据并将其绑定到订单模型。

c# .net azure-functions asp.net-core-2.2
1个回答
0
投票

根据错误消息,我们可以找到一行:

at System.DateTimeFormat.Format(DateTime dateTime, String format, IFormatProvider provider)

但是“ DateTimeFormat”仅存在于.net 2.2的“ System.Globalization.CultureInfo”中。

错误消息中的另一行是:

at System.Globalization.CultureInfo.get_DateTimeFormat() at System.DateTimeFormat.Format(DateTime dateTime, String format, IFormatProvider provider, TimeSpan offset)

所以我认为这可能会导致错误消息,这两个“ DateTimeFormat”不匹配。

由于我看不到您的所有代码和功能,因此我只能提供此信息,我怀疑这可能是您参考的原因。请检查您的代码,并注意代码中的“ DateTimeFormat”(也许当您从sql中获得订单时,我猜当您从sql中获得“ DateTimeFormat”时是“ System.DateTimeFormat”,而当您新建“ CultureInfo”时, “,如果可以只使用.net 2.2中的” System.Globalization.CultureInfo.DateTimeFormat“。

© www.soinside.com 2019 - 2024. All rights reserved.