Closed b0xCH closed 2 years ago
I don't think there is anything particular about DatabaseReader
that would prevent it from being initialized for multiple databases in that manner at startup. Is there a particular exception or error that you are receiving while doing this?
If I initialize it like this:
builder.Services.AddSingleton(maxmindASN => new DatabaseReader("GeoLite2-ASN.mmdb"));
builder.Services.AddSingleton(maxmindCity => new DatabaseReader("GeoLite2-City.mmdb"));
I will only receive values from "City" as it was the last one initialized. I'm accessing the data like this:
public class IndexModel : PageModel
{
private readonly DatabaseReader _maxmindASN;
private readonly DatabaseReader _maxmindCity;
public IndexModel(DatabaseReader maxmindASN, DatabaseReader maxmindCity)
{
_maxmindASN = maxmindASN;
_maxmindCity = maxmindCity;
}
public static string GetISP(string ClientIP, DatabaseReader _maxmindASN)
{
try
{
return _maxmindASN.Asn(ClientIP).AutonomousSystemOrganization;
}
catch (Exception)
{
return "Local Computer";
}
}
public static string GetCity(string ClientIP, DatabaseReader _maxmindCity)
{
try
{
return _maxmindCity.City(ClientIP).City.Name;
}
catch (Exception)
{
return "Local Computer";
}
}
public void OnGet()
{
remoteIpAddress = "8.8.8.8";
//Debug
Console.WriteLine("ASN: "+GetISP(remoteIpAddress,_maxmindASN)); //This gives me an empty result
Console.WriteLine("City: " + GetCity(remoteIpAddress, _maxmindCity)); //This gives me the city
}
}
This will result in an empty ASN info and a populated City information. I am assuming it is because City has been initialized as the last DatabaseReader. Am I referencing them wrong?
I don't think the issue is at the DatabaseReader
level. You can initialize multiple readers and use them. For instance, this simple program:
using MaxMind.GeoIP2;
var asnReader = new DatabaseReader("/usr/local/share/GeoIP/GeoLite2-ASN.mmdb");
var cityReader = new DatabaseReader("/usr/local/share/GeoIP/GeoLite2-City.mmdb");
Console.WriteLine(asnReader.Asn("128.101.101.101").AutonomousSystemOrganization);
Console.WriteLine(cityReader.City("128.101.101.101").City.Name);
produces:
UMN-SYSTEM
Minneapolis
Perhaps if you remove your exception handling, you will see the underlying cause of the issue. It is a bit hard to diagnose this further without a complete runnable example. I am guessing this is actually an ASP.NET issue that is not specific to this library. I'd suggest asking on Stack Overflow or an online forum.
Closing as I don't believe there to be an actionable issue with the library.
I am currently trying to implement a backend service which in the end should be able to do IP lookups with the three databases "City", "Country" and "ASN".
For performance reasons, I'd like to initiate the DatabaseReader on startup and then utilize it whenever I want to do a Query, so that the DB-File does not need to be accessed for every lookup.
To do so, I tried to add this to "Program.cs":
builder.Services.AddSingleton(maxmindASN => new DatabaseReader("GeoLite2-ASN.mmdb"));
This works and I'm able to query data. However, when I want to add the "City" and "Country" databases, I am unable to register them the same way, as only one of the 3 DBs is initiated.
Is there any recommended way on how to initiate the 3 DBs in the same application on startup, so that they can be shared?
Thanks in advance!