更改的sqldependency并不总是触发

时间:2016-09-08 10:14:33

标签: vb.net onchange sqldependency

这是我的代码:

 Public Sub RegisterNotification()
    Dim conStr As String = ConfigurationManager.ConnectionStrings("sqlConString").ConnectionString
    Dim sqlCommand As String = "SELECT [ID],[Description], [CreateDate], [NoteTypeID] from [note].[Notes]"

    Using con As New SqlConnection(conStr)
        Dim cmd As New SqlCommand(sqlCommand, con)
        If con.State <> System.Data.ConnectionState.Open Then
            con.Open()
        End If
        cmd.Notification = Nothing
        Dim sqlDep As New SqlDependency(cmd)
        AddHandler sqlDep.OnChange, AddressOf sqlDep_OnChange

        Using reader As SqlDataReader = cmd.ExecuteReader()
        End Using
    End Using
End Sub

Private Sub sqlDep_OnChange(sender As Object, e As SqlNotificationEventArgs)
    If e.Type = SqlNotificationType.Change Then
        Dim sqlDep As SqlDependency = TryCast(sender, SqlDependency)
        'Dim id
        RemoveHandler sqlDep.OnChange, AddressOf sqlDep_OnChange

        If e.Info = 1 Then


            Dim notificationHub = GlobalHost.ConnectionManager.GetHubContext(Of NotificationHub)()
            notificationHub.Clients.All.notify("added")
            RegisterNotification()
            'GetNotificationList(0, 1)
        End If


    End If
End Sub

Global.asax代码:

 Protected Sub Application_Start()
    Try
        Dim _obj As New DAL.sap.Register
        _obj.Register()
        AreaRegistration.RegisterAllAreas()
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
        RouteConfig.RegisterRoutes(RouteTable.Routes)
        BundleConfig.RegisterBundles(BundleTable.Bundles)
        WebSecurity.InitializeDatabaseConnection("DefaultConnection", "webpages_UserProfile", "UserId", "UserName", True)


        SqlDependency.Start(con)
    Catch ex As Exception

    End Try



End Sub

Protected Sub Session_Start(sender As Object, e As EventArgs)
    Try
        Dim NC As New NotificationComponent()            
        NC.RegisterNotification()
    Catch ex As Exception

    End Try

End Sub
Protected Sub Application_End()
    Try
        SqlDependency.Stop(con)
    Catch ex As Exception

    End Try

End Sub

启动代码:

<Assembly: OwinStartupAttribute(GetType(www.Startup))>

命名空间www 部分公共类启动         公共子配置(app as IAppBuilder)             app.MapSignalR()         结束子     结束班 结束命名空间

我的问题是sqldep_onchange并不总是触发。有时它完美无缺,但有时候什么也没发生这是一个bug还是类似的东西?我做错了什么?

1 个答案:

答案 0 :(得分:0)

看起来一切都是正确的,虽然我的VB非常生疏,我会尝试删除SqlNotificationType上的if并查看是否有帮助,它可能太限制了,我还要确保它实际上是依赖性没有开火,而是稍后吞下它的东西。如果您的TryCast失败会发生什么?由于它不是在尝试它不会抛出,你的通知将不会触发,你的新处理程序将不会被添加。

Private Sub sqlDep_OnChange(sender As Object, e As SqlNotificationEventArgs)
If e.Type = SqlNotificationType.Change Then
    Dim sqlDep As SqlDependency = TryCast(sender, SqlDependency)
    'Dim id

   Delete --> ***RemoveHandler sqlDep.OnChange, AddressOf sqlDep_OnChange*** <--Delete

    If e.Info = 1 Then


        Dim notificationHub = GlobalHost.ConnectionManager.GetHubContext(Of NotificationHub)()
        notificationHub.Clients.All.notify("added")
        RegisterNotification()
        'GetNotificationList(0, 1)
    End If


End If

End Sub