diff --git a/binding/csharp/IP2Region.Net.BenchMark/IP2Region.Net.BenchMark.csproj b/binding/csharp/IP2Region.Net.BenchMark/IP2Region.Net.BenchMark.csproj index 9a77e8c..8d4f436 100644 --- a/binding/csharp/IP2Region.Net.BenchMark/IP2Region.Net.BenchMark.csproj +++ b/binding/csharp/IP2Region.Net.BenchMark/IP2Region.Net.BenchMark.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net9.0 enable enable @@ -17,8 +17,8 @@ - - IP2Region/ip2region.xdb + + IP2Region/ip2region_v4.xdb PreserveNewest diff --git a/binding/csharp/IP2Region.Net.BenchMark/Program.cs b/binding/csharp/IP2Region.Net.BenchMark/Program.cs index c8c14e6..b079b8a 100644 --- a/binding/csharp/IP2Region.Net.BenchMark/Program.cs +++ b/binding/csharp/IP2Region.Net.BenchMark/Program.cs @@ -7,7 +7,7 @@ BenchmarkRunner.Run(typeof(Program).Assembly); public class CachePolicyCompare { - private static readonly string XdbPath = Path.Combine(AppContext.BaseDirectory, "IP2Region", "ip2region.xdb"); + private static readonly string XdbPath = Path.Combine(AppContext.BaseDirectory, "IP2Region", "ip2region_v4.xdb"); private readonly ISearcher _contentSearcher = new Searcher(CachePolicy.Content, XdbPath); private readonly ISearcher _vectorSearcher = new Searcher(CachePolicy.VectorIndex,XdbPath); private readonly ISearcher _fileSearcher = new Searcher(CachePolicy.File,XdbPath); diff --git a/binding/csharp/IP2Region.Net.Test/IP2Region.Net.Test.csproj b/binding/csharp/IP2Region.Net.Test/IP2Region.Net.Test.csproj index e8a9f28..c2dd005 100644 --- a/binding/csharp/IP2Region.Net.Test/IP2Region.Net.Test.csproj +++ b/binding/csharp/IP2Region.Net.Test/IP2Region.Net.Test.csproj @@ -1,7 +1,7 @@ - + - net6.0 + net9.0 enable enable @@ -9,11 +9,17 @@ - - - - - + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -21,12 +27,12 @@ - - TestData/ip.merge.txt + + TestData/ipv4_source.txt PreserveNewest - - TestData/ip2region.xdb + + TestData/ip2region_v4.xdb PreserveNewest diff --git a/binding/csharp/IP2Region.Net.Test/SearcherTest.cs b/binding/csharp/IP2Region.Net.Test/SearcherTest.cs index 87337ff..21aa767 100644 --- a/binding/csharp/IP2Region.Net.Test/SearcherTest.cs +++ b/binding/csharp/IP2Region.Net.Test/SearcherTest.cs @@ -5,7 +5,7 @@ namespace IP2Region.Net.Test; [TestFixture] public class SearcherTest { - private readonly string _xdbPath = Path.Combine(AppContext.BaseDirectory, "TestData", "ip2region.xdb"); + private readonly string _xdbPath = Path.Combine(AppContext.BaseDirectory, "TestData", "ip2region_v4.xdb"); public static IEnumerable Ips() { @@ -49,7 +49,7 @@ public class SearcherTest public void TestBenchSearch(CachePolicy cachePolicy) { Searcher searcher = new Searcher(cachePolicy,_xdbPath); - var srcPath = Path.Combine(AppContext.BaseDirectory, "TestData", "ip.merge.txt"); + var srcPath = Path.Combine(AppContext.BaseDirectory, "TestData", "ipv4_source.txt"); foreach (var line in File.ReadLines(srcPath)) { diff --git a/binding/csharp/IP2Region.Net.slnx b/binding/csharp/IP2Region.Net.slnx new file mode 100644 index 0000000..559f5de --- /dev/null +++ b/binding/csharp/IP2Region.Net.slnx @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/binding/csharp/IP2Region.Net/Extensions/ServiceCollectionExtensions.cs b/binding/csharp/IP2Region.Net/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..980243a --- /dev/null +++ b/binding/csharp/IP2Region.Net/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,33 @@ +using IP2Region.Net.Abstractions; +using IP2Region.Net.XDB; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Microsoft.Extensions.DependencyInjection; + +/// +/// IP2Region 服务扩展类 +/// +public static class IP2RegionExtensions +{ + /// + /// 添加 IP2RegionService 服务。 + /// + /// 集合 + /// IP2Region 数据库文件的路径。 + /// 缓存策略,默认为 。 + public static IServiceCollection AddIP2RegionService(this IServiceCollection services, string path, CachePolicy cachePolicy = CachePolicy.Content) + { + services.TryAddSingleton(provider => + { + return new Searcher(cachePolicy, path); + }); +#if NET8_0_OR_GREATER + services.TryAddKeyedSingleton("IP2Region.Net", (provider, _) => + { + return provider.GetRequiredService(); + }); +#endif + + return services; + } +} diff --git a/binding/csharp/IP2Region.Net/IP2Region.Net.csproj b/binding/csharp/IP2Region.Net/IP2Region.Net.csproj index 372dd42..badc071 100644 --- a/binding/csharp/IP2Region.Net/IP2Region.Net.csproj +++ b/binding/csharp/IP2Region.Net/IP2Region.Net.csproj @@ -2,7 +2,7 @@ IP2Region.Net - 2.0.2 + 2.1.0 IP2Region.Net Alan Lee Apache-2.0 @@ -15,17 +15,44 @@ git enable enable - netstandard2.0;netstandard2.1;net6.0;net7.0 - 10.0 + netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0;net9.0 + latest c2f07fe1-a300-4de3-8200-1278ed8cb5b7 + + + + + + + + + + + + + + + + + + + + + + + + + + + CHANGELOG.md diff --git a/binding/csharp/README.md b/binding/csharp/README.md index 8f8c78c..f203c57 100644 --- a/binding/csharp/README.md +++ b/binding/csharp/README.md @@ -30,28 +30,33 @@ Generate using [maker](https://github.com/lionsoul2014/ip2region/tree/master/mak ## ASP.NET Core Usage ```csharp -services.AddSingleton(new Searcher(CachePolicy , "your xdb file path")); +services.AddIP2RegionService("your xdb file path", cachePolicy: CachePolicy.Content); +``` + +NET6/7 +```csharp +provider.GetRequiredService() +``` + +NET8+ support keyed service +```csharp +provider.GetRequiredKeyedService("IP2Region.Net"); ``` ## Performance ``` ini - BenchmarkDotNet=v0.13.2, OS=macOS 13.4.1 (c) (22F770820d) [Darwin 22.5.0] Apple M1, 1 CPU, 8 logical and 8 physical cores .NET SDK=7.0.306 [Host] : .NET 6.0.20 (6.0.2023.32017), Arm64 RyuJIT AdvSIMD DefaultJob : .NET 6.0.20 (6.0.2023.32017), Arm64 RyuJIT AdvSIMD - - ``` -| Method | Mean | Error | StdDev | -|-------------------------|-----------:|---------:|---------:| -| CachePolicy_Content | 155.7 ns | 0.46 ns | 0.39 ns | -| CachePolicy_File | 2,186.8 ns | 34.27 ns | 32.06 ns | -| CachePolicy_VectorIndex | 1,570.3 ns | 27.53 ns | 22.99 ns | - - +| Method | Mean | Error | StdDev | +|------------------------ |-------------:|----------:|----------:| +| CachePolicy_Content | 58.32 ns | 0.182 ns | 0.170 ns | +| CachePolicy_File | 16,417.56 ns | 50.569 ns | 47.302 ns | +| CachePolicy_VectorIndex | 9,348.11 ns | 38.492 ns | 65.363 ns | ## Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.