Extend or replace the 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:

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

 

Replace the default service

Sitefinity implementation of IpCityLocationService relies on the MaxMind 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 and API.

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:

  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:

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?