I have a Windows Service project in whose OnStart method, I am calling a function from a reference WCF Service like this -
var factory = new ChannelFactory<MySolution.IDataService>("BasicHttpBinding_IDataService");
MySolution.IDataService client = factory.CreateChannel();
Task.Factory.StartNew(() => client.GetData()).Wait();
After hosting this, when I try to start the service, it is throwing error that it "was started and then stopped". I debugged and found that the last line (with Wait call) is causing the service to break. What can be the reason of this? How to fix it?
Thanks in advance.
Edit1
As suggested, I moved the GetData code to another function which is called with a timer -
OnStart(){
....
....
System.Timers.Timer _timer = new Timer(60 * 1000);
_timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
_timer.Start();
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Task.Factory.StartNew(() => client.GetData()).Wait();
}
Now service is starting but the timer_Elapsed function is not invoked, or may be it is but the function is again failing. The function works fine in debug mode. Can you tell if this is the correct way?