I am working on an C# WCF project and I have got it pretty much working except for quite a big but hopefully simple problem.
The WCF service is hosted from within my Console application and my console application calls a function to a different class to open the connection for the WCF service.
However, if the last line of the function is host.open(); the function call then finishes to the connection gets closed and the service can no longer be used. However, if I put Console.ReadLine() after the host.open then the service stays open and I can use it but obviously the rest of the flow of the program no longer runs.
Below is the code I am using to open the host connection.
public void startSoapServer()
{
string methodInfo = classDetails + MethodInfo.GetCurrentMethod().Name;
if (String.IsNullOrEmpty(Configuration.soapServerSettings.soapServerUrl) ||
Configuration.soapServerSettings.soapPort == 0)
{
string message = "Not starting Soap Server: URL or Port number is not set in config file";
library.logging(methodInfo, message);
library.setAlarm(message, CommonTasks.AlarmStatus.Medium, methodInfo);
return;
}
//baseAddress = new Uri(string.Format("{0}:{1}", Configuration.soapServerSettings.soapServerUrl,
// Configuration.soapServerSettings.soapPort));
baseAddress = new Uri("http://localhost:6525/hello");
using (ServiceHost host = new ServiceHost(typeof(SoapServer), baseAddress))
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Opened += new EventHandler(host_Opened);
host.Faulted += new EventHandler(host_Faulted);
host.Open();
Console.ReadLine();
}
Without the Console.ReadLine() there the function finishes so the connection closes. How can I keep the host open for the duration that the C# is app is running.
This function call is called from within the Main method halfway through initiliasing some stuff within the console stuff.
Thanks for any help you can provide.