Skip to content

Commit

Permalink
.NET 7.0 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPaskhin committed Mar 5, 2023
1 parent b9fd3ff commit 8f444a3
Show file tree
Hide file tree
Showing 42 changed files with 1,857 additions and 2,064 deletions.
10 changes: 0 additions & 10 deletions Mvc.Formatters.Xml.Extensions/NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@
<configuration>
<packageSources>
<clear />
<add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
<add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
<add key="dotnet5-transport" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5-transport/nuget/v3/index.json" />
<add key="dotnet6" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json" />
<add key="dotnet6-transport" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6-transport/nuget/v3/index.json" />
<add key="roslyn" value="https://dotnet.myget.org/F/roslyn/api/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="roslyn-tools" value="https://dotnet.myget.org/F/roslyn-tools/api/v3/index.json" />
<!-- Used for the SiteExtension 3.1 bits that are included in the 5.0 build -->
<add key="dotnet31-transport" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet3.1-transport/nuget/v3/index.json" />
</packageSources>
<disabledPackageSources />
</configuration>
4 changes: 2 additions & 2 deletions Mvc.Formatters.Xml.Extensions/build/dependencies.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<DotNetCoreAppTarget>net6.0</DotNetCoreAppTarget>
<DotNetCoreAppTarget>net7.0</DotNetCoreAppTarget>
<DotNetCoreStandardTarget>netstandard2.1</DotNetCoreStandardTarget>
<XmlResultVersion>6.0.0</XmlResultVersion>
<XmlResultVersion>7.0.0</XmlResultVersion>
</PropertyGroup>

</Project>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,41 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;

namespace Microsoft.Extensions.DependencyInjection
namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Extension methods for adding Xml formatters to MVC.
/// </summary>
public static class MvcXmlMvcBuilderExtensions
{
/// <summary>
/// Extension methods for adding Xml formatters to MVC.
/// Adds the XmlSerializer and DataContractSerializer formatters to MVC.
/// Adds the XmlResult and FromXmlBody Extension to MVC.
/// </summary>
public static class MvcXmlMvcBuilderExtensions
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddXmlFormaterExtensions(this IMvcBuilder builder)
{
/// <summary>
/// Adds the XmlSerializer and DataContractSerializer formatters to MVC.
/// Adds the XmlResult and FromXmlBody Extension to MVC.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddXmlFormaterExtensions(this IMvcBuilder builder)
if (builder == null)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.AddXmlDataContractSerializerFormatters();
builder.AddXmlSerializerFormatters();
AddXmlFormaterExtensionsServices(builder.Services);
return builder;
throw new ArgumentNullException(nameof(builder));
}
builder.AddXmlDataContractSerializerFormatters();
builder.AddXmlSerializerFormatters();
AddXmlFormaterExtensionsServices(builder.Services);
return builder;
}

// Internal for testing.
internal static void AddXmlFormaterExtensionsServices(IServiceCollection services)
{
services.TryAddSingleton<XmlDcResultExecutor>();
services.TryAddTransient<DcXmlBodyModelBinder>();
services.TryAddTransient<DcXmlBodyModelBinderOnly>();
// Internal for testing.
internal static void AddXmlFormaterExtensionsServices(IServiceCollection services)
{

services.TryAddSingleton<XmlDcResultExecutor>();
services.TryAddTransient<DcXmlBodyModelBinder>();
services.TryAddTransient<DcXmlBodyModelBinderOnly>();

services.TryAddSingleton<XmlResultExecutor>();
services.TryAddTransient<XmlBodyModelBinder>();
services.TryAddTransient<XmlBodyModelBinderOnly>();
}
services.TryAddSingleton<XmlResultExecutor>();
services.TryAddTransient<XmlBodyModelBinder>();
services.TryAddTransient<XmlBodyModelBinderOnly>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,42 @@
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.Extensions.DependencyInjection
namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Extension methods for adding Xml formatters to MVC.
/// </summary>
public static class MvcXmlMvcCoreBuilderExtensions
{
/// <summary>
/// Extension methods for adding Xml formatters to MVC.
/// Adds the XmlSerializer and DataContractSerializer formatters to MVC.
/// Adds the XmlResult and FromXmlBody Extension to MVC.
/// </summary>
public static class MvcXmlMvcCoreBuilderExtensions
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
public static IMvcCoreBuilder AddXmlFormaterExtensions(this IMvcCoreBuilder builder)
{
/// <summary>
/// Adds the XmlSerializer and DataContractSerializer formatters to MVC.
/// Adds the XmlResult and FromXmlBody Extension to MVC.
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
public static IMvcCoreBuilder AddXmlFormaterExtensions(this IMvcCoreBuilder builder)
if (builder == null)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
throw new ArgumentNullException(nameof(builder));
}

builder.AddXmlDataContractSerializerFormatters();
builder.AddXmlSerializerFormatters();
AddXmlFormaterExtensionsServices(builder.Services);
builder.AddXmlDataContractSerializerFormatters();
builder.AddXmlSerializerFormatters();
AddXmlFormaterExtensionsServices(builder.Services);

return builder;
}
return builder;
}

