Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling method of SignalR from Global.asax (MVC 3)
    primarykey
    data
    text
    <p>I have created a sample signalR for POC. I want to call a hub method from Global.asax and pass a string value to client. My Message hub is :-</p> <pre><code>[HubName("messageHub")] public class MessageHub : Hub { public static IHubContext context = GlobalHost.ConnectionManager.GetHubContext&lt;MessageHub&gt;(); public void Message() { /* * services and updates the messages * property on the PushMessage hub */ //IHubContext context = GlobalHost.ConnectionManager.GetHubContext&lt;SignalR_Error_Logging.Models.ErrorModel&gt;(); List&lt;GenerateError.Repository.ErrorModel&gt; model = ErrorRepository.GetError(); context.Clients.pushMessages(model[0].ErrorMessage); } </code></pre> <p>I have defined two of the scripts in layout.cshtml</p> <pre><code>&lt;script type="text/javascript" src="../../Scripts/jquery-1.6.4.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="../../Scripts/jquery.signalR-0.5.3.js"&gt;&lt;/script&gt; </code></pre> <p>My Index.html is as below:-</p> <pre><code> @{ ViewBag.Title = "Receive Error message"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;script src="/signalr/hubs" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function () { var myHub = $.connection.messageHub; myHub.pushMessages = function (value) { console.log('Server called addMessage(' + value + ')'); $("#messages").append("&lt;li&gt;" + value + "&lt;/li&gt;"); }; $("#btnMessage").click(function () { myHub.message(); }); $.connection.hub.start().done(function () { alert("Now connected!"); }).fail(function () { alert("Could not Connect!"); }); }); &lt;/script&gt; &lt;h2&gt;Receive Error Messages&lt;/h2&gt; &lt;ul id="messages"&gt;&lt;/ul&gt; &lt;input type="button" id="btnMessage" value="Get Error" /&gt; </code></pre> <p>In Global.asax I have written </p> <pre><code>SignalR_Error_Logging.SignalRHub.MessageHub hub = new SignalRHub.MessageHub(); hub.Message(); </code></pre> <p>In Application_Start();</p> <p>I am not able to display message in my UI(i.e Index.cshtml). </p> <p>Things that i have tried:-</p> <ul> <li>Running the application as IIS.</li> <li><p>Changing the way of creating HubContext.</p> <pre><code> IHubContext _context = GlobalHost.ConnectionManager.GetHubContext&lt;MessageHub&gt;(); context.Clients.notify("Hello world"); </code></pre></li> <li><p><code>if (Clients != null) { Clients.shootErrorMessage(message); this.Clients.shootErrorMessage(message); }</code></p></li> <li><p>Gone thru links of Stackoverflow<a href="https://stackoverflow.com/questions/7634255/calling-signalr-hub-clients-from-elsewhere-in-system">Calling SignalR hub clients from elsewhere in system</a></p></li> </ul> <p>Any advice???</p> <p>When i call my hub method by creating a button in Index.html it works fine.</p> <p>Apologies for not framing my question properly!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload