Thursday 18 December 2014

Getting NServiceBus, SignalR and Autofac to work together

I’m currently working on an existing web app that we’re updating to add in NServiceBus and SignalR. A broad brush overview is shown below…

image

Here a request is sent from the browser which goes to a SignalR hub inside the website. That request is then sent onto the bus and some back end magic happens, which ultimately ends up in a response being sent to the bus. This is picked up by the website and a response is pushed down to the client courtesy of SignalR.

If we look into what’s happening inside the website we see the following…

image

The hub implements an interface that’s injected into the message handler, so that the handler can call back to the hub when the response message comes in, which ultimately then makes the hub call back to the browser to finish the loop.

Getting all this setup was a bit of a bother and I went down a few blind alleys before getting it all to work properly, hence documenting it so that I can hopefully short-circuit someone else’s work.

In pseudocode this is what happens…

  • The browser makes a request to the hub.
  • The hub code creates a unique Request Id, and stuffs this into a dictionary, mapping this Request Id to the SignalR Context.ConnectionId.
  • The hub then sends a message onto the bus, this contains the Request Id.
  • After the back-end has processed this message a response is placed on the bus, again containing the Request Id.
  • The message handler receives the message and uses the callback interface and Request Id to build a dynamic object that we can call the browser back with.
  • The message handler then makes the callback.

For this example I’m doing the callback within the message handler, but you could encapsulate this fully within the Hub too if you prefer. OK, enough explanation, on to some code…

    public interface ICallback
{
dynamic GetCallback(Guid requestId);
}



This interface is used to communicate between the message handler and the Hub. In addition I added a “registration” service that is used to map a Request Id to a client connection Id…

    public interface IRegistration
{
void Register(Guid id, string data);

string GetRegistration(Guid id);
}



The implementation of this service was a simple dictionary, this however should be beefed up as it needs a “tidy up” mechanism that will ensure that data is removed from this dictionary when the client disappears. The hub then is as shown below…

    public class WibbleHub : Hub, ICallback
{
public WibbleHub(IBus bus, IRegistration registration)
{
_bus = bus;
_reg = registration;
}

public void RequestWibble(string text)
{
Guid requestId = Guid.NewGuid();

_reg.Register(requestId, Context.ConnectionId);

_bus.Send(new WibbleMessage { RequestId = requestId, Text = text});
}

public dynamic GetCallback(Guid requestId)
{
dynamic callback = null;
string clientId = _reg.GetRegistration(requestId);

if (null != clientId)
callback = Clients.Client(clientId);

return callback;
}

private IBus _bus;
private IRegistration _reg;
}



The hub imports the bus and registration services, and the RequestWibble method is the one we’re calling from the web client. This registers the mapping between the Request Id and the client Connection Id, and then sends a message onto the bus.


The GetCallback method uses the registration service to find the client connection Id, then returns a dynamic object hooked to that client if the Id matches one that has been registered.


The message handler is fairly simple too…

    public class WibbleHandler : IHandleMessages<WibbleMessage>
{
public WibbleHandler(ICallback callback)
{
_callback = callback;
}

public void Handle(WibbleMessage message)
{
var cb = _callback.GetCallback(message.RequestId);

if (null != cb)
{
try
{
cb.wibbleSucceeded(message.Text);
}
catch (Exception ex)
{
int i = 0;
}
}
}

private ICallback _callback;
}



The handler imports the ICallback interface (exposed by the Hub), and when it handles the incoming message it calls the GetCallback() method to retrieve the dynamic object that allows us to call down to the web client.


