我正在尝试使用 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/