SQL Зависимость не может вызывать функции OnDependencyChange - PullRequest
0 голосов
/ 17 июня 2020

Я впервые работаю над уведомлениями. У меня есть setup signalR, всякий раз, когда я вызываю getAllVehicles, создается новый объект sqlDependency и во время получения данных он подписывается на OnDependencyChange с новым идентификатором и SqlNotificationEventArgs.type = "Subscribe". когда я вношу изменения в свою базу данных (по моему мнению) OnDependencyChange должен запускаться автоматически, откуда уведомление может быть показано пользователю. Вот мой код:

            using (var sCon = new SqlConnection(GetConnectionString()))
            {
                using (var sCmd = new SqlCommand("[getAllVehicles]", sCon))
                {
                    sCon.Open();
                    sCmd.CommandTimeout = 300;
                    sCmd.CommandType = CommandType.StoredProcedure;
                    sCmd.Notification = null;
                    ////here w'll set up our sql dependency
                    dependency = new SqlDependency(sCmd);

                    //// Subscribe to the SqlDependency event.
                    dependency.OnChange += new
                       OnChangeEventHandler(OnDependencyChange);

                    var sParameter = new List<SqlParameter>
                    {
                        new SqlParameter { ParameterName = "@WorkshopID", Value = filter.WorkshopID },
                    };

                    if (null != sParameter)
                    {
                        sCmd.Parameters.AddRange(sParameter.ToArray());
                    }
                    using (var sDAdapter = new SqlDataAdapter(sCmd))
                    {
                        using (var ds = new DataSet())
                        {
                            sDAdapter.Fill(ds);
                            //return ds;
                        }
                    }
                }
            }

Вот функция dependencyChange:

    public void OnDependencyChange(object sender, SqlNotificationEventArgs e)
    {
        // Handle the event (for example, invalidate this cache entry).

        if (dependency != null)
        {
            dependency.OnChange -= OnDependencyChange;
            dependency = null;
        }
        if (e.Type == SqlNotificationType.Change)
        {
            ServerHub.BroadCastMessage("New data added.");
        }


    }

Я запустил SqlDependency в глобальном файле asax. Процедура My Store здесь:

 ALTER PROCEDURE [dbo].[getAllVehicles]  -- [dbo].[getAllVehicles] @WorkshopID = 19
@PlateNumber NVARCHAR(100) = NULL,
@MinProductionYear SMALLINT = NULL,
@MaxProductionYear SMALLINT = NULL,
@CustomerName NVARCHAR(300) = NULL,
@VIN NVARCHAR(40) = NULL,
@MakeID SMALLINT = NULL,
@ModelID SMALLINT = NULL,
@WorkshopID INT 

AS
BEGIN

SET NOCOUNT ON;

SELECT  v.VehicleID, v.InternalVehicleNo, v.MakeID, v.ModelID, v.ProductionYear, v.SeriesYearStart, v.SeriesYearEnd, v.ModelVersionID, v.VIN, v.Color, v.PlateTypeID, v.PlateNumber, v.EngineNumber, v.EngineCapacity, v.EngineCapacityUnitID, v.VehicleTypeID, 
                     v.EngineTypeID, ot2.TypeNameEnglish AS EngineTypeName, v.CustomerID, v.IsActive, mk.ArabicMakeName, mk.EnglishMakeName, mdl.ArabicModelName, mdl.EnglishModelName, CONCAT(cus.FirstName, ' ', cus.LastName) AS CustomerFullName, ot3.TypeNameEnglish AS EngineCapacityUnitName
FROM dbo.Vehicle AS v
    INNER JOIN dbo.Make AS mk ON mk.MakeID = v.MakeID
    INNER JOIN dbo.Model AS mdl ON mdl.ModelID = v.ModelID
    LEFT JOIN dbo.ObjectType AS ot ON ot.ObjectTypeID = v.VehicleTypeID
    LEFT JOIN dbo.ObjectType AS ot2 ON ot2.ObjectTypeID = v.EngineTypeID
    LEFT JOIN dbo.ObjectType AS ot3 ON ot3.ObjectTypeID = v.EngineCapacityUnitID
    LEFT JOIN dbo.Customer AS cus ON cus.CustomerID = v.CustomerID
WHERE 
    (@PlateNumber IS NULL OR v.PlateNumber LIKE '%'+@PlateNumber+'%')
    AND (v.ProductionYear BETWEEN ISNULL(@MinProductionYear, v.ProductionYear) AND ISNULL((CASE WHEN @MaxProductionYear = 0 THEN NULL ELSE @MaxProductionYear END), v.ProductionYear))
    AND (@CustomerName IS NULL OR CONCAT(cus.FirstName, ' ', cus.LastName) LIKE '%'+@CustomerName+'%')
    AND (@VIN IS NULL OR v.VIN LIKE '%'+v.VIN+'%')
    AND (@MakeID = 0 OR v.MakeID = @MakeID)
    AND (@ModelID = 0 OR v.ModelID = @ModelID)
    AND (v.IsDeleted = 0) 
    AND (v.WorkshopID = @WorkshopID)

ORDER BY v.CreatedOn DESC

END;

ищу хороший ответ

...