2017-04-06 2 views
2

지난 3 일 동안이 문제로 고심하고 마침내 여기에 문제를 제기하고 있습니다.SqlDependency.OnChange가 실행되지 않는 이유는 무엇입니까?

SignalR 및 SqlDependency를 사용하여 실시간 응용 프로그램을 빌드하려고합니다. 분명히 SQLDependency가 작동하지 않습니다. 그러나, 데이터베이스 상호 작용을 필요로하지 않는 많은 기능을 시도 했으므로 SignalR이 잘 작동합니다.

아래 코드는 제 코드입니다. Here은 제가 참고하고 있습니다.

Global.asax.cs

public class MvcApplication : System.Web.HttpApplication 
{ 
    NotificationHub objNotificationHub; 

    protected void Application_Start() 
    { 
     string connectionString = WebConfigurationManager.AppSettings["SQL"]; 
     // SQL Command Text 
     string commandText = "SELECT status From tableTask"; 
     using (SqlConnection connection = new SqlConnection(connectionString)) 
     { 
      SqlCommand command = new SqlCommand(commandText, connection); 
      SqlDependency dependency = new SqlDependency(command); 
      dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);  
      SqlDependency.Start(connectionString); 
      command.ExecuteReader().Dispose(); 
      objNotificationHub = new NotificationHub(); 
     } 
    } 

    private void dependency_OnChange(object sender, SqlNotificationEventArgs e) 
    { 
     if (e.Type == SqlNotificationType.Change) 
     { 
      objNotificationHub.SendNotifications(); 
     } 
    } 
} 

NotificationHub.cs (SignalR 허브 클래스)

[HubMethodName("sendNotifications")] 
public void SendNotifications() 
{ 
    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>(); 
    context.Clients.All.recieveNotification("asbc"); 
} 

내 SqlDependency.OnChange 이벤트 즉 dependency_OnChange 데이터베이스에 발사되지 최신 정보.

나는 부여 권한이 같은 ALTER 데이타베이스보기 MyDB SET ENABLE_BROKER

및 많은 다른 사람

처럼 모든 방법을 시도했습니다. 하지만 성공하지 못했습니다.

누락 된 부분이있을 수 있습니까? 또한, 내 코드가 SQL Server와 통신하는지 확인하는 방법이 있습니까?

TIA

+0

https://msdn.microsoft.com/en-us/library/ms181122.aspx –

답변

2

당신은 당신의 명령을 실행하지 않는, 그리고이 통지를받을 필요 :

SqlDependency.Start(connectionString); 
string commandText = "SELECT status From dbo.tableTask"; // don't forget schema here 
using (SqlConnection connection = new SqlConnection(connectionString)) 
{ 
    SqlCommand command = new SqlCommand(commandText, connection); 
    SqlDependency dependency = new SqlDependency(command); 
    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);  
    command.ExecuteReader().Dispose(); 
    objNotificationHub = new NotificationHub(); 
} 

당신이 이해했는지 확인하는 방법을 예를 들어 그 종속 작업 (- 하나 개의 통지를받은 후 - 당신 후속 작업을하려면 다시 등록해야합니다.) 또는 this과 같은 래퍼 라이브러리를 사용하십시오.

이 간단한 예제를 테스트 할 수

static void Main(string[] args) { 
    var cs = "connection string";   
    using (SqlConnection connection = new SqlConnection(cs)) 
    { 
     connection.Open(); 
     SqlCommand command = new SqlCommand("select ErrorCode from dbo.Error", connection); 
     SqlDependency dependency = new SqlDependency(command); 
     dependency.OnChange += OnChange; 
     SqlDependency.Start(cs); 
     command.ExecuteReader().Dispose();     
    } 
    Console.ReadKey(); 
} 

private static void OnChange(object sender, SqlNotificationEventArgs e) { 
    Console.WriteLine(e.Info); 
} 
+0

감사 EVK를. 방금 했어. 불행히도 성공하지 못했습니다. TBH, 나는 이전에도 그렇게했다. 그러나 그것은 효과가 없었습니다. 그럼에도 불구하고, 나는 당신이 "당신이 후속 조치를 취하기 위해 다시 등록해야합니다"라는 것이 무슨 뜻인지 알고 싶습니다. 어떻게하면 현재 코드에서이 작업을 수행 할 수 있습니까? –

+0

게시하기 전에 작동하는지 확인 했으므로 다른 문제가없는 한 작동해야합니다. 다시 등록하는 경우 - 하나의 통지를 얻은 후에이 모든 것을 한 번만 다시 실행하면됩니다 (명령 작성, 종속성 작성, 실행). 먼저 간단한 콘솔 응용 프로그램을 만들고 신호기없이 모든 응용 프로그램이 제대로 작동하는지 확인하십시오. – Evk

+0

Evk, 콘솔 응용 프로그램도 하나 시도했지만 작동하지 않았습니다. 데이터베이스 변경 사항은 코드 측면에서 반영되지 않습니다. 이벤트가 발생하지 않습니다. 나는이 좋은 기사를 읽었다 https://hendrikbulens.wordpress.com/2015/04/28/c-firing-code-after-database-changes-with-sqldependency-it/comment-page-1/#comment -355 –

관련 문제