c# - 未找到单声道 MissingMethodException : DateTimeOffset. FromUnixTimeSeconds

标签 c# linux mono

我正在尝试使用 https://github.com/MrRoundRobin/telegram.bot 编写电报机器人, 它是 Telegram 机器人的 C# api。

编译后的程序在 Windows 上运行没有任何问题,但是当尝试在 Raspbian Linux(Debian on ARM 处理器)上使用 Mono 运行它时,它崩溃了

System.MissingMethodException: Method 'DateTimeOffset.FromUnixTimeSeconds' not found.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable

加上整个堆栈跟踪的剩余部分。 telegram.bot 方法显然使用 Newtonsoft.Json 序列化请求/反序列化响应。

Mono JIT compiler version 4.2.2 (Stable 4.2.2.30/996df3c Fri Jan 00:48:14 UTC 2016)

如有任何帮助,我们将不胜感激。

最佳答案

这是因为在旧的 .Net 版本中缺少 DateTimeOffset.FromUnixTimeSeconds,因为在 Net 4.6 中添加了这个扩展,ofc 我也会告诉 MrRR 来解决这个问题的向后兼容性,但你的 prj .Net 版本可能是更低,而且您似乎从方法中使用了可等待的用法,因此它会抛出异常。

顺便说一句,你可以围绕它做一些事情,

1:最常见

        try
        {
            // your code
            var result = await bot.SendPhoto(id, file);
        }
        catch (Exception ex)
        {
            // handleing code
        }

或您所知道的更准确:

        try
        {
            // your code
            var result = await bot.SendPhoto(id, file);
        }

        //-- Because this exception is a kind of AggregateExceptions
        catch (AggregateException ex) 
        // for C# 6.0 you can use when (somehow)
        // catch (AggregateException ex) when (something to check  Method not found)
        {
            // handleing code
        }

2:自己添加一个扩展(这个我还没有检查)

尝试将具有精确“FromUnixTimeSeconds”的扩展添加到 DateTimeOffset,并实现具有相同返回值的方法。 知道如何更好地实现它,就像 .Net 4.6 可以从这里使用:https://msdn.microsoft.com/en-us/library/system.datetimeoffset.fromunixtimeseconds(v=vs.110).aspx

而且这是基本异常,完整的堆栈跟踪可以帮助更多人了解这一点:

   Method not found: 'System.DateTimeOffset System.DateTimeOffset.FromUnixTimeSeconds(Int64)'.
   at Telegram.Bot.Helpers.UnixDateTimeConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, Encoding effectiveEncoding, IFormatterLogger formatterLogger)
   at System.Net.Http.Formatting.JsonMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, Encoding effectiveEncoding, IFormatterLogger formatterLogger)
   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Net.Http.HttpContentExtensions.<ReadAsAsyncCore>d__0`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Telegram.Bot.Api.<SendWebRequest>d__106`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at [Your Project and line code]

希望这些对你有帮助

如果您需要知道为什么会发生这种情况并可以提供更多相关信息

关于c# - 未找到单声道 MissingMethodException : DateTimeOffset. FromUnixTimeSeconds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35178178/

相关文章:

c# - Asp.Net MVC 和 ajax 异步回调执行顺序

c# - 是否可以手动修改 IQueryable 表达式

c# - 抛出 HttpException 总是发回 HTTP 500 错误?

linux - Bash 脚本和别名

c# - 从托管代码调用非托管方法

c# - Mono 编译器生成的 .exe 直接从 linux 命令行运行,为什么?

c# - System.Net.WebRequest 在 monotuch 上没有 Accept 属性

c# - 如何在没有包含元素的情况下序列化 'Any' 元素序列

linux - 如何将所有流量引导至 Linux 上的 Tor

linux - 设置命令不适用于变量值