2009-08-24 153 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