WCF ServiceHost shuts down when not in console application
-
Hi, I am just starting to learn WCF. In all the examples I've seen so far, the SerciveHost was initialized inside a console application and remained listening until the console was closed. I am trying to have it initiated from inside a winform app. I have a button which triggers this method: private void StartServer() { using (host = new ServiceHost(typeof(DataBase.DataBaseService ), new Uri("http://localhost:8000/DataBaseService"))) { host.AddServiceEndpoint(typeof( DataBase.IDataBaseService), new BasicHttpBinding(), "DataBaseService"); host.Open(); } } As soon as the method reaches the end, host.Status is set to Closed. Is there any way to stop this?
-
Hi, I am just starting to learn WCF. In all the examples I've seen so far, the SerciveHost was initialized inside a console application and remained listening until the console was closed. I am trying to have it initiated from inside a winform app. I have a button which triggers this method: private void StartServer() { using (host = new ServiceHost(typeof(DataBase.DataBaseService ), new Uri("http://localhost:8000/DataBaseService"))) { host.AddServiceEndpoint(typeof( DataBase.IDataBaseService), new BasicHttpBinding(), "DataBaseService"); host.Open(); } } As soon as the method reaches the end, host.Status is set to Closed. Is there any way to stop this?
The problem you're facing here is that you are disposing of the host immediately after opening it. What you might want to do here is to make host a member instead, then initialise it in StartServer. In your Form_Closing method, you would want to close host.
private _host = null;
private void StartServer()
{
_host = new ServiceHost(typeof(DataBase.DataBaseService ), new Uri("http://localhost:8000/DataBaseService"));
_host.AddServiceEndpoint(typeof(DataBase.IDataBaseService),
new BasicHttpBinding(), "DataBaseService");
_host.Open();
}private void StopServer()
{
if (_host != null)
{
_host.Close();
_host.Dispose();
}
}Call StopServer in the Form_Closing event.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
-
The problem you're facing here is that you are disposing of the host immediately after opening it. What you might want to do here is to make host a member instead, then initialise it in StartServer. In your Form_Closing method, you would want to close host.
private _host = null;
private void StartServer()
{
_host = new ServiceHost(typeof(DataBase.DataBaseService ), new Uri("http://localhost:8000/DataBaseService"));
_host.AddServiceEndpoint(typeof(DataBase.IDataBaseService),
new BasicHttpBinding(), "DataBaseService");
_host.Open();
}private void StopServer()
{
if (_host != null)
{
_host.Close();
_host.Dispose();
}
}Call StopServer in the Form_Closing event.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
I did have host as a member. I think the problem was using the 'using' statement. After looking at your code I dropped it and it works so thanks a lot.
-
I did have host as a member. I think the problem was using the 'using' statement. After looking at your code I dropped it and it works so thanks a lot.
Not a problem. For your information - the using statement here triggers a Dispose when the code reaches the end of the code block (which was the point I was trying to make).
"WPF has many lovers. It's a veritable porn star!" - Josh Smith