2009-08-24 51 views
0

我有一個silverlight 3應用程序,它通過WCF從ms-sql-server 2008中提取一些簡單的數據。首先,它獲取存儲在數據庫中的所有ID(〜2000),然後從另一個表中獲取這些ID的所有詳細信息(平均每個ID約10個記錄)。從silverlight調用WCF服務時的長延遲

我的問題是,從調用細節到實際得到結果(〜13-18秒)需要很長時間。在獲得第一個細節項目後,其他細節快速進入。

我在哪裏尋找瓶頸?

這是我使用的代碼。起初,我的兩個WCF的方法

這一個得到IDS

public HashSet<int> GetAllIds() 
    { 
     HashSet<int> resultSet = new HashSet<int>(); 
     SqlConnection connection = new SqlConnection(sqlConnectionString); 
     connection.Open(); 

     try 
     { 
      SqlCommand command = new SqlCommand("SELECT id FROM stammDaten", connection); 

      using (IDataReader reader = command.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        resultSet.Add(reader.GetInt32(0)); 
       } 
       reader.Close(); 
      } 
     } 
     catch (Exception e) 
     { 
      Logger.instance.ErrorRoutine(e, ""); 
     } 

     connection.Close(); 

     return resultSet; 
    } 

這一個獲取信息的單標識:

public List<GeoKoordinates> GetGeoKoordinatesById(int stammDatenId) 
    { 
     List<GeoKoordinates> resultSet = new List<GeoKoordinates>(); 
     SqlConnection connection = new SqlConnection(sqlConnectionString); 
     connection.Open(); 

     try 
     { 
      SqlCommand command = new SqlCommand("SELECT stammDatenId, position, latitude, longitude FROM geoKoordinates WHERE [email protected] ORDER BY stammDatenId, position", connection); 
      command.Parameters.Add(new SqlParameter("@stammDatenId", SqlDbType.Int)); 
      command.Parameters["@stammDatenId"].Value = stammDatenId; 

      using (IDataReader reader = command.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        GeoKoordinates geoKoors = new GeoKoordinates(); 
        geoKoors.stammDatenId = reader.GetInt32(0); 
        geoKoors.position = reader.GetInt32(1); 
        geoKoors.latitude = reader.GetDouble(2); 
        geoKoors.longitude = reader.GetDouble(3); 

        resultSet.Add(geoKoors); 
       } 
       reader.Close(); 
      } 
     } 
     catch (Exception e) 
     { 
      Logger.instance.ErrorRoutine(e, ""); 
     } 

     connection.Close(); 

     return resultSet; 
    } 

這裏是我的silverlight-的功能應用程序,消耗這些方法。 _S1是服務引用到我的WCF的應用程序的實例

private void InitMap() 
{ 
      ... 
     _s1.GetAllIdsCompleted += new System.EventHandler<OSMDeepEarthExample.ServiceReference1.GetAllIdsCompletedEventArgs>(s1_GetAllIdsCompleted); 
    _s1.GetGeoKoordinatesByIdCompleted += new System.EventHandler<GetGeoKoordinatesByIdCompletedEventArgs>(s1_GetGeoKoordinatesByIdCompleted); 
    _startTime = DateTime.Now; 
    _s1.GetAllIdsAsync(); 
    } 

這一種叫,當WCF服務返回的ID

void s1_GetAllIdsCompleted(object sender, OSMDeepEarthExample.ServiceReference1.GetAllIdsCompletedEventArgs e) 
{ 
    TextBlockTest.Text += (DateTime.Now - _startTime).Seconds.ToString(); 

    foreach (int id in e.Result) 
    { 
     _s1.GetGeoKoordinatesByIdAsync(id); 
    } 
} 

最後,該處理返回detail-的一個集。

void s1_GetGeoKoordinatesByIdCompleted(object sender, GetGeoKoordinatesByIdCompletedEventArgs e) 
{ 
    TextBlockTest.Text += (DateTime.Now - _startTime).Seconds.ToString(); 

    if (e.Result.Count > 0) 
    { 
     Polygon thePoly = new Polygon(); 
     _myLayer.Add(thePoly); 

     ObservableCollection<Point> myPoints = new ObservableCollection<Point>(); 

     foreach (GeoKoordinates ko in e.Result) 
     { 
      Point point = new Point(ko.longitude, ko.latitude); 

      if (!myPoints.Contains(point)) 
       myPoints.Add(point); 
     } 

     thePoly.Points = myPoints; 
        ... more polygone formatting ... 

    } 

由於提前, 弗蘭克

回答

1

因此,這是你在做什麼:

Call WCF 
Open db connection 
get 2000 records 
close connection 

for 1 to 2000 
    Call WCF 
    open db connection 
    get 10 records 
    close connection 
next 

20秒似乎非常快,使2001年調用WCF以及打開和關閉數據庫連接2001次。

嘗試這樣的:

Call WCF once 
Open db connection 
get 2000 records 
for 1 to 2000 
    get 10 records 
next 
close db connection 

return a List<GeoKoordinates> 

1 WCF電話和1個數據庫連接要快很多

+0

邑,這就是問題所在。我通過用2000個ID來調用WCF 1次來解決這個問題,而不是用一個ID來調用它2000次。在存儲過程中,返回記錄,我將這個列表寫入一個臨時表。然後,我將該臨時表與包含要提取的數據的表一起加入。所以我一次獲得所有我需要的數據。 – Aaginor 2010-02-03 15:01:06