Take the community feedback survey now.

sanjay.kumar
May 4, 2021
  79
(0 votes)

Update GeoIP2 database automatically

The purpose of the blog to update the latest GeoIP2 database automatically. Because the GeoLite2 Country, City, and ASN databases are updated weekly, every Tuesday of the week. So we required to update the database up to date, which we can accomplish through the schedule job.

About GeoIP2:

MaxMind GeoIP2 offerings provide IP geolocation and proxy detection for a wide range of applications including content customization, advertising, digital rights management, compliance, fraud detection, and security.

The GeoIP2 Database MaxMind provides both binary and CSV databases for GeoIP2. Both formats provide additional data not available in our legacy databases including localized names for cities, subdivisions, and countries.

 

Requirement:

In one of my current projects, we required to read the zip code/postal code on basis of the client IP Address and populate into the address area. Similarly, you can retrieve the Country, City Name, Latitude & Longitude, Metro Code etc.

Solution:

  1. Create the schedule job and download GeoLiteCity.dat.gz.
  2. Uncompressed the file and copy the GeoLite2-City.mmdb database file on physical location which you have define in basePath.
  3. Read the client IP Address.
  4. Read the downloaded GeoLite2-City.mmdb database through DatabaseReader and search the IP address into city database and retrieve zip code/postal code.

 

Okay, so let's do some coding for achieving the above approaches:

Step1: Create the schedule job.

public class GeoIp2DataBaseUpdateJob : ScheduledJobBase
{
        private bool _stopSignaled;
        private const string GeoLiteCityFileDownloadUrl = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz";

        public GeoIp2DataBaseUpdateJob()
        {
            this.IsStoppable = true;
        }

        /// <summary>
        /// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
        /// </summary>
        public override void Stop()
        {
            base.Stop();
            _stopSignaled = true;
        }

        /// <summary>
        /// Called when a scheduled job executes
        /// </summary>
        /// <returns>A status message to be stored in the database log and visible from admin mode</returns>
        public override string Execute()
        {
                   // 1. Download file
            var result = DownloadFile()
                // 2. Unzip file
                .Bind(UnzipFile)
                // 3. Replace at physical location
                .Bind(ReplaceCurrentFile)
                .Match(
                    right => $"👍 GeoIP2 database updated successfully.",
                    left => $"👎 GeoIP2 database update failed.");

            if (_stopSignaled)
            {
                return "Stop of job was called";
            }

            return result;
        }

        private static Either<Exception, string> DownloadFile()
        {
            var licenseKey = ConfigurationManager.AppSettings["Geolocation.LicenseKey"];
            var uri = new Uri(GeoLiteCityFileDownloadUrl + $"&license_key={licenseKey}");

            return Prelude.Try(
                    () =>
                    {
                        using (var client = new WebClient())
                        {
                            var tempDir = Path.GetDirectoryName(Path.GetTempPath());
                            var localFile = Path.Combine(tempDir, "MaxMind/GeoLite2-City.tar.gz");
                            var maxMindFolderPath = Path.GetDirectoryName(localFile);
                            if (!Directory.Exists(maxMindFolderPath) && !string.IsNullOrWhiteSpace(maxMindFolderPath))
                            {
                                Directory.CreateDirectory(maxMindFolderPath);
                            }

                            client.DownloadFile(uri, localFile);
                            return localFile;
                        }
                    })
                .Succ(localFile => Prelude.Right<Exception, string>(localFile))
                .Fail(ex => Prelude.Left<Exception, string>(ex));
        }

        private static Either<Exception, string> ReplaceCurrentFile(string unzippedFileName)
        {
            return Prelude.Try(
                    () =>
                    {
                        var maxMindDbFilePath = Path.Combine(EPiServerFrameworkSection.Instance.AppData.BasePath, "MaxMind/GeoLite2-City.mmdb");
                        File.Copy(unzippedFileName, maxMindDbFilePath, true);

                        //Delete extracted folder
                        var dir = Path.GetDirectoryName(unzippedFileName);
                        if (Directory.Exists(dir))
                        {
                            Directory.Delete(dir, true);
                        }
                        return unzippedFileName;

                    })
                .Succ(fileName => Prelude.Right<Exception, string>(fileName))
                .Fail(ex => Prelude.Left<Exception, string>(ex));
        }

