【问题标题】:Reading RSS in .NET Core 3.1 is not working在 .NET Core 3.1 中读取 RSS 不起作用
【发布时间】:2025-11-30 17:55:01
【问题描述】:

我有非常简单的 .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(类型 propertyType,PropertyInfo propertyInfo,类型 classType,JsonSerializerOptions 选项) 在 System.Text.Json.JsonClassInfo..ctor(类型类型,JsonSerializerOptions 选项) 在 System.Text.Json.JsonSerializerOptions.GetOrAddClass(类型 classType) 在 System.Text.Json.JsonClassInfo.get_ElementClassInfo() 在 System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer,Int32 originalWriterDepth,Int32 flushThreshold,JsonSerializerOptions 选项,WriteStack& 状态) 在 System.Text.Json.JsonSerializer.WriteAsyncCore(流 utf8Json,对象值,类型 inputType,JsonSerializerOptions 选项,CancellationToken 取消令牌) 在 Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext 上下文,编码 selectedEncoding) 在 Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext 上下文,编码 selectedEncoding) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker 调用者,Task lastTask,State next,Scope 范围,Object state,Boolean isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed 上下文) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() --- 从之前抛出异常的位置结束堆栈跟踪 --- 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker 调用程序,任务 lastTask,下一个状态,作用域范围,对象状态,布尔 isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed 上下文) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(状态&下一个,范围&范围,对象&状态,布尔& isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() --- 从之前抛出异常的位置结束堆栈跟踪 --- 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker 调用程序,任务任务,IDisposable 范围) 在 Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(端点端点,任务 requestTask,ILogger 记录器) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)

【问题讨论】:

  • 您的错误表明您在使用 ASP.NET Core 3.x 中内置的 System.Text.Json API 时遇到问题。尝试切换回Newtonsoft.Json
  • 我从未明确使用过 System.Text.Json 或 Newtonsoft.Json,这两行代码就是我所拥有的,我无法切换回来,因为我从未切换过 :)
  • System.Text.Json 是 ASP.NET Core 3.x 中的默认 JSON 序列化程序 Overview 文章告诉你更多
  • 我知道它是什么 :) 我说过我从未在我的代码中明确使用过它。您在我的帖子中看到这两行代码吗?就是这样。

标签: .net-core rss syndication-feed


【解决方案1】:

.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...
}

【讨论】: