2014-03-27 87 views
2

使用maxmind/geoip api我想出了錯誤「數據庫已關閉」任何人有任何想法,爲什麼這可能是?Maxmind/geoip錯誤「數據庫已關閉」

我已經嘗試了多種不同的方式來做到這一點(單行&多行解決方案等),但不知道爲什麼它不起作用。 調試時我發現,對數據庫進行讀取的代碼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'已被賦予適當的值。

我的方法有什麼問題?

+0

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"); } } 

每個方法調用然後檢查做了一個輕微的修改,使其成爲一個真正的問題。當然,可以隨意進一步改進內容 – Alex

回答

2

old version of the api代碼隱藏它無法加載該文件的事實:如果this.file已設置引發異常你看到

public Country getCountry(long ipAddress){ 
      if (file == null) { 
       //throw new IllegalStateException("Database has been closed."); 
       throw new Exception("Database has been closed."); 
      } 
0

事實證明,我犯了一個錯誤,並且正在服務器上的錯誤位置查找文件。也就是說,由geoip給出的錯誤消息並沒有說明代碼中的錯誤在哪裏,並且被告知沒有找到.dat文件會很有幫助,並且會在'LookupService'行中拋出錯誤而不是錯誤來到'國家'線。 感謝那些試圖幫助的人!

相關問題