简体   繁体   中英

ClientBase: How do I wait for it to finish opening?

I built a Client class that inherits from the ClientBase class as part of my WCF Client. I am trying to figure out how to ensure that the Connection does not need to be re-established. I did see the CommunicationState enumeration has the stuff I need, along with the ClientBase.State, however I'm not sure how to get it to wait if it's in the Opening state, or something similar... Basically if it's Opened and not locked I'm good to go, but if it's doing something like Opening or Created or whatnot, I don't wanna have to create a new one. Alternatively, is there any way to remove the Timeout all-together and just have it close the connection when the application closes?

private static MyClient _client;
public static MyClient Client
{
    get
    {
        if (_client != null)
        {
            lock(_client)
            {
                //How do I ensure that I can return a MyClient that is in Opened state??
                //If it's doing something like Opening I don't wanna create a new one...
            }
        }
    }
}

From the MSDN on CommunicationObject (what ClientBase is based on) it is safe to call Open() on a already open or opening connection. It will pass through on an already open connection or wait for for the connection to complete opening if it is not done yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM