Please developers help me a week ago I could not solve the problem
These is details of project as :
1- Startup.cs file as :
- [assembly: OwinStartupAttribute(typeof(LinkForEmployment.Startup))]
- [assembly: OwinStartup(typeof(LinkForEmployment.Startup))]
- namespace LinkForEmployment
- {
- public partial class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- app.MapSignalR();
- ConfigureAuth(app);
- }
- }
- }
- using Microsoft.AspNet.SignalR;
- namespace LinkForEmployment
- {
- public class NotificationHub : Hub
- {
- //public void Hello()
- //{
- // Clients.All.hello();
- //}
- }
- }
- using Microsoft.AspNet.SignalR;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Linq;
- using LinkForEmployment.Models;
- using System.Web;
- namespace LinkForEmployment
- {
- public class NotificationComponent
- {
- //Here we will add a function for register notification (will add sql dependency)
- public void RegisterNotification(DateTime currentTime)
- {
- string conStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
- string sqlCommand = @"SELECT [UserId],[NotificationTitle],[NotificationIcon],[NotificationMessage],[link],[NotificationTime] from [dbo].[Notifications] where [NotificationTime] > @AddedOn";
- //you can notice here I have added table name like this [dbo].[Contacts] with [dbo], its mendatory when you use Sql Dependency
- using (SqlConnection con = new SqlConnection(conStr))
- {
- SqlCommand cmd = new SqlCommand(sqlCommand, con);
- cmd.Parameters.AddWithValue("@AddedOn", currentTime);
- if (con.State != System.Data.ConnectionState.Open)
- {
- con.Open();
- }
- cmd.Notification = null;
- SqlDependency sqlDep = new SqlDependency(cmd);
- sqlDep.OnChange += sqlDep_OnChange;
- //we must have to execute the command here
- using (SqlDataReader reader = cmd.ExecuteReader())
- {
- // nothing need to add here now
- }
- }
- }
- void sqlDep_OnChange(object sender, SqlNotificationEventArgs e)
- {
- //or you can also check => if (e.Info == SqlNotificationInfo.Insert) , if you want notification only for inserted record
- if (e.Type == SqlNotificationType.Change)
- {
- SqlDependency sqlDep = sender as SqlDependency;
- sqlDep.OnChange -= sqlDep_OnChange;
- //from here we will send notification message to client
- var notificationHub = GlobalHost.ConnectionManager.GetHubContext
(); - notificationHub.Clients.All.notify("added");
- //re-register notification
- RegisterNotification(DateTime.Now);
- }
- }
- public List
GetData(DateTime afterDate) - {
- using (ApplicationDbContext db = new ApplicationDbContext())
- {
- return db.Notifications.Where(a => a.NotificationTime > afterDate).OrderByDescending(a => a.NotificationTime).ToList();
- }
- }
- }
- }
- namespace LinkForEmployment
- {
- public class MvcApplication : System.Web.HttpApplication
- {
- string con = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- GlobalConfiguration.Configure(WebApiConfig.Register);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- SqlDependency.Start(con);
- }
- protected void Session_Start(object sender, EventArgs e)
- {
- NotificationComponent NC = new NotificationComponent();
- var currentTime = DateTime.Now;
- HttpContext.Current.Session["LastUpdated"] = currentTime;
- NC.RegisterNotification(currentTime);
- }
- protected void Application_End()
- {
- //here we will stop Sql Dependency
- SqlDependency.Stop(con);
- }
- }
- }
- public class Notification
- {
- public int Id { get; set; }
- public string UserId { get; set; }
- public string NotificationTitle { get; set; }
- public string NotificationIcon{ get; set; }
- public string NotificationMessage { get; set; }
- public string link { get; set; }
- public DateTime NotificationTime { get; set; }
- }
- public class NotificationController : Controller
- {
- public JsonResult GetNotifications()
- {
- var notificationRegisterTime = Session["LastUpdated"] != null ? Convert.ToDateTime(Session["LastUpdated"]) : DateTime.Now;
- NotificationComponent NC = new NotificationComponent();
- var list = NC.GetData(notificationRegisterTime);
- //update session here for get only new added contacts (notification)
- Session["LastUpdate"] = DateTime.Now;
- return new JsonResult { Data = list, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
- }
- }
7- _Layout.cshtml Shared page as :
- @using LinkForEmployment.Models;
- @using Microsoft.AspNet.Identity;
- "en-US">
- "utf-8" />
- "viewport" content="width=device-width, initial-scale=1.0">
-
- @*@Styles.Render("~/Content/css")*@
- "~/Content/bootstrap.css" rel="stylesheet" />
- "~/Content/bootstrap-rtl.min.css" rel="stylesheet" />
- "stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
- "~/Content/style.css" rel="stylesheet" />
- @Html.Partial("_EmployerNavbar")
- class="container-fluid pageContent myContainer" id="cont">class="row">
- @RenderBody()
- ="page-footer">
- @*@Scripts.Render("~/bundles/jquery")*@
- @*@Scripts.Render("~/bundles/bootstrap")*@
- @* @RenderSection("scripts", required: false)*@
- @* Add jquery code for Get Notification & setup signalr *@
- <link href="~/Content/StyleSeekerNavbar.css" rel="stylesheet" />
- <style>
- .list-Items a:hover {
- font-size: 14px;
- }
- .NotifyContenet {
- font-size: 10px;
- color: black;
- }
- .navbar-inverse .navbar-nav > .open > a, .navbar-inverse .navbar-nav > .open > a:hover, .navbar-inverse .navbar-nav > .open > a:focus {
- color: #ffffff;
- background-color: none;
- }
- .navbar {
- /*background:#faebd700;*/
- background: #002637;
- }
- style>
- <div class="navbar navbar-inverse navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
- <span class="icon-bar">span>
- <span class="icon-bar">span>
- <span class="icon-bar">span>
- button>
- <a href="#" class="navbar-brand">
- <img class="logo-img" src="~/images/Job-Searh-icon-Converted1.png" />
- a>
- div>
- <div class="navbar-collapse collapse nav-font">
- <ul class="nav navbar-nav navbar-text">
- <li class="navbar-Items">
- <a href="~/Home/index" class="navbar-Item">
- Main
- a>
- li>
- <li class="navbar-Items">
- <a href="#" class="navbar-Item">
- Jobs
- a>
- li>
- <li class="navbar-Items">
- <a href="#" class="navbar-Item">
- Job apply
- a>
- li>
- <li class="navbar-Items">
- <a href="#" class="navbar-Item">
- How to use this Site
- a>
- li>
- <li id="navbar" class="navbar-Items">
- <span class="noti glyphicon glyphicon-globe"><span class="count"> span>span>
- <div class="noti-content">
- <div class="noti-top-arrow">div>
- <ul id="notiContent">ul>
- div>
- li>
- ul>
- div>
- div>
- div>

kumar vipinPosted Nov 19, 2019, 2:15 AM
if it is started then the message is not getting supplied in application
In that Case firstly check signalr is working or not
Askmejust PortalPosted Nov 15, 2019, 7:23 AM
kumar vipinPosted Nov 1, 2019, 9:05 PM
Check magical script of signalr is there or not after running app put url localhost/Signalr/Hubs
try removing where [NotificationTime] > @AddedOn
Ultimate ProgrammerPosted Aug 22, 2019, 4:25 AM
Abdulmajeed AlshariPosted Feb 2, 2018, 7:30 AM
Altaf AnsariPosted Feb 1, 2018, 10:24 PM