c# - send SignalR client message from background thread -
i'm trying track progress of hangfire background jobs guided article http://docs.hangfire.io/en/latest/background-processing/tracking-progress.html
unfortunately example given in article not working. downloaded , run sample web application (https://github.com/hangfireio/hangfire.highlighter) locally. it's ok when client subscribes after job complete. in case message sent directly hub , has been received client. otherwise message call invoked hangfire job , nothing happens. no exceptions, no result. causing behavior? in case: hangfire jobs not work asynchronously...
code sample:
hangfire job (see highlight method)
using system; using system.collections.generic; using system.net.http; using system.threading.tasks; using hangfire.highlighter.hubs; using hangfire.highlighter.models; using microsoft.aspnet.signalr; namespace hangfire.highlighter.jobs { public class snippethighlighter : idisposable { private readonly ihubcontext _hubcontext; private readonly highlighterdbcontext _dbcontext; public snippethighlighter() : this(globalhost.connectionmanager.gethubcontext<snippethub>(), new highlighterdbcontext()) { } internal snippethighlighter(ihubcontext hubcontext, highlighterdbcontext dbcontext) { if (hubcontext == null) throw new argumentnullexception("hubcontext"); if (dbcontext == null) throw new argumentnullexception("dbcontext"); _hubcontext = hubcontext; _dbcontext = dbcontext; } public void highlight(int snippetid) { var snippet = _dbcontext.codesnippets.find(snippetid); if (snippet == null) return; snippet.highlightedcode = highlightsource(snippet.sourcecode); snippet.highlightedat = datetime.utcnow; _dbcontext.savechanges(); _hubcontext.clients.group(snippethub.getgroup(snippet.id)) .highlight(snippet.highlightedcode); } public void dispose() { _dbcontext.dispose(); } private static async task<string> highlightsourceasync(string source) { using (var client = new httpclient()) { var response = await client.postasync( @"http://hilite.me/api", new formurlencodedcontent(new dictionary<string, string> { { "lexer", "c#" }, { "style", "vs" }, { "code", source } })); response.ensuresuccessstatuscode(); return await response.content.readasstringasync(); } } private static string highlightsource(string source) { // microsoft.net.http not provide synchronous api, // using wrapper perform sync call. return runsync(() => highlightsourceasync(source)); } private static tresult runsync<tresult>(func<task<tresult>> func) { return task.run<task<tresult>>(func).unwrap().getawaiter().getresult(); } } }
hub
using system.data.entity; using system.linq; using system.threading.tasks; using hangfire.highlighter.models; using microsoft.aspnet.signalr; namespace hangfire.highlighter.hubs { public class snippethub : hub { public async task subscribe(int snippetid) { await groups.add(context.connectionid, getgroup(snippetid)); // when user subscribes snippet // highlighted, need send immediately, because // otherwise listen infinitely. using (var db = new highlighterdbcontext()) { var snippet = await db.codesnippets .where(x => x.id == snippetid && x.highlightedcode != null) .singleordefaultasync(); if (snippet != null) { clients.client(context.connectionid) .highlight(snippet.id, snippet.highlightedcode); } } } public static string getgroup(int snippetid) { return "snippet:" + snippetid; } } }
singnalr configured using owin startup class.
Comments
Post a Comment