Hello!

You know you’re one older person, when it’s your turn to clean the remains of your children diapers. This is not worrisome because since we are born until we die, we are basically a crap factory. The problem is that this one is a physiological obligation. Quite another stuff is when you stuck with an shit idea between both ears and this idea ends up out in the form of code.

That said, 2 points which give cause for today’s post

  1. It is possible to host SignalR in one application, for example app console, service windows, etc.
  2. With the eternal problem of the app A needs to communicate bi-directionally with the app B, there are privileged minds, who decide that both apps must host a SignalR hub and then… well now imagine.

But well, better back to point 1. Below I leave a couple of links that are helpful and which explain a step by step complete. In my case, I leave it in 4 steps:

1. Create a console app

2. Add NuGet 2 packages: Microsoft.AspNet.SignalR.SelfHost, and Microsoft.Owin.Cors

3. Modify the source code of console with a code similar to the following

   1: using System;

   2: using Microsoft.AspNet.SignalR;

   3: using Microsoft.Owin.Hosting;

   4: using Owin;

   5: using Microsoft.Owin.Cors;

   6:  

   7: namespace SignalRHost

   8: {

   9:     internal class Program

  10:     {

  11:         private static void Main(string[] args)

  12:         {

  13:             const string url = "http://localhost:50000";

  14:             using (WebApp.Start(url))

  15:             {

  16:                 Console.WriteLine("Server running on {0}", url);

  17:                 Console.ReadLine();

  18:             }

  19:         }

  20:     }

  21:  

  22:     class Startup

  23:     {

  24:         public void Configuration(IAppBuilder app)

  25:         {

  26:             app.UseCors(CorsOptions.AllowAll);

  27:             app.MapSignalR();

  28:         }

  29:     }

  30:     public class MyHub : Hub

  31:     {

  32:         public void Send(string message)

  33:         {

  34:             Console.WriteLine("message from:" + Context.ConnectionId + " - message:" + message);

  35:             Clients.All.sendMessage(message);

  36:         }

  37:     }

  38: }

4 Done!

We already have a hub called “MyHub” hosted in a console app. It leaves traces of Send messages, similar to the following image.

image

The possibilities are very large. Personally would not opt for using SignalR as the primary mechanism for communicating apps, but if I have to thank for reaching the package [Microsoft.AspNet.SignalR.SelfHost ]

By the way, I almost forgot to let a code example of a WPF client that uses this “server”, here goes:

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using System.Text;

   5: using System.Threading.Tasks;

   6: using System.Windows;

   7: using System.Windows.Controls;

   8: using System.Windows.Data;

   9: using System.Windows.Documents;

  10: using System.Windows.Input;

  11: using System.Windows.Media;

  12: using System.Windows.Media.Imaging;

  13: using System.Windows.Navigation;

  14: using System.Windows.Shapes;

  15: using Microsoft.AspNet.SignalR.Client;

  16:  

  17: namespace WpfApplication2

  18: {

  19:     /// <summary>

  20:     /// Interaction logic for MainWindow.xaml

  21:     /// </summary>

  22:     public partial class MainWindow : Window

  23:     {

  24:         private HubConnection _connection;

  25:         private IHubProxy _myHub;

  26:  

  27:         public MainWindow()

  28:         {

  29:             InitializeComponent();

  30:             Loaded += MainWindow_Loaded;

  31:         }

  32:  

  33:         void MainWindow_Loaded(object sender, RoutedEventArgs e)

  34:         {

  35:             _connection = new HubConnection("http://localhost:50000/");

  36:             _myHub = _connection.CreateHubProxy("MyHub");

  37:             _connection.Start().Wait();

  38:         }

  39:  

  40:         private void Button_Click(object sender, RoutedEventArgs e)

  41:         {

  42:             _myHub.Invoke("Send", "message");

  43:         }

  44:     }

  45: }

Saludos @ Home

El Bruno

image image image Google

Leave a comment

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading