簡體   English   中英

在 .NET Core 3.1 中讀取 RSS 不起作用

[英]Reading RSS in .NET Core 3.1 is not working

我有非常簡單的 .NET Core Web Api 代碼之前在 Z303CB0EF9EDB9082D979 升級到 Core Z303CB0EF9EDB9082D61BBBE5825 后工作的代碼停止工作。

[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]”。 at System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonConverter系統轉換器,JsonSerializerOptions 選項)在 System.Text.Json.JsonClassInfo.AddProperty(類型 propertyType,PropertyInfo propertyInfo,類型 classType,JsonSerializerOptions 選項)在 System.Text.Json.SonTypeInfosOptions 選項。 .Json.JsonSerializerOptions.GetOrAddClass(Type classType) at System.Text.Json.JsonClassInfo.get_ElementClassInfo() at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken) at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker 調用程序,Task lastTask, State next, Scope scope, Object state, Boolean isCompleted ) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft. AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() --- 從先前引發異常的位置結束堆棧跟蹤 --- 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker 調用程序,任務 lastTask,State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State&接下來,Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineCoreAsync() --- 堆棧跟蹤的結束位置從之前拋出異常的位置 Microsoft.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語境)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM