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.
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.
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.
If you have any other questions, please feel free to contact me any time.
Best regards,
Dillion
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
4 Posts
Error Using "Windows Service" as Client SignalR (Server in Windows Service Self Hosted)
May 04, 2017 08:47 PM|VictorPerez2911|LINK
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
All-Star
45489 Points
7008 Posts
Microsoft
Re: Error Using "Windows Service" as Client SignalR (Server in Windows Service Self Hosted)
May 05, 2017 08:36 AM|Zhi Lv - MSFT|LINK
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 .
(2) SignalRChat.cs ,this is the service of SignalR.
(3)Startup.cs. this is the entry of owin which init signalr.
(4)MyHub.cs. this is the implement of hub which what you want to do
(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.
and you should change the hub url of connection in javascript before use.
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
None
0 Points
4 Posts
Re: Error Using "Windows Service" as Client SignalR (Server in Windows Service Self Hosted)
May 05, 2017 01:41 PM|VictorPerez2911|LINK
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