javascript
Teams Bot库的JSON
如果你和我一樣,一直使用最新的 asp.net core 來開發teams bot的應用,那么你就會發現當你使用最新的 LTS 3.1版本或者 5.0 版本或者最新的 6.0 版本,asp.net core 默認使用System.Text.Json庫,所以當你在處理 Teams 發送來的請求的時候,如果還是使用類似于下面的代碼,那很多時候就是發現activity解析不出來,返回的response也不對。
[HttpPost("api/messages")] public Activity GetMessage([FromBody]Activity activity) {....return new Activity(){....}; }為什么呢?從 Microsoft.Bot.Schema 的源代碼里的?Activity 定義?里可以發現原因:
using System.Collections.Generic; using Newtonsoft.Json;namespace Microsoft.Bot.Schema {public partial class Activity{[JsonProperty(PropertyName = "type")]public string Type { get; set; }[JsonProperty(PropertyName = "id")]public string Id { get; set; }[JsonProperty(PropertyName = "timestamp")]public System.DateTimeOffset? Timestamp { get; set; }[JsonProperty(PropertyName = "localTimestamp")]public System.DateTimeOffset? LocalTimestamp { get; set; }[JsonProperty(PropertyName = "localTimezone")]public string LocalTimezone { get; set; }[JsonProperty(PropertyName = "serviceUrl")]public string ServiceUrl { get; set; }[JsonProperty(PropertyName = "channelId")]public string ChannelId { get; set; }[JsonProperty(PropertyName = "from")]public ChannelAccount From { get; set; }...} }從上面的代碼可以看到,Activity 使用了 Newtonsoft.JSON 的 JsonProperty attribute 來說明如何 serialize 和 deserialize。既然是 Newtonsoft.JSON 的 attribute,那肯定對 System.Text.Json 不起作用了,就是說默認的 asp.net core 的 json 不會去讀取 JsonProperty attribute。除了 JsonProperty,Microsoft.Bot.Schema和其他bot的庫也使用了很多其他 Newtonsoft.JSON 的東西。
現在相信大家知道了原因,那如何解決?有這么幾種方法:
然后在Startup文件中:
services.AddMvc().AddNewtonsoftJson();或者
services.AddControllers().AddNewtonsoftJson(); services.AddControllersWithViews().AddNewtonsoftJson(); services.AddRazorPages().AddNewtonsoftJson();上面的 JsonPropertyName 是 System.Text.Json 里的一種 attribute。
不過,最終的解決方案,肯定是微軟官方的這些 Bot,Teams 的library可以支持最新的 System.Text.Json,希望這天可以快點到來。
總結
以上是生活随笔為你收集整理的Teams Bot库的JSON的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Teams App如何选择用户
- 下一篇: dev万能头文件_【C++】Dev-C+