internal SessionRegistrationCompletedEventArgs(SessionRequest request, SessionResult result)
 {
     Request = request;
     Result = result;
 }
Example #2
0
        private async Task<SessionResult> RegisterEx(string projectApiKey)
        {
            if (!Client.Configuration.Enabled)
            {
                return null;
            }

            var result = new SessionResult();
            SessionRequest request = null;
            SessionResponse response = null;

            lock (_syncRoot)
            {
                if (_ongoingSessionRegistration)
                {
                    var waitTime = new TimeSpan(0, 0, 3, 0);
                    if (!_sessionRegistered.WaitOne(waitTime))
                    {
                        if (string.IsNullOrEmpty(_sessionKey))
                        {
                            throw new TimeoutException("Done waiting for another thread trying to get the session registered.").AddData("WaitSeconds", waitTime.TotalSeconds.ToString("0"));
                        }
                    }
                }
                _ongoingSessionRegistration = true;
            }

            try
            {
                if (!string.IsNullOrEmpty(_sessionKey))
                {
                    result.SetAlreadyRegistered();
                }
                else
                {
                    try
                    {
                        request = new SessionRequest
                        {
                            ProjectApiKey = projectApiKey,
                            ClientStartTime = DateTime.UtcNow,
                            Environment = Environment,
                            Application = Client.Information.Application.GetApplicationData(),
                            Machine = Client.Information.Machine.GetMachineData(),
                            User = Client.Information.User.GetDataUser(),
                        };

                        OnSessionRegistrationStartedEvent(new SessionRegistrationStartedEventArgs(request));

                        response = await Client.WebApiClient.CreateAsync<SessionRequest, SessionResponse>("Client/Session", request);

                        if (response.SessionKey == null) throw new InvalidOperationException("No session key returned from the server.");
                        _sessionKey = response.SessionKey;
                        _sessionUrl = response.SessionUrl;
                    }
                    catch (Exception exception)
                    {
                        result.SetException(exception);
                        throw;
                    }
                    finally
                    {
                        result.SetCompleted(response);
                        LastSessionRegistrationCompletedEventArgs = new SessionRegistrationCompletedEventArgs(request, result);
                        OnSessionRegistrationCompletedEvent(LastSessionRegistrationCompletedEventArgs);
                    }
                }
            }
            finally
            {
                _ongoingSessionRegistration = false;
                _sessionRegistered.Set();
            }

            return result;
        }