Good morning, I'm starting to work with Signal R
, I've taken an example of an exercise, and in visual everything is perfectly executed, just that when I publish it on a server, it sends me the following error:
Failed to load resource: the server responded to a status of 500 (Internal Server Error)
My code is as follows:
<script src="Scripts/jquery-1.6.4.min.js"></script>
<script src="Scripts/jquery.signalR-2.3.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() {
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>ID</th><th>Name</th><th>Price</th><th>Qty</th></tr>');
var rows = [];
for (var i = 0; i < newdata.length; i++) {
rows.push(' <tr><td>' + newdata[i].id + '</td><td>' + newdata[i].Name + '</td><td>' + newdata[i].PricDecimal + '</td><td>' + newdata[i].QuantDecimal + '</td></tr>');
}
$tbl.append(rows.join(''));
}
}
});
}
</script>
[WebMethod]
public static IEnumerable<Products> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [ProductID],[Name],[UnitPrice],[Quantity] FROM [Products]", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency.Start(ConfigurationManager.ConnectionStrings["DataBase"].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 Products()
{
id = x.GetInt32(0),
Name = x.GetString(1),
PricDecimal = x.GetDecimal(2),
QuantDecimal = x.GetDecimal(3)
}).ToList();
}
}
}
public class MyHub : Hub
{
public static void Show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.displayStatus();
}
}