Pregunta

I was just tidying up some WCF (self-hosted) code of mine and realised that my ServiceHost object was going out of scope after it had been initially setup, but I could still connect to it.

public void StartMyService()
{
    var host = new ServiceHost(typeof(MyService), new Uri("http://localhost:8000/MyService"));
    host.Open();
}

If I call this again and to reinstantiate it and try opening it again, I get an error saying there is already something connected to the endpoint. It only gets released now when the application shuts down.

I can only assume that the call to Open() does something at a lower level to keep it open in a now unmanaged way. However, I can't find any information on this unless I'm just totally misinterpreting things here.

And thinking about it, this really doesn't seem right that the connection is now still open but I have no way to get back to it (to close etc) without killing the app. Or maybe this doesn't even matter, I can just leave it open all the time if it's not harming anything?

UPDATE: So I've accepted Eiver's answer as it explains my original question as to why it stays open even after going out of scope. However, I'm now wondering a couple of things about this (or maybe my inexperience with threading). Firstly, am I bothered about this cos as long as it's open that's all that matters right. And if there is some way of accessing the thread by keeping it in scope, would it even help. Maybe, setting it up like this (and never calling close) makes the calling code simple (i.e. don't have to hold onto the reference) and I could even wrap it up in a static method for even simpler calls. All sounds a bit dodge right....hmmmm!

UPDATE 2: So, I knew doing it this way wasn't good as it meant I no longer had control over the host and the thread that it was started on. So I have wrapped it all as a Singleton, giving me the benefit of being able to access the host at any point (for errors, events, close/reopen on same endpoint etc) and making the call extremely simple (like a simple static method call).

¿Fue útil?

Solución

Not everything that goes out of scope is terminated. If a class spawns another thread (and I am sure WCF does so), the thread might live even if the reference to a class that created it was garbage collected.

You might have to call host.Close()

Otros consejos

My guess is the Class with the method StartMyService() is still alive. If you code it like this it does what (I think) it is supposed to do. Left the commented out code as I think that is a cleaner way of starting a service and letting it go out of scope.

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("**** Console Based WCF Host *****");

            //using (ServiceHost serviceHost = new ServiceHost(typeof(MagicEightBallService)))
            //{
            //    serviceHost.Open();
            //    Console.WriteLine("The service is running");
            //    Console.ReadLine();
            //}
            using (MySvc ms = new MySvc())
            {
                ms.StartMyService();
            }
            Console.ReadLine();
        }

        public class MySvc: IDisposable
        {
            ServiceHost serviceHost;
            public void StartMyService()
            {
                serviceHost = new ServiceHost(typeof(MagicEightBallService));
                serviceHost.Open();
            }
            public void Dispose() 
            {
                // the class needs to clean up after itself
                serviceHost.Close();
            }
            public MySvc() { }
        }
    }

Maybe it's not getting collected before you try to access it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top