XmlSerializer Exceptions on start up when debugging after Upgrading Visual Studio 2022 to v17.9.0

Hatoum,Mark 5 Reputation points
2024-03-01T19:59:38.64+00:00

After upgrading Visual Studio 2022 to version 17.9.0, xml serialization exceptions are displayed on the console and output windows when starting to debug a .NET 6 Web API solution. The prior versions of Visual Studio 2022 do not have the issue with the same solution.

Here is an example of one of the exception stacks:

warn: Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter[1]

  *An error occurred while trying to create an XmlSerializer for the type '<xxxxxxxxx.xxxxxxx.xxxxxxxxxxx.xxxxxx>'.*

  *System.InvalidOperationException: There was an error reflecting type '<xxxxxxxxx.xxxxxxx.xxxxxxxxxxx.xxxxxx>'.*

   *---> System.InvalidOperationException: There was an error reflecting property 'bbbbbb'.*

   *---> System.InvalidOperationException: There was an error reflecting type '<yyyyyyyyyy.yyyyy.yyyyyyy.yyy>'.*

   *---> System.NotSupportedException: Cannot serialize member <yyyyyyyyyy.yyyyy.yyyyyyy.yyy> of type System.Collections.Concurrent.ConcurrentDictionary`2[[<zzzzzzzzzzzzzzz.zzzzzzzzzz.zzzzz.zzzzzzzzzzzzz>, <zzzzzzzzzzzzzzz>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Collections.Generic.List`1[[<aaaaaaaaaaaaaa.aaaaaaa.aaa.aaaaaaaaa>, <zzzzzzzzzzzzzzz>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], because it implements IDictionary.*

     *at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String memberInfo)*

     *at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)*

     *at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)*

     *at System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo)*

     *at System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo)*

     *at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)*

     *--- End of inner exception stack trace ---*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter)*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns, RecursionLimiter limiter)*

     *at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)*

     *--- End of inner exception stack trace ---*

     *at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)*

     *--- End of inner exception stack trace ---*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)*

     *at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)*

     *at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)*

     *at System.Xml.Serialization.XmlSerializer..ctor(Type type)*

     *at Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter.CreateSerializer(Type type)*
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,390 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,618 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Tianyu Sun-MSFT 27,356 Reputation points Microsoft Vendor
    2024-03-04T06:26:02.8266667+00:00

    Hello @Hatoum,Mark,

    Thank you for taking time to post this issue in Microsoft Q&A forum.

    Please try to rename/remove the hidden .vs folder in the solution folder, clean the project cache and rebuild your solution.

    I noticed that there were some similar or related issues about Web API project in VS version 17.9.0 but fixed in version 17.9.2. If it’s possible I would recommend trying to update Visual Studio 2022 to version 17.9.2. Rolling back Visual Studio(Visual Studio Installer > More > Rollback to previous version) may be another temporarily workaround.

    BTW, if this issue can be reproduced on a newly created project, please kindly share us detailed steps to reproduce this issue and check further.

    Kind Regards,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.