Skip to content

Commit e1418ec

Browse files
committed
Cleanup EFXmlRepo
1 parent bee3aee commit e1418ec

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

src/DataProtection/DataProtection/ref/Microsoft.AspNetCore.DataProtection.netcoreapp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public virtual void StoreElement(System.Xml.Linq.XElement element, string friend
323323
}
324324
public partial interface IXmlRepository
325325
{
326-
System.Collections.Generic.IReadOnlyCollection<System.Xml.Linq.XElement?> GetAllElements();
326+
System.Collections.Generic.IReadOnlyCollection<System.Xml.Linq.XElement> GetAllElements();
327327
void StoreElement(System.Xml.Linq.XElement element, string friendlyName);
328328
}
329329
public partial class RegistryXmlRepository : Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository

src/DataProtection/DataProtection/ref/Microsoft.AspNetCore.DataProtection.netstandard2.0.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public virtual void StoreElement(System.Xml.Linq.XElement element, string friend
323323
}
324324
public partial interface IXmlRepository
325325
{
326-
System.Collections.Generic.IReadOnlyCollection<System.Xml.Linq.XElement?> GetAllElements();
326+
System.Collections.Generic.IReadOnlyCollection<System.Xml.Linq.XElement> GetAllElements();
327327
void StoreElement(System.Xml.Linq.XElement element, string friendlyName);
328328
}
329329
public partial class RegistryXmlRepository : Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository

src/DataProtection/DataProtection/src/Repositories/IXmlRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface IXmlRepository
1818
/// <remarks>
1919
/// All top-level elements in the repository.
2020
/// </remarks>
21-
IReadOnlyCollection<XElement?> GetAllElements();
21+
IReadOnlyCollection<XElement> GetAllElements();
2222

2323
/// <summary>
2424
/// Adds a top-level XML element to the repository.

src/DataProtection/EntityFrameworkCore/src/EntityFrameworkCoreXmlRepository.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,27 @@ public EntityFrameworkCoreXmlRepository(IServiceProvider services, ILoggerFactor
3838
}
3939

4040
/// <inheritdoc />
41-
public virtual IReadOnlyCollection<XElement?> GetAllElements()
41+
public virtual IReadOnlyCollection<XElement> GetAllElements()
4242
{
43-
using (var scope = _services.CreateScope())
43+
// forces complete enumeration
44+
return GetAllElementsCore().ToList().AsReadOnly();
45+
46+
IEnumerable<XElement> GetAllElementsCore()
4447
{
45-
var context = scope.ServiceProvider.GetRequiredService<TContext>();
48+
using (var scope = _services.CreateScope())
49+
{
50+
var context = scope.ServiceProvider.GetRequiredService<TContext>();
51+
52+
foreach (var key in context.DataProtectionKeys.AsNoTracking())
53+
{
54+
_logger.ReadingXmlFromKey(key.FriendlyName!, key.Xml);
4655

47-
// Put logger in a local such that `this` isn't captured.
48-
var logger = _logger;
49-
return context.DataProtectionKeys.AsNoTracking().Select(key => TryParseKeyXml(key.Xml!, logger)).ToList().AsReadOnly();
56+
if (!string.IsNullOrEmpty(key.Xml))
57+
{
58+
yield return XElement.Parse(key.Xml);
59+
}
60+
}
61+
}
5062
}
5163
}
5264

@@ -67,18 +79,5 @@ public void StoreElement(XElement element, string friendlyName)
6779
context.SaveChanges();
6880
}
6981
}
70-
71-
private static XElement? TryParseKeyXml(string xml, ILogger logger)
72-
{
73-
try
74-
{
75-
return XElement.Parse(xml);
76-
}
77-
catch (Exception e)
78-
{
79-
logger?.LogExceptionWhileParsingKeyXml(xml, e);
80-
return null;
81-
}
82-
}
8382
}
8483
}

src/DataProtection/EntityFrameworkCore/src/LoggingExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ namespace Microsoft.Extensions.Logging
77
{
88
internal static class LoggingExtensions
99
{
10-
private static readonly Action<ILogger, string, Exception?> _anExceptionOccurredWhileParsingKeyXml;
10+
private static readonly Action<ILogger, string?, string?, Exception?> _readingXmlFromKey;
1111
private static readonly Action<ILogger, string, string, Exception?> _savingKeyToDbContext;
1212

1313
static LoggingExtensions()
1414
{
15-
_anExceptionOccurredWhileParsingKeyXml = LoggerMessage.Define<string>(
16-
eventId: new EventId(1, "ExceptionOccurredWhileParsingKeyXml"),
17-
logLevel: LogLevel.Warning,
18-
formatString: "An exception occurred while parsing the key xml '{Xml}'.");
15+
_readingXmlFromKey = LoggerMessage.Define<string?, string?>(
16+
eventId: new EventId(1, "ReadKeyFromElement"),
17+
logLevel: LogLevel.Debug,
18+
formatString: "Reading data with key '{FriendlyName}', value '{Value}'.");
1919
_savingKeyToDbContext = LoggerMessage.Define<string, string>(
2020
eventId: new EventId(2, "SavingKeyToDbContext"),
2121
logLevel: LogLevel.Debug,
2222
formatString: "Saving key '{FriendlyName}' to '{DbContext}'.");
2323
}
2424

25-
public static void LogExceptionWhileParsingKeyXml(this ILogger logger, string keyXml, Exception exception)
26-
=> _anExceptionOccurredWhileParsingKeyXml(logger, keyXml, exception);
25+
public static void ReadingXmlFromKey(this ILogger logger, string? friendlyName, string? keyXml)
26+
=> _readingXmlFromKey(logger, friendlyName, keyXml, null);
2727

2828
public static void LogSavingKeyToDbContext(this ILogger logger, string friendlyName, string contextName)
2929
=> _savingKeyToDbContext(logger, friendlyName, contextName, null);

0 commit comments

Comments
 (0)