WebAPI try to create not nessesary XmlSerializer instance
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### 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:
1. Using this version of ASP.NET Core '...' **3.0.100-preview7-012821**
2. Run this code '....'
Use Attached Sample or
Create an ASP.NET Core WebAPI
Add nuget
Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview8.19405.7"
Newtonsoft.Json" Version="12.0.2"
[WebApiTest.zip](https://github.com/aspnet/AspNetCore/files/3501119/WebApiTest.zip)
startup.cs
```
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddXmlSerializerFormatters()
.AddNewtonsoftJson();
}
```
Add a Controller
```
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace WebAPITest.Controllers
{
[ApiController]
[Route("[controller]")]
[Produces("application/json")]
public class TestController : ControllerBase
{
private readonly ILogger _logger;
public TestController(ILogger logger)
{
_logger = logger;
}
[HttpGet("A")]
public ActionResult GetA()
{
return Ok(new TestValue() {Id = 1,Text="Test A"});
}
[HttpGet("B")]
public ActionResult GetB()
{
return Ok(new TestValue2(1, "This call produce the hidden error"));
}
}
public class TestValue
{
public int Id { get; set; }
public string Text { get; set; }
}
public class TestValue2
{
public TestValue2(int id, string text)
{
Id = id;
Text = text;
}
[JsonProperty("id")]
public int Id { get; }
[JsonProperty("txt")]
public string Text { get; }
}
}
```
Start and browse to
https://localhost:5001/test/B
The Response is ok it's Json and correct but the console show this
> Request starting HTTP/2 GET https://localhost:5001/test/B
> info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
> Executing endpoint 'WebAPITest.Controllers.TestController.GetB (WebAPITest)'
> info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
> Route matched with {action = "GetB", controller = "Test"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.ActionResult`1[WebAPITest.Controllers.TestValue] GetB() on controller WebAPITest.Controllers.TestController (WebAPITest).
> info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[1]
> Executing action method WebAPITest.Controllers.TestController.GetB (WebAPITest) - Validation state: Valid
> info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
> Executed action method WebAPITest.Controllers.TestController.GetB (WebAPITest), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 0.6145ms.
> warn: Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter[1]
> An error occurred while trying to create an XmlSerializer for the type 'WebAPITest.Controllers.TestValue2'.
> System.InvalidOperationException: WebAPITest.Controllers.TestValue2 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 System.Xml.Serialization.XmlSerializer..ctor(Type type)
> at Microsoft.AspNetCore.Mvc.Formatters.XmlSerializerOutputFormatter.CreateSerializer(Type type)
> info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
> Executing ObjectResult, writing value of type 'WebAPITest.Controllers.TestValue2'.
> info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
> Executed action WebAPITest.Controllers.TestController.GetB (WebAPITest) in 697.043ms
> info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
> Executed endpoint 'WebAPITest.Controllers.TestController.GetB (WebAPITest)'
> info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
> Request finished in 791.3005ms 200 application/json; charset=utf-8
### 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]
Contributor guide
Assessment
This issue has not been assessed yet.