locked
Error Using "Windows Service" as Client SignalR (Server in Windows Service Self Hosted) RRS feed

  • Question

  • User2129799919 posted

    Someone has some sample code of a signalr connection established (not using localhost as Listener) using as a Server and as a Client Windows Service type applications?

    I have a solution running smoothly while the client is as a Console, but once transformed into service, the connection does not work.

    Apparently the problem is the Client, because the server is connected through any test with other applications, debug server (Console) or release (Windows Service), but the Client as a Windows Service on the Server does not connect at all.

    Any Tips?

    Victor Perez

    Thursday, May 4, 2017 8:47 PM

Answers

  • User2129799919 posted

    Thanks for the attention Dillion.

    But as I wrote in the post, in my case, both client and server are Windows Services, and there is no Client Java Script.

    All worked with "localhost" as Listener address, the problem occurred only when the customer was encapsulated in a Windows Service.

    Installing the service on a computer without Antivirus (Bitdefender), everything works normally.

    Anyway, thank you very much.

    Victor Perez

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 5, 2017 1:41 PM

All replies

  • User-271186128 posted

    Hi Victor Perez,

    According to your issue description, I think you want to build a SignalR Server host on windows service.

    step 1: you should create a windows service project to host signalr.

    you can refer this link how to create windows service
    https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx

    (1) service entry .

    static class Program
     {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main()
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    //the service which to run 
                    new SignalRChat() 
                };
                ServiceBase.Run(ServicesToRun);
            }
    }
    

    (2) SignalRChat.cs ,this is the service of SignalR.

     public partial class SignalRChat : ServiceBase
     {
            IDisposable webApp = null;
            public SignalRChat()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                //start owin host when windows service start
                webApp = WebApp.Start<Startup>("http://localhost:8080");
            }
    
            protected override void OnStop()
            {
                //dispose webapp when windows service stop
                webApp.Dispose();
            }
       }
    

    (3)Startup.cs. this is the entry of owin which init signalr.

    public class Startup
    {
            public void Configuration(IAppBuilder app)
            {
                //init signalr
                app.MapSignalR();
            }
    }
    

    (4)MyHub.cs. this is the implement of hub which what you want to do

    public class MyHub : Hub
        {
            public void Send(string name, string message)
            {
                Clients.All.addMessage(name, message);
            }
        }
    
    

    (5) compile this project and install this windows service.

    if you don't know how to install windows service, this link maybe help you
    https://msdn.microsoft.com/en-us/library/sd8zc8ha(v=vs.110).aspx

    step 2: when you want to invoke on client with reference signalR script ,you should notice the signalR address is appoint to then endpoint of http://localhost:8080 ,this address is based on WebApp.Start<Startup>("http://localhost:8080") in previous section.

     <!--Reference the jQuery library. -->
     <script src="Scripts/jquery-1.10.2.min.js"></script>
     <!--Reference the SignalR library. -->
     <script src="Scripts/jquery.signalR-2.2.1.min.js"></script>
     <!--Reference the autogenerated SignalR hub script. -->
     <script src="http://localhost:8080/signalr/hubs"></script>
    

    and you should change the hub url of connection in javascript before use.

    <script type="text/javascript">
        $.connection.hub.url = "http://localhost:8080/signalr";
        var chat = $.connection.myHub;
        chat.client.addMessage = function (name, message) {
             //.................
        }
         $.connection.hub.start().done(function(){
            //..................
        })
    </script
    

    Other places are the same as normal use.

    you can also refer this link, it might help you.

    https://code.msdn.microsoft.com/SignalR-self-hosted-in-6ff7e6c3

    If you have any other questions, please feel free to contact me any time.

    Best regards,
    Dillion

    Friday, May 5, 2017 8:36 AM
  • User2129799919 posted

    Thanks for the attention Dillion.

    But as I wrote in the post, in my case, both client and server are Windows Services, and there is no Client Java Script.

    All worked with "localhost" as Listener address, the problem occurred only when the customer was encapsulated in a Windows Service.

    Installing the service on a computer without Antivirus (Bitdefender), everything works normally.

    Anyway, thank you very much.

    Victor Perez

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 5, 2017 1:41 PM