0
SignalR을 사용하여 실시간 웹 페이지를 작성하려고합니다. 처음 시도하고 있습니다. 내 아약스가 내 서버 코드를 호출하고있다. 그러나 그것은 서버 측에서 어떤 것을 반환하지 않습니다. 여기ASP.Net을 사용하여`SqlDependency.Start()를 실행하기 전에 호출해야 함`오류를 해결하는 방법은 무엇입니까?
는
[WebMethod]
public static IEnumerable<Product> GetData()
{
string cs = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
//Make sure the command object does not already have
using (SqlCommand cmd = new SqlCommand("SELECT [Id],[Name],[PricDecimal],[QuantDecimal]FROM [TestDatabase].[dbo].[Product]"))
{
cmd.Connection = con;
SqlDependency dependency = new SqlDependency(cmd);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (con.State == ConnectionState.Closed)
con.Open();
using (var reader = cmd.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new Product()
{
id = x.GetInt32(0),
Name = x.GetString(1),
PricDecimal = x.GetDecimal(2),
QuantDecimal = x.GetDecimal(3)
}).ToList();
}
}
}
private static void dependency_OnChange(object sender,SqlNotificationEventArgs e)
{
MyHub.show();
}
Default.cs에서 제품 클래스는
public class Product
{
public int id { get; set; }
public string Name { get; set; }
public decimal PricDecimal { get; set; }
public decimal QuantDecimal { get; set; }
}
MyHub.cs FIL 파일 스크립트
<script>
$(function() {
//Proxy created on the fly
var job = $.connection.myHub;
//Declare a function on the hub so the server can invoke it
job.client.displaystatus = function() {
getData();
};
//Start the connection
$.connection.hub.start();
getData();
});
function getData() {
//alert('ok');
var $tb1 = $('#tb1');
$.ajax({
url: 'Default.aspx/GetData',
contentType: "application/json; charset-utf-8",
dataType: "json",
type: "POST",
success: function (data) {
alert('Success:' + data);
},
error:function(){
alert('Failed');
}
});
}
</script>
Default.cs 코드 전자
public class MyHub : Hub
{
public static void show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.displayStatus();
}
}
Startup.cs는 파일
[assembly: OwinStartup(typeof(Startup))]
public class Startup
{
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
app.MapSignalR();
//string cs = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
//System.Data.SqlClient.SqlDependency.Start(cs);
}
}
오류 MSG
예외 유형 : "System.InvalidOperationException"
메시지 : "옵션 값을 제공하지 않고 SqlDependency를 사용하는 경우 SqlDependency 인스턴스에 추가 된 명령을 실행하기 전에 SqlDependency.Start()를 호출해야합니다."
아무도 SignalR에 대해 알고하지를? –
나는 이것을 믿을 수 없다. 이것은 스택 오버 플로우이다. –