// Internal for testing.
internal static void AddXmlFormaterExtensionsServices(IServiceCollection services)
{
services.TryAddSingleton<XmlDcResultExecutor>();
services.TryAddTransient<DcXmlBodyModelBinder>();
services.TryAddTransient<DcXmlBodyModelBinderOnly>();
// Internal for testing.
internal static void AddXmlFormaterExtensionsServices(IServiceCollection services)
{
services.TryAddSingleton<XmlDcResultExecutor>();
services.TryAddTransient<DcXmlBodyModelBinder>();
services.TryAddTransient<DcXmlBodyModelBinderOnly>();

services.TryAddSingleton<XmlResultExecutor>();
services.TryAddTransient<XmlBodyModelBinder>();
services.TryAddTransient<XmlBodyModelBinderOnly>();
}
services.TryAddSingleton<XmlResultExecutor>();
services.TryAddTransient<XmlBodyModelBinder>();
services.TryAddTransient<XmlBodyModelBinderOnly>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,43 @@
using System.Runtime.Serialization;
using System.Xml;

namespace Microsoft.AspNetCore.Mvc.Formatters.Xml
namespace Microsoft.AspNetCore.Mvc.Formatters.Xml;

/// <summary>
/// Contains methods which are used by Xml input formatters.
/// </summary>
internal static class FormattingUtilities
{
public const int DefaultMaxDepth = 32;
public static readonly XsdDataContractExporter XsdDataContractExporter = new XsdDataContractExporter();

/// <summary>
/// Contains methods which are used by Xml input formatters.
/// Gets the default Reader Quotas for XmlReader.
/// </summary>
internal static class FormattingUtilities
/// <returns>XmlReaderQuotas with default values</returns>
public static XmlDictionaryReaderQuotas GetDefaultXmlReaderQuotas()
{
public const int DefaultMaxDepth = 32;
public static readonly XsdDataContractExporter XsdDataContractExporter = new XsdDataContractExporter();

/// <summary>
/// Gets the default Reader Quotas for XmlReader.
/// </summary>
/// <returns>XmlReaderQuotas with default values</returns>
public static XmlDictionaryReaderQuotas GetDefaultXmlReaderQuotas()
return new XmlDictionaryReaderQuotas()
{
return new XmlDictionaryReaderQuotas()
{
MaxArrayLength = int.MaxValue,
MaxBytesPerRead = int.MaxValue,
MaxDepth = DefaultMaxDepth,
MaxNameTableCharCount = int.MaxValue,
MaxStringContentLength = int.MaxValue
};
}
MaxArrayLength = int.MaxValue,
MaxBytesPerRead = int.MaxValue,
MaxDepth = DefaultMaxDepth,
MaxNameTableCharCount = int.MaxValue,
MaxStringContentLength = int.MaxValue
};
}

/// <summary>
/// Gets the default XmlWriterSettings.
/// </summary>
/// <returns>Default <see cref="XmlWriterSettings"/></returns>
public static XmlWriterSettings GetDefaultXmlWriterSettings()
/// <summary>
/// Gets the default XmlWriterSettings.
/// </summary>
/// <returns>Default <see cref="XmlWriterSettings"/></returns>
public static XmlWriterSettings GetDefaultXmlWriterSettings()
{
return new XmlWriterSettings
{
return new XmlWriterSettings
{
OmitXmlDeclaration = true,
CloseOutput = false,
CheckCharacters = false
};
}
OmitXmlDeclaration = true,
CloseOutput = false,
CheckCharacters = false
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,72 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Internal
namespace Microsoft.AspNetCore.Internal;

internal static class ResponseContentTypeHelper
{
internal static class ResponseContentTypeHelper
/// <summary>
/// Gets the content type and encoding that need to be used for the response.
/// The priority for selecting the content type is:
/// 1. ContentType property set on the action result
/// 2. <see cref="HttpResponse.ContentType"/> property set on <see cref="HttpResponse"/>
/// 3. Default content type set on the action result
/// </summary>
/// <remarks>
/// The user supplied content type is not modified and is used as is. For example, if user
/// sets the content type to be "text/plain" without any encoding, then the default content type's
/// encoding is used to write the response and the ContentType header is set to be "text/plain" without any
/// "charset" information.
/// </remarks>
public static void ResolveContentTypeAndEncoding(
string? actionResultContentType,
string? httpResponseContentType,
(string defaultContentType, Encoding defaultEncoding) @default,
Func<string, Encoding?> getEncoding,
out string resolvedContentType,
out Encoding resolvedContentTypeEncoding)
{
/// <summary>
/// Gets the content type and encoding that need to be used for the response.
/// The priority for selecting the content type is:
/// 1. ContentType property set on the action result
/// 2. <see cref="HttpResponse.ContentType"/> property set on <see cref="HttpResponse"/>
/// 3. Default content type set on the action result
/// </summary>
/// <remarks>
/// The user supplied content type is not modified and is used as is. For example, if user
/// sets the content type to be "text/plain" without any encoding, then the default content type's
/// encoding is used to write the response and the ContentType header is set to be "text/plain" without any
/// "charset" information.
/// </remarks>
public static void ResolveContentTypeAndEncoding(
string actionResultContentType,
string httpResponseContentType,
(string defaultContentType, Encoding defaultEncoding) @default,
Func<string, Encoding> getEncoding,
out string resolvedContentType,
out Encoding resolvedContentTypeEncoding)
var (defaultContentType, defaultContentTypeEncoding) = @default;

// 1. User sets the ContentType property on the action result
if (actionResultContentType != null)
{
var (defaultContentType, defaultContentTypeEncoding) = @default;
resolvedContentType = actionResultContentType;
var actionResultEncoding = getEncoding(actionResultContentType);
resolvedContentTypeEncoding = actionResultEncoding ?? defaultContentTypeEncoding;
return;
}

// 1. User sets the ContentType property on the action result
if (actionResultContentType != null)
// 2. User sets the ContentType property on the http response directly
if (!string.IsNullOrEmpty(httpResponseContentType))
{
var mediaTypeEncoding = getEncoding(httpResponseContentType);
if (mediaTypeEncoding != null)
{
resolvedContentType = actionResultContentType;
var actionResultEncoding = getEncoding(actionResultContentType);
resolvedContentTypeEncoding = actionResultEncoding ?? defaultContentTypeEncoding;
return;
resolvedContentType = httpResponseContentType;
resolvedContentTypeEncoding = mediaTypeEncoding;
}

// 2. User sets the ContentType property on the http response directly
if (!string.IsNullOrEmpty(httpResponseContentType))
else
{
var mediaTypeEncoding = getEncoding(httpResponseContentType);
if (mediaTypeEncoding != null)
{
resolvedContentType = httpResponseContentType;
resolvedContentTypeEncoding = mediaTypeEncoding;
}
else
{
resolvedContentType = httpResponseContentType;
resolvedContentTypeEncoding = defaultContentTypeEncoding;
}

return;
resolvedContentType = httpResponseContentType;
resolvedContentTypeEncoding = defaultContentTypeEncoding;
}

// 3. Fall-back to the default content type
resolvedContentType = defaultContentType;
resolvedContentTypeEncoding = defaultContentTypeEncoding;
return;
}

public static Encoding GetEncoding(string mediaType)
{
if (MediaTypeHeaderValue.TryParse(mediaType, out var parsed))
{
return parsed.Encoding;
}
// 3. Fall-back to the default content type
resolvedContentType = defaultContentType;
resolvedContentTypeEncoding = defaultContentTypeEncoding;
}

return default;
public static Encoding? GetEncoding(string mediaType)
{
if (MediaTypeHeaderValue.TryParse(mediaType, out var parsed))
{
return parsed.Encoding;
}

return default;
}
}
Loading

0 comments on commit 8f444a3

Please sign in to comment.