Search code examples
c#maxmind

Maxmind/geoip error "Database has been closed"


Using maxmind/geoip api I have come up with the error "Database has been closed" Anyone have any ideas why this might be?

I have tried multiple different ways of doing this (single line & multiple line solutions etc.) but can't figure out why it isn't working. When debugging I have found that the database is being read for the LookupService line of code as I can see that it has retrieved the country names from the database, but when I try and use

 string userIpAddress = HttpContext.Current.Request.UserHostAddress;
 string geoIpDbPath = "/App_Data/CMSModules/WebAnalytics/MaxMind/";
 string geoIpDb = geoIpDbPath + "GeoIP.dat";
 LookupService ls = new LookupService(geoIpDb, LookupService.GEOIP_MEMORY_CACHE);
 Country c = ls.getCountry(userIpAddress);

This is becoming quite frustrating as I can see that the database has been successfully accessed and the variable 'ls' has been given the appropriate value.

What's wrong with my approach ?


Solution

  • On old version of the api code hides the fact it fails to load the file:

        public LookupService(String databaseFile, int options){
            try {
                this.file = new FileStream(databaseFile, FileMode.Open, FileAccess.Read);
                dboptions = options;
                init();
            } catch(System.SystemException) {
                Console.Write("cannot open file " + databaseFile + "\n");
            }
        }
    

    Each method call then checks if this.file has been set an raises the exception you are seeing

     public Country getCountry(long ipAddress){
                if (file == null) {
                    //throw new IllegalStateException("Database has been closed.");
                    throw new Exception("Database has been closed.");
                }