The last piece of the puzzle is wiring this up, and that was the main thing that took time to get right, mainly down to my misunderstanding of how NServiceBus does its stuff. I created a DependenciesConfig file as follows…

    public static class DependencyConfig
{
public static void BuildDependencies()
{
var container = BuildContainer();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

public static IContainer BuildContainer()
{
if (null == _container)
{
var builder = new ContainerBuilder();

builder.RegisterControllers(Assembly.GetExecutingAssembly())
.PropertiesAutowired();

builder.RegisterType<WibbleHub>()
.AsSelf()
.As<ICallback>()
.ExternallyOwned()
.SingleInstance();

builder.RegisterType<Registration>().As<IRegistration>().SingleInstance();

_container = builder.Build();

var config = new BusConfiguration();
config.UsePersistence<InMemoryPersistence>();
config.UseTransport<RabbitMQTransport>();
config.UseContainer<AutofacBuilder>(c => c.ExistingLifetimeScope(_container));

var bus = Bus.Create(config);
bus.Start();

}

return _container;
}

private static IContainer _container;
}



This has a static BuildContainer method that creates the AutoFac container, and there are a couple of things of note. First off is the registration of the hub, this is registered as a singleton, as is the registration service too.


Then (and this is the bit that I got wrong) is the configuration of the bus. First off, the container is built before the bus is constructed, so the bus itself is not constructed as part of the container itself, it lives “outside”. The bus is setup to hook to the Autofac container using the UseContainer() member, this links the bus to the registered components, and means that when a message comes in on the bus that it can resolve all dependencies such as the ICallback interface exposed by the hub.


Now to the voodoo magic. In the above there is nowhere that the bus itself is registered with the Autofac container, so conventional wisdom would dictate that any components requiring IBus wouldn’t be able to resolve it, so for example this controller shouldn’t work…

    public class DefaultController : Controller
{
public DefaultController(IBus bus)
{
_bus = bus;
}

public ActionResult Index()
{
return View();
}

private IBus _bus;
}



But it does work. How is a mystery. After a bit of searching I came across this post on Stack Overflow that indicates that NSB does this for you. That’s great, but also somewhat confusing for anyone who doesn’t know this beforehand, which presumably is everyone who uses NSB and Autofac, or any IOC container probably as people are used to registering dependencies themselves, rather than some magic happening. Personally I’m not a fan of this magic, it’ll most likely trip you up. However, with this in place it all works as expected – I can then pop some Javascript into my client and everything works as expected.


There’s one more class to add and that’s the startup for Owin, so that we can get SignalR up and running properly…

