2

使用 maxmind/geoip api 我想出了错误“数据库已关闭”有人知道为什么会这样吗?

我尝试了多种不同的方法(单线和多线解决方案等),但无法弄清楚为什么它不起作用。调试时,我发现正在为 LookupService 代码行读取数据库,因为我可以看到它已经从数据库中检索了国家/地区名称,但是当我尝试使用

 string userIpAddress = HttpContext.Current.Request.UserHostAddress;
 string geoIpDbPath = "/App_Data/CMSModules/WebAnalytics/MaxMind/";
 string geoIpDb = geoIpDbPath + "GeoIP.dat";
 LookupService ls = new LookupService(geoIpDb, LookupService.GEOIP_MEMORY_CACHE);
 Country c = ls.getCountry(userIpAddress);

这变得非常令人沮丧,因为我可以看到数据库已成功访问并且变量“ls”已被赋予适当的值。

我的方法有什么问题?

4

2 回答 2

2

旧版本的 api代码中隐藏了无法加载文件的事实:

    public LookupService(String databaseFile, int options){
        try {
            this.file = new FileStream(databaseFile, FileMode.Open, FileAccess.Read);
            dboptions = options;
            init();
        } catch(System.SystemException) {
            Console.Write("cannot open file " + databaseFile + "\n");
        }
    }

然后,每个方法调用都会检查 this.file 是否已设置并引发您看到的异常

 public Country getCountry(long ipAddress){
            if (file == null) {
                //throw new IllegalStateException("Database has been closed.");
                throw new Exception("Database has been closed.");
            }
于 2014-08-19T13:52:54.943 回答
0

事实证明我犯了一个错误,并在服务器上的错误位置查找文件。也就是说,geoip 给出的错误消息并没有说明代码中的错误所在,如果被告知没有找到 .dat 文件并将错误抛出到“LookupService”行而不是错误,这将很有帮助即将进入“国家”线。感谢那些试图提供帮助的人!

于 2014-03-28T16:21:11.937 回答