        private static Either<Exception, string> UnzipFile(string downloadedFileName)
        {
            return Prelude.Try(
                    () =>
                    {
                        var dir = Path.GetDirectoryName(downloadedFileName);
                        FileInfo tarFileInfo = new FileInfo(downloadedFileName);

                        DirectoryInfo targetDirectory = new DirectoryInfo(dir ?? string.Empty);
                        if (!targetDirectory.Exists)
                        {
                            targetDirectory.Create();
                        }
                        using (Stream sourceStream = new GZipInputStream(tarFileInfo.OpenRead()))
                        {
                            using (TarArchive tarArchive = TarArchive.CreateInputTarArchive(sourceStream, TarBuffer.DefaultBlockFactor))
                            {
                                tarArchive.ExtractContents(targetDirectory.FullName);
                            }
                        }

                        var filePath = Directory.GetFiles(dir ?? string.Empty, "*.mmdb", SearchOption.AllDirectories)?.LastOrDefault();

                        //Delete .tar.gz file
                        if (File.Exists(downloadedFileName))
                        {
                            File.Delete(downloadedFileName);
                        }
                        return filePath;
                    })
                .Succ(fileName => Prelude.Right<Exception, string>(fileName))
                .Fail(ex => Prelude.Left<Exception, string>(ex));
        }

    }

Step 2: Update the web.config settings

  1. Set the database file path for geolocation provider if you are using for personalization.
  2. Add the basePath for updating the file on a given physical location.
  3. Set MaxMind license key
<episerver.framework>
  ...
  <geolocation defaultprovider="maxmind">
    <providers>
      <add databasefilename="C:\Program Files (x86)\MaxMind\GeoLite2-City.mmdb" name="maxmind" type="EPiServer.Personalization.Providers.MaxMind.GeolocationProvider, EPiServer.ApplicationModules">
    </add></providers>
  </geolocation>
  <appData basePath="C:\Program Files (x86)\MaxMind" />
</episerver.framework>

<appSettings>
 ...
 <add key="Geolocation.LicenseKey" value="{YourLicenseKey}" 
</appSettings>

Step 3: Run the job and make sure the file is updating, if the code cause the error then update accordingly.

Step4: Create the GeoLocationUtility class and read the database file for client IP Address.

 public class GeoLocationUtility
 {
        private static string _maxMindDatabaseFileName = "GeoLite2-City.mmdb";

        public static GeoLocationViewModel GetGeoLocation(IPAddress address, NameValueCollection config)
        {
            string text = config["databaseFileName"];

            if (!string.IsNullOrEmpty(text))
            {
                _maxMindDatabaseFileName = VirtualPathUtilityEx.RebasePhysicalPath(text);
                config.Remove("databaseFileName");
            }

            if (string.IsNullOrWhiteSpace(_maxMindDatabaseFileName)
                || !File.Exists(_maxMindDatabaseFileName)
                || address.AddressFamily != AddressFamily.InterNetwork && address.AddressFamily != AddressFamily.InterNetworkV6)
            {
                return null;
            }

            var reader = new DatabaseReader(_maxMindDatabaseFileName);
            try
            {
                var dbResult = reader.City(address);
                var result = GeoLocationViewModel.Make(dbResult);
                return result;
            }
            catch
            { 
                //ignore exception
            }
            finally
            {
                reader.Dispose();
            }

            return null;
        }

Step 5: Finally, call the utility method where you want to get the zip code/postal code value such as:

  var maxMindDbFilePath = Path.Combine(EPiServerFrameworkSection.Instance.AppData.BasePath, "MaxMind/GeoLite2-City.mmdb");
   var config = new NameValueCollection
   {
    {"databaseFileName", maxMindDbFilePath}
   };

   var  postalCode= GeoLocationUtility.GetGeoLocation(ipAddress, config)?.PostalCode;

I hope you found this informative and helpful, Please leave your valuable comments in the comment box.

Thanks for your visit!

May 04, 2021

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP - Opticon London 2025

This installment of a day in the life of an Optimizely OMVP gives an in-depth coverage of my trip down to London to attend Opticon London 2025 held...

Graham Carr | Oct 2, 2025

Optimizely Web Experimentation Using Real-Time Segments: A Step-by-Step Guide

  Introduction Personalization has become de facto standard for any digital channel to improve the user's engagement KPI’s.  Personalization uses...

Ratish | Oct 1, 2025 |

Trigger DXP Warmup Locally to Catch Bugs & Performance Issues Early

Here’s our documentation on warmup in DXP : 🔗 https://docs.developers.optimizely.com/digital-experience-platform/docs/warming-up-sites What I didn...

dada | Sep 29, 2025

Creating Opal Tools for Stott Robots Handler

This summer, the Netcel Development team and I took part in Optimizely’s Opal Hackathon. The challenge from Optimizely was to extend Opal’s abiliti...

Mark Stott | Sep 28, 2025

Integrating Commerce Search v3 (Vertex AI) with Optimizely Configured Commerce

Introduction This blog provides a technical guide for integrating Commerce Search v3, which leverages Google Cloud's Vertex AI Search, into an...

Vaibhav | Sep 27, 2025

A day in the life of an Optimizely MVP - Opti Graph Extensions add-on v1.0.0 released

I am pleased to announce that the official v1.0.0 of the Opti Graph Extensions add-on has now been released and is generally available. Refer to my...

Graham Carr | Sep 25, 2025