    public class Startup
{
public void Configuration(IAppBuilder app)
{
var container = DependencyConfig.BuildContainer();

GlobalHost.DependencyResolver = new AutofacDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.MapSignalR(new HubConfiguration { EnableDetailedErrors = true });
}
}



The demo (attached) has a simple form where you can enter text, click a button and lo and behold a message is displayed on screen. That’s pretty useless, but at least is shows how NServiceBus, Autofac and SIgnalR can play nicely together. I’m using RabbitMQ as my messaging infrastructure, feel free to bring your own along if you would prefer.


Hope this helps someone!

41 comments:

Anonymous said...

Thanks, this was exactly what i needed to get my hubs being called from my business logic.

rushmyessay discount code said...

So much coding in this post and quite hard to understand the coding part, also explain each block of coding with an explanation. Thanks

Oliver Maurice said...

I recommend that you read this little article about essay writing to get the best information possible about the subject. Good luck

Unknown said...

I would like to say that this article awriter.org/essay-writing-services really convinced me, you give me best information!

AngelaHolt said...

Thanks blogger for sharing the best article.Score hero mod Traffic rider mod apk Vector mod

Robert James said...

This is amazing you have defined very well and I got the more benefit from it and the structure and flow of your content with the engaging content is so good I have learnt a lots of things from it. best assignment writers UK appreciate it.

Rebecca Virginia said...

The post is very informative, thank you for sharing the hub and interface topic information.
Assignment Writing in UAE

Stefan Carl said...

Students of New Zealand can buy coursework New Zealand help from StudentsAssignmentHelp.com. We provide instant coursework writing help to the students with free revisions and plagiarism free content.

Assignment Help said...

I appreciate the effort you made to share the knowledge. This is really great stuff for sharing. We also provide an assignment to help Australia students.For more info visit:
Operations Research Assignment Help

KeiraDoltan said...

Most of the student Fear of Missing Deadline. and therefore stress out for any kind of Academic work. If You Hire, Strategic Management Assignment Help Then you will be able to meet Deadline Without any burden.

Ethan Lee said...

Are you looking for assignment help in Sydney? Contact me, I will help you. Myassignmenthelp.com is the home of Australia's best academic writers. Whether you need Assignment help Perth, Assignment Help Brisbane or Assignment Help Adelaide, we can help you.
We will also help you for
Assignment Help Perth,
Assignment Help Brisbane,
Assignment Help Melbourne

daisythomas said...
This comment has been removed by the author.
daisythomas said...

Get My Assignment Help services from StudentsAssignmentHelp.com at the cheap prices. Our High-Qualified writers work according to Assignment requirement guidelines. 24x7 online customer support team. Get your order and improve your Grades. For more information contact here- info@studentsassignmenthelp.com or + 44-755-536-9184.

ireland assignment help said...

Get the doctoral dissertation writing assistance by the expert writers at Assignment Help. Our masters have earned their degrees from the renowned universities of Ireland. They are fluent in writing assignments without missing the deadlines.

michaelgordon said...

Deep learning is a concept that should be implemented everywhere because this is what true learning is and gives students a total understanding of the subject. For assignment help and guidance on a college assignment, visit GoAssignmentHelp for best assignment help in sydney Services​

Amania said...

Buy all the items in Auckland New Zealand. Treasurebox provide you all the house hold outdoor garden nzitems which will be easily available at low price..!

Teresa Halminton said...

I really like using Gmail but I can't solve my trouble without gmail email login, I think this website are gonna be helpful to you.

Himah Hamih said...

Very interesting content which helps me to get the indepth knowledge.

online internship
online internships
watch internship online
online internship for students
the internship online
online internship with certificate
online internship certificate
python online internship
data science online internship

onlineAssignmenthelp said...

Assignment Help in Brisbane
Which is the best assignment help in Brisbane? Ans. Thetutorshelp.com is the best assignment help service in Brisbane. We have been offering See. Accounting assignment. Finance assignment , Social science assignment, Engineering assignment all See more= https://www.thetutorshelp.com/assignment-help-in-brisbane.php

onlineAssignmenthelp said...

http://ejournal.litbang.kemkes.go.id/index.php/vk/comment/view/3502/4978/3278
http://ejournal.litbang.kemkes.go.id/index.php/vk/comment/view/3502/0/3278
http://www.morganskinner.com/2014/12/getting-nservicebus-signalr-and-autofac.html
http://www.craftyjenschow.com/2017/11/elles-studio-november-2017-kit-reveal.html
http://www.apsense.com/article/what-kinds-of-helps-brisbane-students-need-help-with-their-assignment.html
https://www.apsense.com/article/what-kinds-of-helps-brisbane-students-need-help-with-their-assignment.html
https://p2m.pnl.ac.id/berita-pengumuman-hasil-seleksi-usulproposal-p2m-dana-dipa-dan-dana-boptn-pnl-tahun-anggaran-2017.html
https://gradegov.com/forum/discussion/114/ocare-my-finally-die-here039s-how
https://pinballspares.com.au/showthread.php?tid=10045&pid=46071
https://pinballspares.com.au/showthread.php?tid=5040

amnamalik said...

https://newcrack.info/wondershare-video-converter-crack/
https://newcrack.info/spyhunter-crack/
https://newcrack.info/wavepad-sound-editor-crack/
https://newcrack.info/adobe-photoshop-cs5-crack/

onlineAssignmenthelp said...

Assignment Help in Australia
We provide online assignment help & writing services like dissertation/thesis, essay, case study, Australia for university and college students. Best Law assignment help team in Australia by Australian Students. Let the experts help with your assignment for High Distinction. Get Help Today! https://www.thetutorshelp.com/australia.php

onlineAssignmenthelp said...

Assignment Help in Adelaide
The Tutors Help is the best assignment writer in Adelaide We provide the most authentic assignment help service in Australia at affordable prices. assignments are written by experts https://www.thetutorshelp.com/assignment-help-in-adelaide.php

onlineAssignmenthelp said...

Assignment Help in Brisbane
Looking for the best assignment writing help in Brisbane? We, Assignment writing help experts are proud to announce that we also provide our amazing most significant advantage of taking assignment help in Brisbane is that these professionals guarantee an increase in grades. https://www.thetutorshelp.com/assignment-help-in-brisbane.php

PGSLOT said...

หาเงินง่ายๆได้แล้ววันนี้เพียงคุณหันมาร่วมลงทุนกับเราที่เกม Cards Hi Lo เกมที่ถูกสร้างสรรค์มาอย่างดีเพื่อคุณ วิกฤตเศรษฐกิจครั้งนี้เราจะผ่านมันไปด้วยกัน ลงทุนเลยตอนนี้รับฟรีเครดิตสูงสุดหนึ่งร้อยเปอร์เซ็นต์สำหรับสมาชิกใหม่ แจกจริงแจกกระจายเหมือนให้กันฟรีๆ

Anne said...

แตกง่ายที่สุด แตกบ่อย แจกทุกวัน ไม่ว่าเกมไหน ไม่ว่าค่ายไหน คุณก็มีโอกาสถูกแจ็กพอตได้เช่นกัน เพราะเว็บของเรา เป็นเว็บไซต์ที่มีรางวัลแจ็คพอตที่โดนมากที่สุด ทุนน้อยก็มีสิทธิ์ลุ้นรางวัลใหญ่เช่นกัน เพราะไม่มีใครรู้ว่าแจ็คพอตจะออกมาเมื่อไหร่ คุณอาจเป็นคนที่โชคดี แค่เล่น ซุปเปอร์สล็อตกับเราก็มีสิทธิ์ลุ้นแจ็คพอตทุกวัน แตกง่าย. จ่ายจริงทุกวัน

koko said...

แจกเงินกระจาย มีโบนัสแจกให้กันแบบรัว ๆ เข้ามาร่วมสนุกกันเลยที่เกม Three Monkeys เกมสล็อตที่นำพาโดยเจ้าลิงน้อยทั้ง 3 ตน มีรางวัลมากมายมอบให้กับนักเดิมพัน สนใจ เชิญคลิก ภายในเกมพบกันสัญลักษณ์ของ Wild เป็นรูปเจ้าลิงน้อยสามตัว สามารถเทนได้ทุกสัญลักษณ์ยกเว้นสัญลักษณ์ของ Scatter ยิ่งเล่นก็ยิ่งรวยคุ้มกว่านี้ไม่มีอีกแล้ว

Anonymous said...

แจ็คพอตแตะกระจาย อยากมีเงินใช้ไม่ขาดกระเป๋าเล่นเลยที่เกม Shaolin Soccer เกมสล็อตคุณภาพดีที่มาในธีมของนักเตะทีมกังฟู มีฟีเจอร์พิเศษและของรางวัลมากมายมอบให้แบบไม่อั้นทุกช่วงเวลา สนใจเชิญเข้ามาเลยที่ PGSLOT.SEXY เป็นเกมสล็อต 5 รีล 4 แถว มีไลน์ทั้งหมด 25 ไลน์ ได้เงินเร็ว สมัครเล่นตอนนี้รับเครดิตฟรีหนึ่งร้อยเปอร์เซ็นต์

seoreturn168 said...

ฝาก50รับ150ไม่ต้องทําเทิร์นถอนไม่จํากัด

Akaslot said...

แนะนำ AKA เว็บไซต์เดิมพันออนไลน์ที่น่าลงทำทุน
“สล็อต” เป็นเลิศในเว็บพนันชั้นแนวหน้ายอดนิยมในกลุ่มคนที่ชอบพอการพนันในตอนนี้ เว็บให้ท่านบันเทิงใจกับการพนันเกมโปรดของคุณโดยไม่นึกถึงเวลาแล้วก็ที่หมาย ฉะนั้นคุณก็เลยสามารถเล่นเกมได้อีกทั้งในสถานที่ทำงานหรือที่บ้านโดยนั่งอยู่บนเตียงพร้อมด้วยถังป๊อปคอร์นและก็โค้ก โอกาสเป็นของคุณทั้งผอง!!
ในทางตรงกันข้าม เว็บยังมีฟีพบร์ที่น่าตื่นตาตื่นใจเพื่อทำให้ทางการเล่นเกมของคุณง่ายมากยิ่งขึ้น
มาดูข้อมูลเชิงลึกเกี่ยวกับคุณลักษณะกลุ่มนี้กัน
คุณลักษณะของสล็อตออนไลน์
คุณลักษณะมีการเจาะจงไว้ดังต่อไปนี้
เว็บมีส่วนต่อผสานที่ใช้งานง่ายซึ่งเปิดขึ้นข้างในไม่กี่วินาที นอกจากนั้น คุณยังสามารถเข้าถึงเว็บบนเครื่องใช้ไม้สอยต่างๆอย่างเช่น แล็ปท็อป คอมพิวเตอร์ หรือโทรศัพท์เคลื่อนที่ (Android หรือ iOS)
มีกรรมวิธีสมัครสมาชิกกล้วยๆซึ่งคุณสามารถสมัครบัตรสมาชิกได้ข้างในไม่กี่คลิก
แพลตฟอร์มนี้ให้ท่านฝากหรือเบิกเงินรางวัลของคุณได้ตลอดระยะเวลา นอกเหนือจากนั้นยังช่วยปกป้องรักษาเนื้อหาแนวทางการทำธุรกรรมของคุณจากการเสี่ยงภัย
มีข้าราชการช่วยเหลือที่สุดยอดที่พร้อมให้ความช่วยเหลือเกื้อกูลคุณทุกเมื่อถ้าหากคุณประสบพบปัญหาใดๆก็ตามขณะเล่นเกมหรือท่องเว็บ
เว็บมีระบบระเบียบทำการระดับสูงที่ป้องกันข้อมูลของคุณ ยกตัวอย่างเช่น รหัสผ่าน เนื้อหาแบงค์จากแฮกเกอร์
เว็บไซต์แห่งนี้ยังอำนวยความสะดวกสำหรับเพื่อการถามคำถามจากผู้เล่นที่ชำนาญ โดยเหตุนี้ผู้เล่นมือสมัครเล่นบางทีอาจได้รับคำหารือสำหรับในการเล่นเกม ช่วยทำให้คุณสร้างห้องสนทนาที่คุณสามารถเพิ่มผู้เล่นคนจำนวนไม่น้อยและก็สนุกสนานกับเกมในกรุ๊ปได้
สล็อตยังมีหนังสือคู่มือที่คุณสามารถค้นหาสูตรต่างๆเพื่อเล่นเกม
สูตรการเล่นไฮโล
ทำตามอย่างสูตรที่กล่าวไว้เพื่อชนะเกมไฮโล
ขั้นตอนแรก ให้รู้จักอัตราต่อรองที่คุณสามารถวางเดิมพันได้ ค้นหาการเสมอสูงสุดรวมทั้งจัดการวางเดิมพัน
แม้คุณติดอยู่ในเกมเป็นประจำให้เปลี่ยนแปลงตำแหน่งของคุณในคะแนนที่ดึงมาจากตารางบ่อยๆเพื่อเพิ่มจังหวะสำหรับการชนะพนัน
คุณสามารถใช้สูตรการพนันแบบทบต้นได้ถ้าคุณรู้สึกต้องการแพ้ในเกม วิธีการแบบนี้มีคุณภาพสำหรับเพื่อการสร้างทุนที่สูญเสียไปใหม่โดยไม่นึกถึงการพนันที่คุณวาง
คุณสามารถใช้สูตร shove the stick เพื่อเล่นเกมได้โดยสวัสดิภาพ แนวทางนี้จะปกป้องไม่ให้ท่านตีเกมในตอนแรก
สูตรการเล่นสล็อต
นี่เป็นแนวทางการเล่นสล็อต
ตรวจตราตู้ก่อนวางเดิมพัน คุณควรจะทราบว่าตู้ด้านล่างมีเงินปริมาณน้อยพร้อมโบนัสอย่างมากมาย ในเวลาที่ตู้ที่สูงกว่าจะมีเงินรางวัลก้อนโตพร้อมโบนัสน้อยกว่า
คุณสามารถเริ่มเกมด้วยตู้ที่สูงขึ้นเพื่อคิดเงินรวมทั้งย้ายไปที่ตู้ที่ต่ำกว่าเพื่อรับรางวัล
เก็บข้อมูลทั้งปวงเกี่ยวกับเหตุการณ์การเล่นเกมเพื่อเลี่ยงการพนันที่เสี่ยง
ระบุจำนวนเงินแผนการที่คุณคาดว่าจะชนะก่อนวางเดิมพัน แนวทางแบบนี้จะคุ้มครองไม่ให้ท่านแพ้เกม

joker said...

Slots free credits can be used to play online slots games immediately without a vest. สล็อต

PG

profressional said...

UFABET เว็บพนันออนไลน์ ดีที่สุด แทงบอลออนไลน์ ฝาก-ถอนไม่มีขั้นต่ำ

kin said...

pg slot เว็บสล็อตตรงที่คุณสามารถเชื่อใจได้ ด้วยการจัดการงานด้านเกมการเดิมพันมามากยิ่งกว่า 20 ปี PG SLOT พร้อมการบริการจากบุคลากรที่มีประสบการณ์มาโดยตรง เกมสล็อตออนไลน์รูปแบบใหม่ที่คุณชอบ pg slot เว็บสล็อต

Service Apartments Gurgaon said...

Wow, such an awesome blog you have written there and you and I get exactly what information I am looking for, in the third paragraph you put amazing effort to explain the theme of the content. As a content writer, I can understand efforts because when students ask me for sitxglc001 assessment answers, I do the same.

Assignment Help said...

This article is quite interesting and I am looking forward to reading more of your posts. Thanks for sharing this article with us.
Psychology Assignment Help Australia
Assignment Help Online

fahsai168 said...

ถ้าไม่ดีคงไม่มีใครอยากเล่นสนใจเล่นสลอดออนไลน์คิดเลยเริ่มต้นเล่นวันนี้สมัครฟรีรับยุทธและเครดิตฟรีมากมายไม่ว่าจะเป็นฟรีสปินหรือแม้กระทั่งโบนัสช่วงเวลาโบนัส pg slot เว็บตรง

tot123 said...

นอกจากงานประจำที่ได้เงินเดือนแล้วก็มีเกมออนไลน์จากเว็บนี่แหละที่ทำให้เรามีรายได้เพิ่มมากขึ้น นอกจากเกมจะเล่นง่ายแล้ว ยังแตกง่ายมากๆอีกด้วยวันไหนดวงดีหน่อยก็แตกตั้งแต่ตาแรกกันเลยเจอแบบนี้จะให้เลิกเล่นได้ไงมาทดลองเล่นกันเลย สโบเบ็ตมือถือ

Assignment Help said...

This article is quite interesting and I am looking forward to reading more of your posts. Thanks for sharing this article with us.

PUBH631 Planning Implementation And Evaluation

Online Assignment Help Australia said...

Looking For Assignment Help Brisbane or Matlab Assignment Help? Connect with Myassignmenthelp Phd Experts and Secure A+ Grade

Angel17 said...

Such an awesome and cool post. Thanks for sharing! https://goo.gl/maps/5suTCTMu9pihdaT79

Unknown said...

I don't have any ideas that links are made like this, Now I am working with the air freight service which is working day and night. tought playing a vital role for the GDP of any country.