LyncClient stuck in Sign In loop

1.4k views Asked by At

I'm making "my own lync client". I've followed the several examples online about how to sign a lync client in using Ui Suppression mode. I can get the client to sign in but then it immediately loops through signing out and signing in.

What causes this?

2014-11-12 08:18:30.4381|DEBUG|LyncWpf.MainWindow|InitClient
2014-11-12 08:18:31.2021|DEBUG|LyncWpf.MainWindow|InitializeCallback
2014-11-12 08:18:31.2131|DEBUG|LyncWpf.MainWindow|StateChanged Uninitialized to Initializing
2014-11-12 08:18:31.2131|DEBUG|LyncWpf.MainWindow|StateChanged Initializing to SignedOut
2014-11-12 08:18:31.4221|DEBUG|LyncWpf.MainWindow|StateChanged SignedOut to SigningIn
2014-11-12 08:18:51.8631|DEBUG|LyncWpf.MainWindow|SigninCallback
2014-11-12 08:18:52.0151|DEBUG|LyncWpf.MainWindow|StateChanged SigningIn to SignedIn
2014-11-12 08:18:52.0211|INFO|LyncWpf.MainWindow|signed in!
2014-11-12 08:18:52.5451|DEBUG|LyncWpf.MainWindow|StateChanged SignedIn to SigningOut
2014-11-12 08:18:52.5591|DEBUG|LyncWpf.MainWindow|StateChanged SigningOut to SigningIn
2014-11-12 08:18:53.6051|DEBUG|LyncWpf.MainWindow|StateChanged SigningIn to SignedIn
2014-11-12 08:18:53.6071|INFO|LyncWpf.MainWindow|signed in!
...repeated forever

Code sample. Notice I don't call BeginSignIn again.

    private void _client_StateChanged(object sender, ClientStateChangedEventArgs e)
    {
        this.Log().Debug("StateChanged {0} to {1}", e.OldState.ToString(), e.NewState.ToString());
        switch (e.NewState)
        {
            case ClientState.SignedIn:
                this.Log().Info("signed in!");
                break;
            case ClientState.SignedOut:
                if (e.OldState == ClientState.Initializing)
                    ((LyncClient) sender).BeginSignIn(null, null, null, SigninCallback, sender);
                break;
        }
    }
    private void InitClient()
    {
        this.Log().Debug("InitClient");
        _client = LyncClient.GetClient(true);
        if (_client.InSuppressedMode)
        {
            _client.StateChanged += _client_StateChanged;
            if (_client.State == ClientState.Uninitialized)
            {
                Object[] asyncState = { _client };
                _client.BeginInitialize(InitializeCallback, asyncState);
            }
        }
    }
    private void InitializeCallback(IAsyncResult ar)
    {
        this.Log().Debug("InitializeCallback");
        if (ar.IsCompleted)
        {
            object[] asyncState = (object[])ar.AsyncState;
            ((LyncClient)asyncState[0]).EndInitialize(ar);
        }
    }
    private void SigninCallback(IAsyncResult ar)
    {
        this.Log().Debug("SigninCallback");
        if (ar.IsCompleted)
        {
            try
            {
                ((LyncClient)ar.AsyncState).EndSignIn(ar);
            }
            catch (RequestCanceledException re)
            {
                this.Log().Warn("SignIn Request Cancelled {0}", re.Message);
                throw; 
            }
        }

    }
1

There are 1 answers

1
Shane Powell On BEST ANSWER

I had the same problem on the second run of the application. In my case, it was because the Lync 2013 shutdown api doesn't work correctly and leaves a copy of lync in memory. When you run your app for the second time, it creates a new instance of ui suppressed mode lync and the two instances seem to fight with each to login.

I found the only way to fix this was to kill any background lync processes (i.e. has no main window handle).