Vladislav Hromyh Vladislav Hromyh - 2 months ago 26
C# Question

GeoIP Service for .NET Exception

I use .net service called GeoIP and I have an exception that makes me dissapointed.

Service adress: http://www.webservicex.net/geoipservice.asmx?WSDL

I use it very first time, so it might seems to be "click-and-watch" code, but it is not the main thing.

I have an exception when I try to get an IO or country after initalizing client.

GeoIPService.GeoIP geoIp;
GeoIPServiceSoapClient client;
client = new GeoIPServiceSoapClient("GeoIPServiceSoap");

geoIp = client.GetGeoIP("37.57.106.53"); // HERE IS EXCEPTION


Exception message text:

An unhandled exception of type 'System.ServiceModel.FaultException' occurred in mscorlib.dll

Additional information: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.

at WebserviceX.Service.Adapter.IPAdapter.CheckIP(String IP)

at WebserviceX.Service.GeoIPService.GetGeoIP(String IPAddress)

--- End of inner exception stack trace ---

And there is a link for printscreen: https://www.dropbox.com/s/yoq1pr7zp6qax04/geoIpEx.png?dl=0

It would be really lucky for me if someone had to use this service and know how to solve that trouble.

Thanks!

Answer

As per comments, a different GeoIP provider is needed since it doesn't resolve all host address well.

We can use http://json2csharp.com/ and feed it the JSON it gave you from that IP adress. That generates the C# class:

public class GeoIPInfo
{
    public string ip { get; set; }
    public string country_code { get; set; }
    public string country_name { get; set; }
    public string region_code { get; set; }
    public string region_name { get; set; }
    public string city { get; set; }
    public string zip_code { get; set; }
    public string time_zone { get; set; }
    public double latitude { get; set; }
    public double longitude { get; set; }
    public int metro_code { get; set; }
}

We download the JSON through HTTP with a WebClient object, then convert the string into above C# object using Newtonsoft.JSON. (install libary via nuget package at https://www.nuget.org/packages/Newtonsoft.Json/). A sample program would be:

    static void Main(string[] args)
    {
        /* Download the string */
        WebClient client = new WebClient();
        string json = client.DownloadString("https://freegeoip.net/json/37.57.106.53");
        Console.WriteLine("Returned " + json);

        /* We deserialize the string into our custom C# object. ToDo: Check for null return or exception. */
        var geoIPInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<GeoIPInfo>(json);

        /* Print out some info */
        Console.WriteLine(
            "We resolved the IP {0} to country {1}, which has the timezone {2}.",
            geoIPInfo.ip, geoIPInfo.country_name, geoIPInfo.time_zone);

        Console.ReadLine();

        return;
     }

Which outputs

We resolved the IP 37.57.106.53 to country Ukraine, which has the timezone Europe/Kiev.
Comments