# Implement IpCityLocationService

To extend the default functionality of Sitefinity `IpCityLocationService`, you can implement the `IIpCityLocationService` interface in your class. You then replace the instance of IpCityLocationService that Sitefinity uses with your implementation via the `ObjectFactory` container.  The IIpCityLocationService interface requires you to implement the `GetLocation(IPAddress ipAddress)` method and the `bool IsReady`  property. 

## Extend the service

To extend the default functionality, you can call the Sitefinity base implementation and plug your logic afterwards. For example:

```C#
using System.Net;
using Telerik.Sitefinity.Personalization.Impl.GeoData;

namespace Sitefinity.Documentation.GeoLocation.Samples
{
   public class IpCityLocationServiceCustom : IIpCityLocationService
   {
       private IpCityLocationService ipCityLocationService;

       public IpCityLocationServiceCustom()
       {
           this.ipCityLocationService = new IpCityLocationService();
       }

       public bool IsReady
       {
           get
           {
               return this.ipCityLocationService.IsReady;
           }
       }

       public CityLocation GetLocation(IPAddress ipAddress)
       {
           var cityLocation = this.ipCityLocationService.GetLocation(ipAddress);

           if (cityLocation != null && cityLocation.Country == "United Kingdom" && cityLocation.RegionName == "England")
               cityLocation.Country = "England";

           return cityLocation;
       }
   }
}
```

Next, you register the newly created service in Global.asax as follows:

```C#
using System;
using Telerik.Microsoft.Practices.Unity;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Personalization.Impl.GeoData;

namespace Sitefinity.Documentation.GeoLocation.Samples
{
   public class Global : System.Web.HttpApplication
   {
       protected void Application_Start(object sender, EventArgs e)
       {
           Bootstrapper.Initialized += Bootstrapper_Initialized;
       }

       private void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
       {
           if (e.CommandName == "Bootstrapped")
           {
               ObjectFactory.Container.RegisterType<IIpCityLocationService, IpCityLocationServiceCustom>(new ContainerControlledLifetimeManager());
           }
       }
   }
}
```

## Replace the default service

