diff --git a/src/ImageSharp/Formats/ImageFormatManager.cs b/src/ImageSharp/Formats/ImageFormatManager.cs
index 63fd02d8d..e62805d47 100644
--- a/src/ImageSharp/Formats/ImageFormatManager.cs
+++ b/src/ImageSharp/Formats/ImageFormatManager.cs
@@ -13,6 +13,12 @@ namespace SixLabors.ImageSharp.Formats
///
public class ImageFormatManager
{
+ ///
+ /// Used for locking against as there is no ConcurrentSet type.
+ ///
+ ///
+ private static readonly object HashLock = new object();
+
///
/// The list of supported keyed to mime types.
///
@@ -26,7 +32,7 @@ namespace SixLabors.ImageSharp.Formats
///
/// The list of supported s.
///
- private readonly ConcurrentBag imageFormats = new ConcurrentBag();
+ private readonly HashSet imageFormats = new HashSet();
///
/// The list of supported s.
@@ -74,7 +80,14 @@ namespace SixLabors.ImageSharp.Formats
Guard.NotNull(format, nameof(format));
Guard.NotNull(format.MimeTypes, nameof(format.MimeTypes));
Guard.NotNull(format.FileExtensions, nameof(format.FileExtensions));
- this.imageFormats.Add(format);
+
+ lock (HashLock)
+ {
+ if (!this.imageFormats.Contains(format))
+ {
+ this.imageFormats.Add(format);
+ }
+ }
}
///
@@ -86,9 +99,9 @@ namespace SixLabors.ImageSharp.Formats
{
Guard.NotNullOrWhiteSpace(extension, nameof(extension));
- if (extension[0] == '.')
- {
- extension = extension.Substring(1);
+ if (extension[0] == '.')
+ {
+ extension = extension.Substring(1);
}
return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase));
diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs
index 1a7183df8..4bec25f7a 100644
--- a/tests/ImageSharp.Tests/ConfigurationTests.cs
+++ b/tests/ImageSharp.Tests/ConfigurationTests.cs
@@ -2,13 +2,9 @@
// Licensed under the Apache License, Version 2.0.
using System;
-using System.Collections.Generic;
-using System.IO;
using System.Linq;
-using SixLabors.ImageSharp.Formats;
-using SixLabors.ImageSharp.IO;
-using SixLabors.ImageSharp.PixelFormats;
using Moq;
+using SixLabors.ImageSharp.IO;
using Xunit;
// ReSharper disable InconsistentNaming
@@ -19,8 +15,8 @@ namespace SixLabors.ImageSharp.Tests
///
public class ConfigurationTests
{
- public Configuration ConfigurationEmpty { get; private set; }
- public Configuration DefaultConfiguration { get; private set; }
+ public Configuration ConfigurationEmpty { get; }
+ public Configuration DefaultConfiguration { get; }
public ConfigurationTests()
{
@@ -96,5 +92,26 @@ namespace SixLabors.ImageSharp.Tests
provider.Verify(x => x.Configure(config));
}
+
+ [Fact]
+ public void ConfigurationCannotAddDuplicates()
+ {
+ const int count = 4;
+ Configuration config = Configuration.Default;
+
+ Assert.Equal(count, config.ImageFormats.Count());
+
+ config.ImageFormatsManager.AddImageFormat(ImageFormats.Bmp);
+
+ Assert.Equal(count, config.ImageFormats.Count());
+ }
+
+ [Fact]
+ public void DefaultConfigurationHasCorrectFormatCount()
+ {
+ Configuration config = Configuration.Default;
+
+ Assert.Equal(4, config.ImageFormats.Count());
+ }
}
}
\ No newline at end of file