2009-08-24 11 views
0

Silverlight 3アプリケーションがあり、WCF経由でms-sql-server 2008から簡単なデータを取得しています。最初に、データベースに格納されているすべてのids(〜2000)を取得し、その後、これらのidsのすべての詳細を別のテーブルから取得します(平均してIDあたり10レコード)。SilverlightからWCFサービスを呼び出すときに長時間がかかる

私の問題は、細部を実際に結果を得るまで(非常に長い)(約13-18秒)かかることです。最初のdetails-itemが取得された後、残りの部分は素早く入ります。

どこでボトルネックを探しますか?

私が使用するコードは次のとおりです。

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-の関数である:最初、私の2 WCF-方法

ではこの1つはこの1つは、単一のIDの詳細情報を取得する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; 
    } 

を取得しますアプリ、それらのメソッドを消費します。 _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(); 
    } 

にServiceReferenceのインスタンスであるこの1は、WCFサービスは、IDS

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

そして最後に、返さを扱う1を返すとき、呼ばれ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秒は、WCFと開口部への2001年の呼び出しを行うことは非常に速いようで、データベースを閉じます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

ヤップはるかに高速である必要があります。 WCFを1回だけ2000回呼び出すのではなく、2000年のIDのリストでWCFを1回呼び出して解決しました。レコードを返すストアドプロシージャでは、このリストをテンポラリテーブルに書き込みます。次に、私はフェッチしたいデータを含むテーブルにこの一時テーブルを結合します。だから私は一度に必要なすべてのデータを取得します。 – Aaginor