SignalR2在使用SqlDependency的MVC5中不起作用

问题描述 投票:1回答:1

我在发布这个问题时犹豫不决,因为网上有很多可用的答案但是运气不好,没有什么可以帮助我。对于我的Web应用程序,我需要通知部分,因为我认为使用SignalR 2。 但它不起作用。以下是完整的代码部分: ==> Hub类

[HubName("MyHub")]
public class MyHub : Hub
{
    public static void Show()
    {
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
        context.Clients.All.displayStatus();
    }
}

==>全局文件

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        SqlDependency.Start(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

    protected void Application_End()
    {
        SqlDependency.Stop(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    }
}

==>库

public class DAL
{
    public List<DTO.Employee> GetEmployee()
    {
        List<DTO.Employee> l = new List<DTO.Employee>();
        try
        {
            using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                con.Open();
                using (var cmd = new SqlCommand("select * from employee", con))
                {
                    cmd.Notification = null;
                    SqlDependency dep = new SqlDependency(cmd);
                    dep.OnChange += new OnChangeEventHandler(Dep_OnChange);

                    using (var drd = cmd.ExecuteReader())
                    {
                        while (drd.Read())
                        {
                            l.Add(new DTO.Employee()
                            {
                                Id = Convert.ToInt64(drd["id"]),
                                Name = Convert.ToString(drd["name"])
                            });
                        }
                    }
                }
            }
        }
        catch { }
        return l;
    }

    private void Dep_OnChange(object sender, SqlNotificationEventArgs e)
    {
        if (e.Type == SqlNotificationType.Change)
        {
            MyHub.Show();
        }
    }
}

==> Owin Startup Class

[assembly: OwinStartupAttribute(typeof(SignalR2_App1.Startup))]

namespace SignalR2_App1 { public partial class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } }

==>查看

<script src="~/Scripts/jquery-1.10.2.min.js"></script>    
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="~/signalr/hubs"></script>
<script type="text/javascript">
    $(function () {
        var job = $.connection.MyHub;
        job.client.displayStatus = function () {
            getData();
        }
        $.connection.hub.start();
        getData();
    })
    function getData() {
        $.ajax({
            url: "/data/getdata",
            contentType: "application/json charset=utf-8",
            dataType: "json",
            type: "Post",
            success: function (result) {
                $.each(result, function (e, obj) {
                    $("#tbldata_tbody").append("<tr><td>" + obj.Id + "</td><td>" + obj.Name + "</td></tr>")
                })
            },
            error: function () {
                alert("Error");
            }
        })
    }
</script>
<body>
<table id="tbldata">
    <thead>
        <tr>
            <td>Id</td>
            <td>Name</td>
        </tr>
    </thead>
    <tbody id="tbldata_tbody"></tbody>
</table>

==>动作

[HttpPost]
    public JsonResult GetData()
    {
        DAL.DAL O = new DAL.DAL();
        return Json(O.GetEmployee());
    }

您可以从以下链接下载整个代码: Code Link

asp.net-mvc-5 signalr signalr-hub sqldependency signalr-2
1个回答
1
投票

根据你的代码,我认为你不了解signalr的所有细节。以下是一些提示:

1.在集线器内部,您不需要GlobalHost.ConnectionsManager

在集线器内部,您已经拥有了Clients属性

2.不要自己创建一个hub实例!

在你DAL内你不应该打电话

  MyHub.Show();

而不是grap HubContext:

GlobalHost.ConnectionManager.GetHubContext<MyHub>().Clients.All.displayStatus();

更好的方法 - 使用强类型集线器:

根据你的代码,我认为你不了解signalr的所有细节。我建议你阅读https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server

例如,在集线器上,您应该只定义客户端可以在服务器上调用的方法。为了正确定义服务器可以在客户端上调用的方法,可以使用“Strongly-Types-Hubs”。见:https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#stronglytypedhubs

Hub对象生存期的背景信息:

https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server

您没有实例化Hub类或从服务器上自己的代码调用其方法;所有这些都由SignalR Hubs管道完成。每次需要处理Hub操作时,SignalR都会创建Hub类的新实例,例如客户端连接,断开连接或对服务器进行方法调用时。

因为Hub类的实例是瞬态的,所以不能使用它们来维持从一个方法调用到下一个方法调用的状态。每次服务器从客户端接收方法调用时,Hub类的新实例都会处理该消息。要通过多个连接和方法调用来维护状态,请使用其他方法,例如数据库,Hub类上的静态变量,或不从Hub派生的其他类。如果将数据保留在内存中,使用Hub类上的静态变量等方法,则应用程序域回收时数据将丢失。

如果要从在Hub类外部运行的自己的代码向客户端发送消息,则不能通过实例化Hub类实例来实现,但是可以通过获取Hub类的SignalR上下文对象来实现。有关详细信息,请参阅本主题后面的如何从Hub类外部调用客户端方法和管理组。

© www.soinside.com 2019 - 2024. All rights reserved.