2011-07-05 3 views
1
SqlConnection sqlConn = new SqlConnection(m_connectionString); 
m_cmd = sqlConn.CreateCommand(); 
m_cmd.CommandText = "Select id,name from dbo.instances"; 
m_cmd.Notification = null; 
SqlCacheDependency cacheDepen = new SqlCacheDependency(m_cmd); 

using (SqlDataAdapter sda = new SqlDataAdapter(m_cmd)) 
{ 
    DataSet ds = new DataSet(); 
    sda.Fill(ds, "instance"); 
    Cache.Insert("instance", ds.Tables["instance"],cacheDepen); 
    Cache.Insert("timeNow", DateTime.Now.ToLongTimeString(), cacheDepen); 
} 

は、SQL Serverは、データが更新されたときに私を通知するサービスブローカを使用しています。 最後の2行で、「複数のCacheエントリからCacheDependencyオブジェクトを参照しようとしました」という例外がスローされます。 例外のために何をすべきですか?1つのSqlCacheDependencyを複数のCacheアイテムに接続するにはどうすればよいですか?私はSqlCacheDependencyによって、Webアプリケーションを作成してい

答えて

0

SqlCacheDependencyオブジェクトの再利用をテストするためのクイックコンソールアプリケーションを作成しました。m_cmd.Notification = null;を省略すると、が正常に機能します。

namespace CacheDependencyReuse 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     SqlCommand cmd = new SqlCommand("SELECT * FROM Products"); 
     SqlCacheDependency dep = new SqlCacheDependency(cmd); 

     // Uncomment this line to get the Exception 
     //cmd.Notification = null; 

     using (SqlConnection conn = new SqlConnection("Data Source=xxx; Initial Catalog=Northwind;User ID=xxx; Password=xxx;")) 
     { 
      SqlDependency.Start(conn.ConnectionString); 
      cmd.Connection = conn; 
      SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
      DataSet productDataSet = new DataSet(); 
      conn.Open(); 
      adapter.Fill(productDataSet); 


      HttpRuntime.Cache.Insert("Products",productDataSet,dep); 
      HttpRuntime.Cache.Insert("Now",DateTime.Now,dep); 

     } 

     Console.ReadLine(); 
    } 
} 

}

私は、デフォルト値を使用するようNotification取り扱いを残す場合は、ランタイムはあなたを整理すると思います。

+0

申し訳ありませんが、あなたの助言によって私のコードに例外が再びスローされます。 Service Brokerのサポートが必要なSqlDependencyが使用されましたが、Console Applicationとのいくつかの違いがあります。 –

関連する問題