Multi-threaded usage with DatabaseReader
mainDatabaseReader object and share it across all threads to optimize performance and resource usage.repository·main·Indexed 18 days ago
https://github.com/maxmind/geoip2-dotnetA client library for accessing MaxMind's GeoIP and GeoLite web services and local .mmdb databases to perform IP geolocation lookups. It provides the WebServiceClient for web-based queries and the DatabaseReader for local database lookups, including support for City, Country, ASN, ISP, Domain, Connection-Type, and Anonymous IP data. The library is thread-safe and compatible with ASP.NET Core dependency injection.
DatabaseReader object and share it across all threads to optimize performance and resource usage.Starting in v5.4.0, MaxMind introduced the Anonymizer object to provide detailed insights into network anonymity.
Important Migration: The following properties on MaxMind.GeoIP2.Model.Traits are marked Obsolete and should be replaced by the Anonymizer object:
IsAnonymousIsAnonymousVpnIsHostingProviderIsPublicProxyIsResidentialProxyIsTorExitNodeNew Anonymizer Properties:
Confidence: Score (1-99) representing percent confidence the network is an active VPN.IsAnonymous: Indicates if the IP belongs to an anonymous network.IsAnonymousVpn: True if registered to an anonymous VPN provider.IsHostingProvider: True if the IP belongs to a hosting or VPN provider.IsPublicProxy: True if the IP is a public proxy.IsResidentialProxy: True if the IP is on a suspected anonymizing network belonging to a residential ISP.IsTorExitNode: True if the IP is a Tor exit node.NetworkLastSeen: Last day the network was sighted (available on .NET 6.0+ as DateOnly).ProviderName: The name of the associated VPN provider.geonameId property. This integer identifies a geographical feature (city, region, country, etc.) in the GeoNames database. MaxMind sources much of its place name and ISO code data from GeoNames premium datasets.Attributes in returned records may be unpopulated depending on the specific endpoint used and the availability of data for a given IP address.
Note: The only piece of data guaranteed to be returned is the ipAddress attribute found in the MaxMind.GeoIP2.Traits record.
The WebServiceClient is used to access MaxMind's web services.
Key usage patterns:
WebServiceClient instance for multiple requests to avoid creating new connections for every request.dispose of the object when finished to ensure connections are closed and resources are returned to the system.host, fall-back locales, or timeout.Host Options:
geolite.info: Queries the GeoLite web service.sandbox.maxmind.com: Queries the Sandbox GeoIP web service.// Standard usage
var client = new WebServiceClient(42, "license_key1");
// GeoLite usage
var client = new WebServiceClient(42, "license_key1", host: "geolite.info");
// Sandbox usage
var client = new WebServiceClient(42, "license_key1", host: "sandbox.maxmind.com");Do not use values from Names properties as keys in databases or dictionaries, as they may change between releases. Instead, use the following stable identifiers:
City.GeoNameIdContinent.Code or Continent.GeoNameIdCountry.IsoCode or Country.GeoNameIdSubdivision.IsoCode or Subdivision.GeoNameIdTo install the GeoIP .NET API library, use the NuGet package manager. Run the following command in the Visual Studio Package Manager Console:
install-package MaxMind.GeoIP2To use the WebServiceClient with the HttpClient factory pattern as a Typed client in ASP.NET Core, follow these steps:
Program.cs to configure options from your configuration section and register the client for dependency injection:builder.Services.Configure<WebServiceClientOptions>(builder.Configuration.GetSection("MaxMind"));
builder.Services.AddHttpClient<WebServiceClient>();appsettings.json with your credentials and optional settings:"MaxMind": {
"AccountId": 123456,
"LicenseKey": "1234567890",
// "Timeout": 3000,
// "Host": "geolite.info"
}WebServiceClient in your controllers or services.[ApiController]
[Route("[controller]")]
public class MaxMindController : ControllerBase
{
private readonly WebServiceClient _maxMindClient;
public MaxMindController(WebServiceClient maxMindClient)
{
_maxMindClient = maxMindClient;
}
[HttpGet]
public async Task<string> Get()
{
var location = await _maxMindClient.CountryAsync();
return location.Country.Name;
}
}Version 6.0 introduced significant breaking changes due to a shift in the underlying data model and deserialization logic. If you are upgrading from v5.x or earlier, you must address the following:
records instead of classes.new Traits(domain: "example.com")new Traits { Domain = "example.com" }init setters. To modify a model, use the with expression pattern via the WithLocales() method instead of in-place mutation.ToString() on NamedEntity subclasses (City, Country, etc.) has been removed. Use the .Name property directly to get the entity name.FileAccessMode.Memory and the DatabaseReader(Stream) constructor now use anonymous memory-mapped files. This removes the ~2.1 GiB size limit but may not work in environments like WASM, mobile/sandboxed runtimes, or hardened containers with restricted shared-memory syscalls.TryXxx methods now use the [MaybeNullWhen(false)] attribute, allowing the compiler to understand that out parameters are non-null when the method returns true.// Old syntax (v5.x and below)
var traits = new Traits("example.com");
// New syntax (v6.0+)
var traits = new Traits { Domain = "example.com" };To use local GeoIP databases instead of web services, use the DatabaseReader class.
Usage Steps:
DatabaseReader instance by providing the file path to your GeoIP database.City()) with the IP address you want to look up.Best Practice:
Reusing the DatabaseReader object is highly recommended. Creating a new instance is relatively expensive because it must read the file's metadata into memory.
The library uses a hierarchy of exceptions for error handling:
MaxMind.Db.InvalidDatabaseException: Thrown if the database file is corrupt or invalid.AddressNotFoundException: Thrown if the requested IP address is not in the database.All web service exceptions inherit from GeoIP2Exception.
AddressNotFoundException: Thrown when the web service returns an explicit error document indicating the address was not found.AuthenticationException: Thrown for authentication errors.InvalidRequestException: Thrown for invalid requests.OutOfQueriesException: Thrown when query limits are exceeded.HttpException: Thrown for transport errors (e.g., 500 errors) or unexpected status codes.GeoIP2Exception: Thrown if the web service returns a 200 OK but the response body is invalid.Use the Asn method on a DatabaseReader instance initialized with an ASN .mmdb file to retrieve Autonomous System Number and Organization information.
using (var reader = new DatabaseReader("GeoLite2-ASN.mmdb"))
{
var response = reader.Asn("85.25.43.84");
Console.WriteLine(response.AutonomousSystemNumber); // 217
Console.WriteLine(response.AutonomousSystemOrganization); // 'University of Minnesota'
Console.WriteLine(response.IPAddress); // '128.101.101.101'
}