【问题标题】:Telegram.Bot SendLocationAsync returns "Input string was not in a correct format" only when running as a ServiceTelegram.Bot SendLocationAsync 仅在作为服务运行时返回“输入字符串格式不正确”
【发布时间】:2020-04-26 14:44:44
【问题描述】:

我正在运行 C# .Net Core 控制台应用程序即服务(使用 Topshelf),一切都很好,除了 Telegram.Bot SendLocationAsync 返回错误:“输入字符串格式不正确”

在调试模式下,它和发布控制台应用程序一样工作正常。其他电报消息工作 100%。 绝对只有当我将应用程序作为服务运行时。

欢迎提出任何想法?

使用的一些示例代码:

        string locString = "-25.2567752,28.7920051";
        try
        {
            string[] cam_loc = locString.Split(",");
            double Lat = Convert.ToDouble(cam_loc[0]);
            double Long = Convert.ToDouble(cam_loc[1]);
            var Result1 = await Bot.SendLocationAsync(chatId: -625759052, latitude: (float)Lat, longitude: (float)Long);
        }
        catch (Exception ex)
        {
            Log.Error("Error Location Send: " + ex.Message);
        }

【问题讨论】:

    标签: telegram topshelf


    【解决方案1】:

    发现的问题是没有正确解析的文化信息

    以下解决了问题:

    double.TryParse(cam_loc[0], NumberStyles.Number, CultureInfo.InvariantCulture, out 纬度_)

    if (double.TryParse(cam_loc[0], NumberStyles.Number, CultureInfo.InvariantCulture, out 
        Lat_) && double.TryParse(cam_loc[1], NumberStyles.Number, 
        CultureInfo.InvariantCulture, out Long_))
    {
        Log.Debug("Lat/Long values: " + Lat_.ToString() + " / " + Long_.ToString());
        try
        {
            var Result1 = await Bot.SendLocationAsync(chatId: vp.cliGroup, latitude: 
        (float)Lat_, longitude: (float)Long_);
        }
        catch (Exception ex)
        {
            Log.Error("Error Location Send: " + ex.Message);
        }
    }
    else
        Log.Debug("Lat/Long (err) values: " + Lat_.ToString() + " / " + Long_.ToString());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 2022-01-23
      相关资源
      最近更新 更多