Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open close ServiceHost multiple times

Tags:

c#

wcf

I am trying to figure out how to open and close a ServiceHost object multiple times in a self-hosted WCF app:

what I did: 1. create ServiceHost object

  1. call Open()
  2. call Closed() so far so good.
  3. call Open() again, exception: Cannot access a disposed object, which is because CommunicationObject is disposed , makes perfect sense.

How can I open the ServiceHost again in this case without recreating that object again (rather, I'd recreate CommunicationObject if it is possible).

Thanks in advance.

like image 349
Yuan Avatar asked Oct 06 '11 01:10

Yuan


1 Answers

ServiceHost inherits from CommunicationObject. ServiceHost is the CommunicationObject throwing the exception. Once a CommunicationObject is Closed it cannot be reopened. You'll have to create a new ServiceHost.

The CommunicationState state machine:

Every object starts in the Created state and proceeds in a one-way progression through the Opening, Opened, Closing, and Closed states.

like image 111
ErnieL Avatar answered Sep 25 '22 00:09

ErnieL