Asked by:
how to make web service realtime by signalr

Question
-
User-1634604574 posted
i have this method inside web service file i want to make it real time with sql by using signalR but is not working real time
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.Script.Services; using System.Web.Services; using service.Hubs; using System.Configuration; using FireSharp.Config; //contain configuration to connect to c# using FireSharp.Interfaces;//contain methods using FireSharp.Response;//response to the firebase using Microsoft.AspNet.SignalR; //using System.Web.Script.Services.ScriptService; namespace service { /// <summary> /// Summary description for WebService1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [WebMethod] // [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public void gettingbranch() { List<Data> b_list = new List<Data>(); using (var connection = new SqlConnection(connectionstring)) { connection.Open(); using (SqlCommand command = new SqlCommand("detail_notification", connection)) { command.CommandType = CommandType.StoredProcedure; command.Notification = null; SqlDependency.Start(connectionstring); dependency = new SqlDependency(command); dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); if (connection.State == ConnectionState.Closed) connection.Open(); var reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { Data d = new Data(); d.branch = reader["branch"].ToString(); b_list.Add(d); } } } HttpContext.Current.Response.ContentType = "application/json"; HttpContext.Current.Response.Write(JsonConvert.SerializeObject(b_list)); HttpContext.Current.Response.End(); } }
Sunday, November 24, 2019 6:35 PM
All replies
-
User61956409 posted
Hi zhyanadil.it@gmail.com,
zhyanadil.it@gmail.com
i want to make it real time with sql by using signalR but is not working real timePlease share the code of your SignalR client and event handler
dependency_OnChange
, so that we can troubleshoot the issue better.Besides, if the new data/message could not been pushed to all connected clients, please debug and check if the dependency_OnChange could be triggered.
With Regards,
Fei Han
Monday, November 25, 2019 6:20 AM -
User-1634604574 posted
i don't have client i just have that web service file i want to make that method as a realtime after clicking on invoke button show me any change from sql db
here is my hub class
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; using Microsoft.AspNet.SignalR; namespace service.Hubs { public class MyHub : Hub { private static string conString = @"Data Source=**;Initial Catalog=**;User ID=admin;Password=**;MultipleActiveResultSets=true;"; //private static string conString = ConfigurationManager.ConnectionStrings["con"].ToString(); private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); public static void GetStatus(string message) { hubContext.Clients.All.acknowledgeMessage(message); } public void Hello() { Clients.All.hello(); } public static void Show() { IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); context.Clients.All.displayCustomer(); } } }
my startup.cs
using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(AspNetWebApiSignalRDemo.WebApi.Startup))] namespace AspNetWebApiSignalRDemo.WebApi { public partial class Startup { public void Configuration(IAppBuilder app) { // app.MapSignalR(); app.MapSignalR("/signalr", new Microsoft.AspNet.SignalR.HubConfiguration()); } } }
Monday, November 25, 2019 6:38 AM -
User-1634604574 posted
and here is method show in side web service file
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e) { MyHub.Show(); }
Monday, November 25, 2019 6:39 AM -
User-1780421697 posted
If you want to get real time updates from database and display them on your web page as soon as you get them, then you have to use async streams that will allow you to get the records from database and display them on screen, In case of ASP.Net SignalR works fine you can get records by async stream and display on web page using SignalR.
Monday, November 25, 2019 7:08 AM -
User-1780421697 posted
//Console example class Program { static async Task Main(string[] args) { await SumCalculation(); Console.ReadKey(); } private static async Task SumCalculation() { await foreach (var x in Calc.Sum(10)) { Console.WriteLine("Sum is " + x); } } } public class Calc { public static async IAsyncEnumerable<int> Sum(int limit) { int sum = 0; for (var i = 0; i <= limit; i++) { sum += i; await Task.Delay(TimeSpan.FromSeconds(0.5)); yield return sum; } } }
Monday, November 25, 2019 7:19 AM -
User-1634604574 posted
how about file web service?
Monday, November 25, 2019 10:02 AM -
User-1780421697 posted
It does not matter what you are getting in response, what matter is Async Streams that give chunk of data, you just need to call the service and get response from service as soon as you fill the list you get data stream
Monday, November 25, 2019 12:25 PM -
User-1634604574 posted
but why you didn't signalr?
Monday, November 25, 2019 12:31 PM -
User475983607 posted
You misunderstand how the technology works. The service is instantiated when there is an HTTP request. The service is torn down when the service returns the response.
You need to rethink the approach. I recommend using SignalR as outlined in the official documentation.
Monday, November 25, 2019 12:40 PM -
User-1780421697 posted
You can do it with SignalR but in that case you need to get chunk of data by service , i think your page size should be at least 5-10 records then you get record page by page, for example you get first 10 records from service and append them in your grid/datatable then get next 10 records and append them and so on ...
Tuesday, November 26, 2019 10:48 AM