views:

554

answers:

1

Hello, a new member here. Nice to see such a neat community.

After a bit of research, I decided to use WCF in my application to do inter process communication, so I am using the NetNamedPipeBinding binding.

The ServiceHost hosting application is not a dedicated server, so it has to spawn the ServiceHost via a thread. So far so good.

So I have something like the following:

Foo()
{
    Thread serverThread = new Thread(new ThreadStart(ServerThread));
    serverThread.Start();
    Console.WriteLine("Foo Exited");
}

ServerThread()
{
   Uri baseAddress = new Uri("net.pipe://localhost/service");
   ServiceHost serviceHost = new ServiceHost(typeof(MyService), baseAddress);
   ...
   serviceHost.Open();
   Console.WriteLine("Server Thread Exited");
}

So as expected, I see:

->   Server Thread Exited
->   Foo Exited

But to my surprise, even though the thread the server is running on has excited, the client can still connect to the serviceHost and the service host processes the request properly!

So how come the ServiceHost is still processing and treating requests even though it's main thread (the one it was created on) is dead?

Also is there a better way to keep the ServerThread alive then a while(true){Thread. Sleep(100);}?

Thanks.

+5  A: 

When you call Open on the ServiceHost, an additional thread will be created to listen for incoming service requests. In this way, your thread may have finished running, but another thread has been created, and will continue to run until you call "Close" on the ServiceHost.

It may not be necessary in your case to spawn off a thread yourself. Just Open your ServiceHost in the application's main thread. You can then do other things in your main thread, and when you're ready to kill the host, just call serviceHost.Close().

Here's a pretty good description I found:

http://www.code-magazine.com/article.aspx?quickid=0701041&page=1

Andy White
This is the right answer... I'm out of votes for the day tho ;)
TheSoftwareJedi
Well, something is telling that it is not as straightforward as it seems. I jut wrote a simple program that did not spawn a thread. In this simple porgram, I created the ServiceHost and Opened() it. Right after, I created a client that connects to the service host. And the client fails (I am guessing it deadlocks).If I create that ServiceHost on a seperate thread (even if the thread exits) the client call succeeds.So why would the client call fail on same thread if the Open() has already spawned the processing threads?
Futurist
You should be able to do this, I just wrote a little test program that does what you describe and it worked. What's the error that you're getting? It could be that you don't have your client configured in your app.config system.serviceModel section.
Andy White
Hi Andy, the client call just stalls and never comes back and the program becomes "non responsive" to Windows. The configuration is proper as the exact same piece of code works (client connects and is able to call service just fine) if the _only_ change I do is to create the ServiceHost in a new thread instead of the main thread (everything else remains the same).
Futurist
Interesting... I'm not sure what's going on. Maybe you could post a code snippet and your config file.
Andy White
The problem is that these little comment boxes are too tiny :)Is there a way to post a "normal" (non comment) rich-text reply without it being an "Answer Your Question"?
Futurist
You could edit your question, and add the additional info there.
Andy White
OK Andy...I tried the code that didn't work earlier in a stand-alone C# exe, and client succeeded in calling the server on the same thread. Earlier, I was trying that code on a thread on a DLL loaded by some application (like a plugin DLL)...which was failing. I wonder what difference does it make if the server and client are on same thread in a DLL vs standalone exe.
Futurist