Posted by: rcosic | 25/09/2012

Handling Lync Client 2010 invalid state

today I’ll talk about one issue with Lync Client API you already might come across in doing initialization of LyncClient object in your custom app.

The problem appears when you handle the LyncClient object reference and, for some reason (albeight communicator is gracefully ended or focibly shut down), the object become ‘unavailable’. If you subscribed to the ClientDisconnected event, it will be fired now. This would be the indication to try to initialize the object again when Lync is back, since the reference becomes useless. Actually, if you try to initialize the object again with:

m_lyncClient = null;
m_lyncClient = LyncClient.GetClient();

you will get either: an exception of one of the lync’ defined types (e.g. ClientNotFoundException), or: the client object will go into the Invalid state. This latter case seems to be the problem to handle.

This case arises when you handle Lync client object on another threads (i.e. worker threads), which is quite normal, since polling for Lync connection via timer and/or operating with Lync client API via wrapper class acting like a ‘model’ (in case of MVVM) is usual thing to do. In this case, Lync Client API doesn’t know how to find the right reference and it just goes into invalid state. The resolution is to find the right thread from where the original reference is created, and this would be in most cases, the very main (UI) thread. If you use Lync controls or start to call the API from some UI element, this would be the thread from where you started to use the API. So, the only thing to do in this case, is to check for the invalid state (a suitable place would be in your re-initialization code) and dispose all resources bound to API, together with the object itself (it doesn’t have a Dispose method, so you just set it to a null value):

try
{
m_lyncClient = null;
m_lyncClient = LyncClient.GetClient();

// we must check the invalid state, since this is the indication of lost reference and malicious lync object
if (m_lyncClient.State == ClientState.Invalid)
{
// Note: disposing of the original reference must happen on the main thread!
App.Current.Dispatcher.Invoke(new Action(() => {

// release main Lync API object (next time, correct reference will be created)
Dispose();
}));
}
else
{
SubscribeToLyncEvents();
}
}
catch (ClientNotFoundException cnfEx)
{

}
catch (COMException cEx)
{

}
catch (NullReferenceException nrEx)
{

}

After you’ve done that, initialize the API object again and you’ll be good to go.

Kind regards,
Ratko.

About these ads

Responses

  1. Hi Ratko, thanks for the details.

    I am having a similar problem here.

    If I crash the communicator process (ending process from the task manager) and start it again, the next instance that I get from LyncClient.GetClient() method is invalid (my app keeps running while crashing the client).

    Even after cleaning the reference and waiting several seconds, I am still getting the instance with the invalid state. It is like it never returns the new one.

    I am not disposing the parent object because I can’t, but I am setting the lync client variable to null before calling GetClient() again.

    Am I missing something? I need to tell somehow the lync client “go and get the new instance!”

    Thanks a lot!

    • The source of this problem is that LyncClient object is not thread safe. So, you have to pay attention to the calling thread. Try to find the very thread that called the instance for the first time, and call the object disposal from it. I hope it helps.

      KR,
      Ratko.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: