简体   繁体   English

嗨,我正在尝试在 Azure 应用服务中运行 web 作业,但它抛出与日期时间相关的错误

[英]Hi, I am trying to run a web job in Azure app service but it is throwing Error related to Datetime

I running a webjob in Azure app service which will get the current datetime and do some operation.我在 Azure 应用服务中运行一个网络作业,它将获取当前日期时间并进行一些操作。 but this works fine when I run it in my local machine (I am creating a webjob using a.Net console application).Once I try to run it as Webjob then I am getting this error.但是当我在本地机器上运行它时效果很好(我正在使用.Net控制台应用程序创建一个webjob)。一旦我尝试将它作为Webjob运行,我就会收到这个错误。

Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime.
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
System.Convert.ToDateTime(String value)

for more details please find the code below有关更多详细信息,请在下面找到代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.IO;

namespace Zoho_API_Services
{
    class Items
    {
        #region DateTime
        public DateTime datetime { get; set; }
        public static DateTime Current_time { get; set; }
        public static DateTime Target_time { get; set; }

    //Constructor to get current date time
        public Items()
        {
            datetime = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
        }
        #endregion

        public static void Main()
        {
        //Call the constructor
            Items obj = new Items();

            //Initialize Current time and Target time
            Current_time = obj.datetime;
            Target_time = Current_time.AddMinutes(55);
           
                if (Current_time >= Target_time)
                {
                    //Reset the Current time and Target time
                    Current_time = obj.datetime;
                    Target_time = Current_time.AddMinutes(55);
                    
                    // Do something
                }

                else
                {
                    // Do Something                   
                }

            Console.ReadKey();
        }

    }
}

The error is thrown when constructor is being called.调用构造函数时会引发错误。 Please Help请帮忙

There are several problems with this line of code:这行代码有几个问题:

datetime = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
  • DateTime.Now uses the system's local time zone. DateTime.Now使用系统的本地时区。 In the cloud this is UTC by default.在云中,默认为 UTC。

    • Since you are using Azure web jobs, you could set the WEBSITE_TIME_ZONE app setting, but only on Windows.由于您使用的是 Azure web 作业,您可以设置WEBSITE_TIME_ZONE应用程序设置,但仅限于 Windows。 If you're running on Linux, this won't work.如果您在 Linux 上运行,这将不起作用。
    • A better option is to use DateTime.UtcNow and then convert to a specific time zone using TimeZoneInfo.ConvertTime if necessary.更好的选择是使用DateTime.UtcNow ,然后在必要时使用TimeZoneInfo.ConvertTime转换为特定时区。
  • In your string format, you are using hh which is for hours 1-12 on a 12-hour clock.在您的字符串格式中,您使用的是hh ,即 12 小时制的 1-12 小时。 Thus, if the time is 13:00 or later, you will lose 12 hours when you parse it.因此,如果时间是 13:00 或更晚,解析时会损失 12 个小时。 HH is for hours of a 24-hour clock. HH表示 24 小时制中的几个小时。

  • In your string format, you are using dd/MM/yyyy date format.在您的字符串格式中,您使用的是dd/MM/yyyy日期格式。 That may be your format locally, but by default in Azure the CurrentCulture is the InvariantCulture , which uses MM/dd/yyyy format.这可能是您在本地的格式,但默认情况下,在 Azure 中, CurrentCultureInvariantCulture ,它使用MM/dd/yyyy格式。 You get the error because you're trying to parse a value like 15/04/2021 by treating 15 as a month, which is out of range.您收到错误是因为您试图通过将 15 视为一个月来解析像15/04/2021这样的值,这超出了范围。

  • One should never ever create a string just to parse it again in the same code.永远不应该创建一个字符串只是为了在相同的代码中再次解析它。 There's absolutely no reason for that.绝对没有理由这样做。 If your intent was to truncate fractional seconds, you can do that with:如果您的意图是截断小数秒,您可以这样做:

     datetime = datetime.AddTicks(-datetime.Ticks % TimeSpan.TicksPerSecond);

Putting it all together, you should probably be doing the following:综上所述,您可能应该执行以下操作:

// I chose the time zone based on the location in your Stack Overflow user profile.
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("India Standard time"); // or "Asia/Kolkata" on Linux

// Get the UTC time and convert to the given time zone.
datetime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);

// Truncate to seconds.
datetime = datetime.AddTicks(-datetime.Ticks % TimeSpan.TicksPerSecond);

The above will avoid any impact of culture settings because there's no parsing or formatting going on.以上将避免文化设置的任何影响,因为没有进行解析或格式化。

Also, you should remove Console.ReadKey();此外,您应该删除Console.ReadKey(); - It will hang your web job. - 它将挂起您的 web 作业。

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

相关问题 尝试在Azure App服务上使用WEB API时出错 - Error when trying to use WEB API on Azure App service 当我尝试运行我的 C# 创建的 API 时,它抛出错误 - When I am trying run my C# created API it is throwing an error 在 Azure 应用服务 Web 作业中使用 X509KeyStorageFlags.EphemeralKeySet 时出错 - Error when using X509KeyStorageFlags.EphemeralKeySet in Azure App Service Web Job Web服务抛出错误 - Web Service Throwing Error Azure 应用服务抛出运行时错误:未找到程序集 - Azure App Service throwing Runtime Error: Assembly not Found 作为Web作业的Azure Service Bus引发错误提供的锁无效 - Azure Service Bus as Web Job throw error The lock supplied is invalid 尝试连接到Azure Web App的授权错误 - Authorization error trying to connect to Azure Web App 我正在尝试在 C# 应用程序上运行一些 powershell 脚本以授予 Azure AD 应用程序的权限,它适用于 Powershell 但不适用于 C# 应用程序 - I am trying to run some powershell script on C# app to grant permission on Azure AD App, it works on Powershell but not on C# app 当我尝试从Xamarin Samples运行Social Framework Demo App时遇到错误 - Getting error when i am trying to run Social Framework Demo App from Xamarin Samples 为什么在尝试将调试过程附加到Microsoft Azure应用程序服务时出现错误? - Why I get error while trying to attached debugging process to a Microsoft Azure app service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM