简体   繁体   English

XmlSerializerInputFormatter 已过时 - ASP.NET Core 2.1

[英]XmlSerializerInputFormatter is obsolete - ASP.NET Core 2.1

I am using the following to accept XML serialized in my Core API App.我正在使用以下内容来接受在我的核心 API 应用程序中序列化的 XML。

services.AddMvc(options =>
{
    // allow xml format for input
    options.InputFormatters.Add(new XmlSerializerInputFormatter());
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

After updating to ASP.NET Core 2.1 I receive the following warning:更新到 ASP.NET Core 2.1 后,我收到以下警告:

'XmlSerializerInputFormatter.XmlSerializerInputFormatter()' is obsolete: 'This constructor is obsolete and will be removed in a future version.' 'XmlSerializerInputFormatter.XmlSerializerInputFormatter()' 已过时:'此构造函数已过时,将在未来版本中删除。

What is the new way to handle this?处理这个问题的新方法是什么?

According to the source code , there's a constructor that has not been marked as Obsolete :根据源代码,有一个构造函数没有被标记为Obsolete

public XmlSerializerInputFormatter(MvcOptions options)

This constructor takes an instance of MvcOptions , so you can pass through your existing options argument:此构造函数采用MvcOptions的实例,因此您可以传递现有的options参数:

services.AddMvc(options =>
{
    // allow xml format for input
    options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
}) ...

As of ASP.NET Core 3.0, this constructor is the only one available.从 ASP.NET Core 3.0 开始,此构造函数是唯一可用的构造函数。 Those that were marked obsolete have now been removed.那些被标记为过时的现在已被删除。

With .NET Core 2.2 or later XmlSerializerInputFormatter should be marked as deprecated.对于 .NET Core 2.2 或更高版本,XmlSerializerInputFormatter 应标记为已弃用。

Instead a of explicitly defining XML serializers as we did before, in the .NET Core 2.2 we can add them simply by calling AddXmlSerializerFormatters() method which will do the job now.在 .NET Core 2.2 中,我们可以简单地通过调用 AddXmlSerializerFormatters() 方法来添加它们,而不是像我们之前那样显式定义 XML 序列化器,该方法现在可以完成这项工作。 Read here why it has been deprecated在这里阅读为什么它已被弃用

Here is how you can do it.这是您如何做到的。

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(config =>
    {
        config.RespectBrowserAcceptHeader = true;
        config.ReturnHttpNotAcceptable = true;

        config.OutputFormatters.Add(new CsvOutputFormatter());
    }).AddXmlSerializerFormatters().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM