Open rranzmaier opened 5 years ago
Does the client specify an Accept: application/json
? In the absence of this or a Produces
attribute on the action, MVC will simply iterate through available formatters in the order in which they are configured and attempt to format it. There's a few ways to solve this
a) Have the client request for a specific content type i.e. application/json
b) Configure the action with a [Produces("application/json")]
attribute
c) Configure MVC to specify the JSON formatters before XML:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddNewtonsoftJson()
.AddXmlSerializerFormatters();
}
a) Have the client request for a specific content type i.e. application/json Yes b) Configure the action with a [Produces("application/json")] attribute Makes no difference c) Configure MVC to specify the JSON formatters before XML: This works better !
For performance reasons it would be better to check the required output and use the correct serializer. With the wrong order of AddNewtonsoftJson and AddXmlSerializerFormatters every application/json request creates an XmlSerializer instance try to serialize and in my example it throws and catch an exception.
@rranzmaier if the client specifies a accept-type, it seems fairly odd that it attempts to use the xml formatter. Worth a investigation.
@rranzmaier if the client specifies a accept-type, it seems fairly odd that it attempts to use the xml formatter. Worth a investigation.
Thanks. I use Postman and specified the accept-type.
GET /Test/B HTTP/1.1 Host: localhost:5001 Accept: application/json User-Agent: PostmanRuntime/7.15.0 Cache-Control: no-cache Postman-Token: 3e725c6f-4dbc-477d-b0de-4e1acb5bec1a,f147d6fa-82e6-469d-a1a0-ef0cf5b37829 Host: localhost:5001 accept-encoding: gzip, deflate Connection: keep-alive cache-control: no-cache
If you need more information let me know
Thanks for the info @rranzmaier. I'll ping you once I have had a chance to investigate.
Hello, I have the same warning when I add: services.AddControllers().AddXmlSerializerFormatters();
1) The client is sending Accept:'application/json'
2) The methods are decorated with [Produces("application/json")]
The response is OK but it appears it is trying to serialize with xmlFormatter Dependencies: Net core 3.1 Microsoft.AspNetCore.Mvc.NewtonsoftJson 3.1.1
The warning: Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter: Warning: An error occurred while trying to create an XmlSerializer for the type 'NetTopologySuite.Features.FeatureCollection'.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.
Thanks for contacting us.
We're moving this issue to the .NET 8 Planning
milestone for future evaluation / consideration. Because it's not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.
The XmlSerializer will not work for properties with private setters. Just give it a try by adding a parameterless constructor, modifying the properties setters, and removing the Produce attribute.
From the attached code of the TestController class, the Produce attribute will try to return only those mentioned types.
Are there any updates regarding this issue? Has the problem been resolved?
Same issue here. Upon initialization, the application is trying to create a XmlSerializer for each method, even those decorated with [Produces("application/json")]
attribute
[21:14:30 INF] Now listening on: http://[::]:9150
[21:14:31 WRN] An error occurred while trying to create an XmlSerializer for the type 'DFeTech.Resources.FilesResource'.
System.InvalidOperationException: There was an error reflecting type 'DFeTech.Resources.FilesResource'.
---> System.InvalidOperationException: Cannot serialize member 'DFeTech.Resources.FilesResource.Files' of type 'System.Collections.Generic.List`1[[DFeTech.Resources.FileResource, NFEio.ProductInvoice, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details.
---> System.InvalidOperationException: DFeTech.Resources.FileResource cannot be serialized because it does not have a parameterless constructor.
--- End of inner exception stack trace ---
at System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type)
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.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 Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter.CreateSerializer(Type type)
[21:14:31 WRN] An error occurred while trying to create an XmlSerializer for the type 'Microsoft.AspNetCore.Mvc.FileStreamResult'.
System.InvalidOperationException: Microsoft.AspNetCore.Mvc.FileStreamResult cannot be serialized because it does not have a parameterless constructor.
at System.Xml.Serialization.TypeDesc.CheckSupported()
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter.CreateSerializer(Type type)
[21:14:31 INF] Application started. Press Ctrl+C to shut down.
[21:14:31 INF] Hosting environment: Development
...
Describe the bug
I write an ASP.net Core 3 WebAPI. For some reasons I need JSON and in some cases XML output.
I noticed that aspnetcore also created an instance for xml in the responses for json. Most of my Models have no Parameterless constructor and therefore not suitable for xml.
To Reproduce
Steps to reproduce the behavior:
startup.cs
Add a Controller
Start and browse to https://localhost:5001/test/B The Response is ok it's Json and correct but the console show this
Expected behavior
In my opinion it's not necessary to create an XmlSerializer instance for json output
Screenshots
Additional context
Include the output of
dotnet --info
.NET Core SDK (reflecting any global.json): Version: 3.0.100-preview7-012821 Commit: 6348f1068a
Runtime Environment: OS Name: Windows OS Version: 10.0.18362 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.0.100-preview7-012821\
Host (useful for support): Version: 3.0.0-preview7-27912-14 Commit: 4da6ee6450
.NET Core SDKs installed: 2.1.500 [C:\Program Files\dotnet\sdk] 2.1.502 [C:\Program Files\dotnet\sdk] 2.1.503 [C:\Program Files\dotnet\sdk] 2.1.504 [C:\Program Files\dotnet\sdk] 2.1.505 [C:\Program Files\dotnet\sdk] 2.1.507 [C:\Program Files\dotnet\sdk] 2.1.508 [C:\Program Files\dotnet\sdk] 2.1.602 [C:\Program Files\dotnet\sdk] 2.1.700 [C:\Program Files\dotnet\sdk] 2.1.701 [C:\Program Files\dotnet\sdk] 2.1.800-preview-009677 [C:\Program Files\dotnet\sdk] 2.1.800-preview-009696 [C:\Program Files\dotnet\sdk] 2.1.800 [C:\Program Files\dotnet\sdk] 2.1.801 [C:\Program Files\dotnet\sdk] 2.2.103 [C:\Program Files\dotnet\sdk] 3.0.100-preview7-012821 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0-preview7.19365.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0-preview7-27912-14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.0-preview7-27912-14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]