Code2.Data.GeoIP 0.1.21

dotnet add package Code2.Data.GeoIP --version 0.1.21
NuGet\Install-Package Code2.Data.GeoIP -Version 0.1.21
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Code2.Data.GeoIP" Version="0.1.21" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Code2.Data.GeoIP --version 0.1.21
#r "nuget: Code2.Data.GeoIP, 0.1.21"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Code2.Data.GeoIP as a Cake Addin
#addin nuget:?package=Code2.Data.GeoIP&version=0.1.21

// Install Code2.Data.GeoIP as a Cake Tool
#tool nuget:?package=Code2.Data.GeoIP&version=0.1.21

Code2.Data.GeoIP

Service for maxmind geoip csv data which defaults to storing the data in memory. The csv files can be downloaded after registering at https://www.maxmind.com/.

options

  • MaxmindLicenceKey, can be obtained after registering a free maxmind account, default: ""
  • MaxmindEdition, edition specifier GeoLite2-Country-CSV, GeoLite2-City-CSV, ..etc, default: "GeoLite2-City-CSV"
  • CsvDownloadUrl, csv download url template with placeholders for $(MaxmindEdition) and $(MaxmindLicenceKey), default: <maxmind-csv-download-url>
  • CsvDataDirectory, directory to store the csv files, default: "./data"
  • CsvBlocksIPv4FileFilter, csv ipv4 blocks *file filter, default: "Blocks-IPv4.csv"
  • CsvBlocksIPv6FileFilter, csv ipv6 blocks *file filter, default: "Blocks-IPv6.csv"
  • CsvLocationsFileFilter, csv locations *file filter, default: "Locations-en.csv"
  • CsvReaderChunkSize, amount of lines to read and process, default=5000
  • CsvReaderErrorLogFile, log file for csv read errors, default: "./data/csv_error.txt"
  • CsvUpdateIntervalInDays, automatic update interval in days related to file age, default: 10
  • AutoLoad, loads the files upon initialization when HasData=false, default: false

*file filter is used search for a specific file and can be set to null to prevent the file from loading

usage

As there are 2 types of csv files you can either use GeoIPServiceCity or GeoIPServiceCountry.

using System;
using Code2.Data.GeoIP;


var geoIPService = new GeoIPServiceCity();
// optional when manual updating csv files
geoIPService.Options.MaxmindEdition = "GeoLite2-City-CSV"; // or GeoLite2-Country-CSV for GeoIPServiceCountry
geoIPService.Options.MaxmindLicenseKey = "<maxmind_licence_key>";

if((DateTime.Now - geoIPService.GetLastFileWriteTime()).TotalDays > 10)
{
	geoIPService.UpdateFiles();
}
geoIPService.Load();

Console.Write("lookup address:");
while (true)
{
	string? line = Console.ReadLine();
	if (string.IsNullOrEmpty(line)) break;
	CityBlock? cityBlock = geoIPService.GetBlock(line);
	CityLocation? cityLocation = cityBlock is null? null: geoIPService.GetLocation(cityBlock.GeoNameId);
	string result = cityLocation is null ? "not found" : $"{cityLocation.CountryName} {cityLocation.CityName}";
	Console.WriteLine(result);
}

If you're only interested in a few object attributes you can define and use your own objects as long as they are derived from LocationBase and BlockBase.


public class MyBlock: BlockBase {public int AccuracyRadius {get;set;} }
public class MyLocation: LocationBase {public string? CityName {get;set;} }
var service = new GeoIPService<MyBlock, MyLocation>(options);
service.Load();

remarks

The InMemoryBlocksRepository stores the data chunked with the chunksize equal to Options.CsvReaderChunkSize

references

https://www.maxmind.com/

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.21 78 4/16/2024
0.1.19 83 4/14/2024
0.1.16 75 4/11/2024
0.1.3 78 9/13/2023