首页 > 解决方案 > 在 .NET Core 3.1 中读取 RSS 不起作用

问题描述

我有非常简单的 .NET Core Web Api 代码,以前在 .NET Core 2.2 中可以使用,但在升级到 3.1 后停止工作

[HttpGet]
[Route("api/news")]
public IEnumerable<SyndicationItem> GetNews(string source)
{
    var feed = SyndicationFeed.Load(XmlReader.Create(source));
    return feed.Items;
}

我可以通过导航到:https://localhost:5001/api/news?source=http://feeds.bbci.co.uk/news/rss.xml来调用它

在调试中,我可以看到它可以很好地获取项目,但是返回它们时抛出此错误,我不明白为什么在升级到 3.1 后它停止工作以及如何修复它

请指教,谢谢!

在此处输入图像描述 在此处输入图像描述

System.NotSupportedException:不支持“System.ServiceModel.Syndication.SyndicationItem.AttributeExtensions”上的集合类型“System.Collections.Generic.Dictionary`2[System.Xml.XmlQualifiedName,System.String]”。在 System.Text.Json.JsonClassInfo.GetElementType(类型 propertyType,类型 parentType,MemberInfo memberInfo,JsonSerializerOptions 选项)在 System.Text.Json.JsonClassInfo.CreateProperty(类型声明PropertyType,类型 runtimePropertyType,类型已实现PropertyType,PropertyInfo propertyInfo,类型 parentClassType,JsonConverter转换器, JsonSerializerOptions 选项) 在 System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options) 在 System.Text.Json.JsonClassInfo..ctor(Type type,

标签: .net-corersssyndication-feed

解决方案


.NET Core 3.x 中的默认JSON 序列化程序更改为System.Text.Json. 在这种情况下,您可能需要指定Newtonsoft.Json用作默认序列化程序(旧版本 .NET Core 中的默认序列化程序)。

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddNewtonsoftJson();

    // other settings in ConfigureServices...
}

推荐阅读