EPiServer, Episerver 11

Episerver TIP: Get IGeolocationProvider from IoC container

Old way of getting the IGeolocationProvider using the static property is now depracated and you should get the provider from IoC container where the default provider is registered.

So the old way was like this:

var provider = EPiServer.Personalization.Geolocation.Provider;
var result = provider.Lookup(GetUsersIPAddressFromSomewhereHere());

The static property Geolocation.Provider is nowadays marked as deprecated and with message “The default provider is registered in IOC container as GeolocationProviderBase“.

But besides using the actual class GeolocationProviderBase you can also use the IGeolocationProvider interface to get the actual implementation of it.

So with this Episerver change we can now use constructor injection (preferred), the Episerver Injected<IGeolocationProvider> property and ServiceLocator.Current.GetInstance<IGeolocationProvider>() to get the IGeolocationProvider implementation.

Here is sample code (image) of all the variations how to get the IGeolocationProvider.


If you need sample code how to lookup users IP-address then have a look at this post by Jon D Jones or use your best friend Google with keywords like C#, ASP.NET, IP-address, load balancing, x-forwarded-for.


2 thoughts on “Episerver TIP: Get IGeolocationProvider from IoC container

  1. I would strongly vote against any other form as constructor injection 😉
    Maybe sometimes Ambient Context (like Something.Current) is the only option (like in places where framework does not provide/support constructor injections).

    1. Exactly. Was thinking should I add the reasoning why so – but then again the real subject of the post would change 😀
      Maybe it should have said: “Use constructor injection when ever you can and fallback to the other options only when you can’t use constructor injection.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s