Reputation: 173
I can't start signalR Client Window service .its through exception that .
The Service 1 service on Local-computer started then stopped. Some Service Stop Automatically If they're not in use by other service or program
Note : my server is running on localhost and client also on local host
My code is here :
IDisposable SignalR { get; set; }
private System.Diagnostics.EventLog eventLog1;
private String UserName { get; set; }
private IHubProxy HubProxy { get; set; }
const string ServerURI = "http://*:8080/signalr";
private HubConnection Connection { get; set; }
public Service1()
{
InitializeComponent();
}
private async void ConnectAsync()
{
try
{
Connection = new HubConnection(ServerURI);
HubProxy = Connection.CreateHubProxy("MyHub");
//Handle incoming event from server: use Invoke to write to console from SignalR's thread
HubProxy.On<string, string>("AddMessage", (name, message) =>
{
eventLog1.WriteEntry(string.Format("Incoming data: {0} {1}", name, message));
});
ServicePointManager.DefaultConnectionLimit = 10;
eventLog1.WriteEntry("Connected");
await Connection.Start();
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.ToString());
//No connection: Don't enable Send button or show chat UI
return;
}
//Activate UI
eventLog1.WriteEntry("Connected to server at " + ServerURI + Environment.NewLine);
}
protected override void OnStart(string[] args)
{
string abd = "Tariq";
ConnectAsync();
HubProxy.Invoke("Send", UserName, abd);
}
Upvotes: 1
Views: 238
Reputation: 456677
Your OnStart
code is assuming that the async void
method has completed. It appears that HubProxy.Invoke
is throwing an exception because it hasn't connected yet.
If this is confusing to you, I recommend reading my async
intro blog post, and also using my AsyncContextThread
type for asynchronous Win32 services. Then you can more properly avoid async void
:
private AsyncContextThread _mainThread = new AsyncContextThread();
protected override void OnStart(string[] args)
{
_mainThread = new AsyncContextThread();
_mainThread.Factory.Run(async () =>
{
string abd = "Tariq";
await ConnectAsync();
HubProxy.Invoke("Send", UserName, abd);
});
}
private async Task ConnectAsync()
{
...
}
Upvotes: 1