C# 信号器不使用ASP.net WebForm项目
当我添加到ASP.net webForm项目时,为什么SignalR不工作? 使用asp.NETWebForm空项目也很好 在我的代码下面 *****MyHub.cs****C# 信号器不使用ASP.net WebForm项目,c#,asp.net,signalr,C#,Asp.net,Signalr,当我添加到ASP.net webForm项目时,为什么SignalR不工作? 使用asp.NETWebForm空项目也很好 在我的代码下面 *****MyHub.cs**** using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.AspNet.SignalR; namespace SignalR_WF_1 { public class My
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalR_WF_1
{
public class MyHub : Hub
{
public static void Show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.displayStatus();
}
}
}
*********新票班**********
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalR_WF_1.Startup))]
namespace SignalR_WF_1
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SignalR_WF_1
{
public class NewTickets
{
public int NTNotification { get; set; }
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="SignalR_WF_1.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-3.1.1.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="/signalR/hubs"></script>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var job = $.connection.myHub;
// Declare a function on the job hub so the server can invoke it
job.client.displayStatus = function () {
getData();
};
// Start the connection
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tbl');
$.ajax({
url: 'index.aspx/GetData',
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
success: function (data) {
debugger;
if (data.d.length > 0) {
var newdata = data.d;
$tbl.empty();
$tbl.append(' <tr><th>Notifications</th></tr>');
var rows = [];
for (var i = 0; i < newdata.length; i++) {
rows.push(' <tr><td>' + newdata[i].NTNotification + '</td></tr>');
}
$tbl.append(rows.join(''));
}
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tbl"></table>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SignalR_WF_1
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
[WebMethod]
public static IEnumerable<NewTickets> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HelpdeskConnectionString"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT * FROM [MyTickets]", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency.Start(ConfigurationManager.ConnectionStrings["HelpdeskConnectionString"].ConnectionString);
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(Dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new NewTickets()
{
NTNotification = x.GetInt32(0),
}).ToList();
}
}
}
private static void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
MyHub.Show();
}
}
}
**********Index.aspx***********
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalR_WF_1.Startup))]
namespace SignalR_WF_1
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SignalR_WF_1
{
public class NewTickets
{
public int NTNotification { get; set; }
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="SignalR_WF_1.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-3.1.1.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="/signalR/hubs"></script>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var job = $.connection.myHub;
// Declare a function on the job hub so the server can invoke it
job.client.displayStatus = function () {
getData();
};
// Start the connection
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tbl');
$.ajax({
url: 'index.aspx/GetData',
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
success: function (data) {
debugger;
if (data.d.length > 0) {
var newdata = data.d;
$tbl.empty();
$tbl.append(' <tr><th>Notifications</th></tr>');
var rows = [];
for (var i = 0; i < newdata.length; i++) {
rows.push(' <tr><td>' + newdata[i].NTNotification + '</td></tr>');
}
$tbl.append(rows.join(''));
}
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tbl"></table>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SignalR_WF_1
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
[WebMethod]
public static IEnumerable<NewTickets> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HelpdeskConnectionString"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT * FROM [MyTickets]", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency.Start(ConfigurationManager.ConnectionStrings["HelpdeskConnectionString"].ConnectionString);
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(Dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new NewTickets()
{
NTNotification = x.GetInt32(0),
}).ToList();
}
}
}
private static void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
MyHub.Show();
}
}
}
**********Index.aspx.cs***********
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalR_WF_1.Startup))]
namespace SignalR_WF_1
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SignalR_WF_1
{
public class NewTickets
{
public int NTNotification { get; set; }
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="SignalR_WF_1.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-3.1.1.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="/signalR/hubs"></script>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var job = $.connection.myHub;
// Declare a function on the job hub so the server can invoke it
job.client.displayStatus = function () {
getData();
};
// Start the connection
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tbl');
$.ajax({
url: 'index.aspx/GetData',
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
success: function (data) {
debugger;
if (data.d.length > 0) {
var newdata = data.d;
$tbl.empty();
$tbl.append(' <tr><th>Notifications</th></tr>');
var rows = [];
for (var i = 0; i < newdata.length; i++) {
rows.push(' <tr><td>' + newdata[i].NTNotification + '</td></tr>');
}
$tbl.append(rows.join(''));
}
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tbl"></table>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SignalR_WF_1
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
[WebMethod]
public static IEnumerable<NewTickets> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HelpdeskConnectionString"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT * FROM [MyTickets]", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency.Start(ConfigurationManager.ConnectionStrings["HelpdeskConnectionString"].ConnectionString);
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(Dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new NewTickets()
{
NTNotification = x.GetInt32(0),
}).ToList();
}
}
}
private static void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
MyHub.Show();
}
}
}
已检查数据库的连接字符串,一切正常
请需要帮助 我认为在你开始调用你的web服务之前,你应该尝试让一个Hello World与Signaler一起工作。而且,您的中心代码看起来不正确。您的服务器方法不应该是静态的。如果在Hub实例本身上获得对上下文的引用,则无需从ConnectionManager获取引用。谢谢你,Glenn,但是如果我在空的asp.net webForm中使用上述代码,则上述代码可以正常工作,而且在调试javaScript时,我发现以下错误:POST 401未经授权