Sitefinity implementation of `IpCityLocationService` relies on the [MaxMind GeoLite2](https://dev.maxmind.com/geoip/geoip2/geolite2/) database. To change this logic completely and use a different GeoLocation database, you can implement the `IIpCityLocationService` interface with your own logic and register the service.

The following example demonstrates how to implement the `IIpCityLocationService` with the previous version of the MaxMind GeoLite City [database](https://dev.maxmind.com/geoip/legacy/geolite/) and [API](https://www.nuget.org/packages/MaxMind.GeoIP/2.1.17).

> info  **NOTE**: The database and API versions of MaxMind GeoLite, mentioned above are used by Sitefinity CMS versions 11.0 and older. In case you notice discrepancies in the behavior between the old database and the new one used in Sitefinity 11.1 and newer, you can use this sample to achieve backward compatibility. In addition, you can use this sample to get you started on how `IIpCityLocationService` interface can be implemented for any other geo IP database service and used in Sitefinity. 

1. Install the MaxMind GeoIP API, available as a NuGet package in the following location: <https://www.nuget.org/packages/MaxMind.GeoIP/2.1.17>
2. Download the MaxMind GeoLite City database. MaxMind still has the old databases available for download at <https://dev.maxmind.com/geoip/legacy/geolite/>). You need to download the binary (e.g. <http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz>) and extract it in your Sitefinity website \~/App_Data/GeoLocation/ folder. The extracted file name must be GeoLiteCity.dat.
3. Implement the `IIpCityLocationService`. When implementing `IIpCityLocationService` interface in your custom service, you need to:

   1. Implement the `IsReady` property so it indicates to Sitefinity or your custom code whether the service is ready to be used. This sample demonstrates how `IsReady` can be set to true/false by checking whether the MaxMind `LookupService` object is properly instantiated.
   2. Implement the GetLocation method, so that your service can return CityLocation objects. This sample uses the MaxMind LookupService public  `getLocation(IPAddress addr)` method and populates a Sitefinity `CityLocation` object with the result.

   The complete sample follows:

   ```C#
   using MaxMind.GeoIP;
   using System;
   using System.IO;
   using System.Net;
   using Telerik.Sitefinity.Abstractions;
   using Telerik.Sitefinity.Personalization.Impl.GeoData;
   using Telerik.Sitefinity.Services;

   namespace Sitefinity.Documentation.GeoLocation.Samples
   {
       public class IpCityLocationServiceGeoLiteV1 : IIpCityLocationService
       {
           #region Constructor

           /// <summary>
           /// Initializes a new instance of the <see cref="IpCityLocationServiceGeoLiteV1"/> class.
           /// </summary>
           public IpCityLocationServiceGeoLiteV1()
           {
               var geolocationDataAbsolutePath = SystemManager.CurrentHttpContext.Server.MapPath(this.geoIpDatabasePath);
               if (File.Exists(geolocationDataAbsolutePath))
               {
                   try
                   {
                       this.lookupService = new LookupService(geolocationDataAbsolutePath);
                   }
                   catch (IOException ex)
                   {
                       Log.Write(ex, ConfigurationPolicy.ErrorLog);
                   }
               }
               else
               {
                   Log.Write("GeoIP database file was not found. Please make sure that it is located in ~/App_Data/GeoLocation folder and accessible.");
               }
           }

           #endregion

           #region Public properties

           /// <summary>
           /// Indicates wheter the service is ready to work with
           /// </summary>
           public bool IsReady
           {
               get
               {
                   return this.lookupService != null;
               }
           }

           #endregion

           #region Public methods

           /// <summary>
           /// Gets the city location information by a given IP address
           /// </summary>
           /// <param name="ipAddress">The IP address used to get the city location information</param>
           /// <returns>The city location information object</returns>
           public CityLocation GetLocation(IPAddress ipAddress)
           {
               var location = new CityLocation();
               if (ipAddress == null
                   || ipAddress.ToString() == "::1"
                   || ipAddress.ToString() == "127.0.0.1")
                   return null;

               try
               {
                   var cityResponse = this.lookupService.getLocation(ipAddress);

                   if (cityResponse != null)
                   {
                       location.City = cityResponse.city ?? string.Empty;
                       location.Country = cityResponse.countryName ?? string.Empty;
                       location.IsoCode = cityResponse.countryCode ?? string.Empty;
                       location.Region = cityResponse.region ?? string.Empty;
                       location.RegionName = cityResponse.regionName ?? string.Empty;
                       location.Latitude = cityResponse.latitude;
                       location.Longitude = cityResponse.longitude;
                       location.PostalCode = cityResponse.postalCode;
                       location.MetroCode = cityResponse.metro_code;
                   }
                   else
                   {
                       Log.Write("IP location cannot be determined.  IP Address: " + ipAddress.ToString());
                   }
               }
               catch (Exception ex)
               {
                   Log.Write(ex.Message.ToString());
                   return null;
               }

               return location;
           }

           #endregion

           #region Private fields

           private LookupService lookupService;
           private readonly string geoIpDatabasePath = "~/App_Data/GeoLocation/GeoLiteCity.dat";

           #endregion
       }

   }

   ```
4. Replace the default `IpCityLocationService` used by Sitefinity with your custom one. To do that you need to hook up to the `Bootstrapper.Bootstrapped` event in your Sitefinity application’s Global.asax and register your service through the `ObjectFactory` container:
   ```C#
   using System;
   using Telerik.Microsoft.Practices.Unity;
   using Telerik.Sitefinity.Abstractions;
   using Telerik.Sitefinity.Personalization.Impl.GeoData;

   namespace Sitefinity.Documentation.GeoLocation.Samples
   {
     public class Global : System.Web.HttpApplication
     {
         protected void Application_Start(object sender, EventArgs e)
         {
             Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
         }

         private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
         {
             ObjectFactory.Container.RegisterType<IIpCityLocationService, IpCityLocationServiceGeoLiteV1>(new ContainerControlledLifetimeManager());
         }

     }
   }
   ```
