mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2026-03-10 00:13:36 +08:00
fix: 修复 XmlSerializer 潜在的内存泄漏问题
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
@@ -9,6 +10,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
|
|||||||
{
|
{
|
||||||
internal static class XmlUtility
|
internal static class XmlUtility
|
||||||
{
|
{
|
||||||
|
// REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies
|
||||||
|
private static Hashtable _serializers = new Hashtable();
|
||||||
|
|
||||||
public static string Serialize(Type type, object obj)
|
public static string Serialize(Type type, object obj)
|
||||||
{
|
{
|
||||||
string xml;
|
string xml;
|
||||||
@@ -20,10 +24,16 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
|
|||||||
settings.WriteEndDocumentOnClose = false;
|
settings.WriteEndDocumentOnClose = false;
|
||||||
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
|
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
|
||||||
|
|
||||||
|
string skey = type.AssemblyQualifiedName;
|
||||||
|
XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey];
|
||||||
|
if (xmlSerializer == null)
|
||||||
|
{
|
||||||
|
xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
||||||
|
_serializers[skey] = xmlSerializer;
|
||||||
|
}
|
||||||
|
|
||||||
using var stream = new MemoryStream();
|
using var stream = new MemoryStream();
|
||||||
using var writer = XmlWriter.Create(stream, settings);
|
using var writer = XmlWriter.Create(stream, settings);
|
||||||
|
|
||||||
XmlSerializer xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
|
||||||
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
|
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
|
||||||
xmlNamespace.Add(string.Empty, string.Empty);
|
xmlNamespace.Add(string.Empty, string.Empty);
|
||||||
xmlSerializer.Serialize(writer, obj, xmlNamespace);
|
xmlSerializer.Serialize(writer, obj, xmlNamespace);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
@@ -9,6 +10,9 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities
|
|||||||
{
|
{
|
||||||
internal static class XmlUtility
|
internal static class XmlUtility
|
||||||
{
|
{
|
||||||
|
// REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies
|
||||||
|
private static Hashtable _serializers = new Hashtable();
|
||||||
|
|
||||||
public static string Serialize(Type type, object obj)
|
public static string Serialize(Type type, object obj)
|
||||||
{
|
{
|
||||||
string xml;
|
string xml;
|
||||||
@@ -20,10 +24,16 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities
|
|||||||
settings.WriteEndDocumentOnClose = false;
|
settings.WriteEndDocumentOnClose = false;
|
||||||
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
|
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
|
||||||
|
|
||||||
|
string skey = type.AssemblyQualifiedName;
|
||||||
|
XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey];
|
||||||
|
if (xmlSerializer == null)
|
||||||
|
{
|
||||||
|
xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
||||||
|
_serializers[skey] = xmlSerializer;
|
||||||
|
}
|
||||||
|
|
||||||
using var stream = new MemoryStream();
|
using var stream = new MemoryStream();
|
||||||
using var writer = XmlWriter.Create(stream, settings);
|
using var writer = XmlWriter.Create(stream, settings);
|
||||||
|
|
||||||
XmlSerializer xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
|
||||||
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
|
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
|
||||||
xmlNamespace.Add(string.Empty, string.Empty);
|
xmlNamespace.Add(string.Empty, string.Empty);
|
||||||
xmlSerializer.Serialize(writer, obj, xmlNamespace);
|
xmlSerializer.Serialize(writer, obj, xmlNamespace);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
@@ -9,6 +10,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
|
|||||||
{
|
{
|
||||||
internal static class XmlUtility
|
internal static class XmlUtility
|
||||||
{
|
{
|
||||||
|
// REF: https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.serialization.xmlserializer#dynamically-generated-assemblies
|
||||||
|
private static Hashtable _serializers = new Hashtable();
|
||||||
|
|
||||||
public static string Serialize(Type type, object obj)
|
public static string Serialize(Type type, object obj)
|
||||||
{
|
{
|
||||||
string xml;
|
string xml;
|
||||||
@@ -20,10 +24,16 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
|
|||||||
settings.WriteEndDocumentOnClose = false;
|
settings.WriteEndDocumentOnClose = false;
|
||||||
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
|
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
|
||||||
|
|
||||||
|
string skey = type.AssemblyQualifiedName;
|
||||||
|
XmlSerializer? xmlSerializer = (XmlSerializer)_serializers[skey];
|
||||||
|
if (xmlSerializer == null)
|
||||||
|
{
|
||||||
|
xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
||||||
|
_serializers[skey] = xmlSerializer;
|
||||||
|
}
|
||||||
|
|
||||||
using var stream = new MemoryStream();
|
using var stream = new MemoryStream();
|
||||||
using var writer = XmlWriter.Create(stream, settings);
|
using var writer = XmlWriter.Create(stream, settings);
|
||||||
|
|
||||||
XmlSerializer xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
|
||||||
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
|
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
|
||||||
xmlNamespace.Add(string.Empty, string.Empty);
|
xmlNamespace.Add(string.Empty, string.Empty);
|
||||||
xmlSerializer.Serialize(writer, obj, xmlNamespace);
|
xmlSerializer.Serialize(writer, obj, xmlNamespace);
|
||||||
|
|||||||
Reference in New Issue
Block a user