public UserResponse CountWordsInStatement(string statement, bool removePunctuation)
        {
            var request = new UserRequestWrapper {
                Statement = statement, RequestReceivedAt = DateTime.Now, RemovePunctuation = removePunctuation
            };

            try
            {
                AddUserRequest(request);

                if (!request.WaitForRequestToComplete.WaitOne(-1))
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "Timed out waiting for request id {1} being processed",
                                  request.Id));
                }

                return(request.ToUserResonse());
            }
            catch (Exception ex)
            {
                return(new UserResponse {
                    Id = request.Id, HasError = true, Error = ex.ToString()
                });
            }
        }
        internal void AddUserRequest(UserRequestWrapper userRequest)
        {
            // If we're in shutdown mode and destroy semaphore at this point
            // the requests being served just return an error,
            // but the service still will exit gracefully
            _servedUserRequests.WaitOne();

            lock (_activeUserRequests)
            {
                if (_activeUserRequests.ContainsKey(userRequest.Id)) // this can never happen, but just in case
                {
                    throw new InvalidOperationException(string.Format("Request id {0} already exists", userRequest.Id));
                }

                _activeUserRequests.Add(userRequest.Id, Task.Factory.StartNew(new Action <object>(o =>
                {
                    RunUserRequest(o);
                }), userRequest));
            }
        }
        public UserResponse CountWordsInStatement(string statement, bool removePunctuation)
        {
            var request = new UserRequestWrapper { Statement = statement, RequestReceivedAt = DateTime.Now, RemovePunctuation = removePunctuation };

            try
            {
                AddUserRequest(request);

                if (!request.WaitForRequestToComplete.WaitOne(-1))
                {
                    throw new InvalidOperationException(
                        string.Format(
                            "Timed out waiting for request id {1} being processed",
                            request.Id));
                }

                return request.ToUserResonse();
            }
            catch (Exception ex)
            {
                return new UserResponse { Id = request.Id, HasError = true, Error = ex.ToString() };
            }
        }
        internal void AddUserRequest(UserRequestWrapper userRequest)
        {
            // If we're in shutdown mode and destroy semaphore at this point
            // the requests being served just return an error,
            // but the service still will exit gracefully
            _servedUserRequests.WaitOne();

            lock (_activeUserRequests)
            {
                if (_activeUserRequests.ContainsKey(userRequest.Id)) // this can never happen, but just in case
                {
                    throw new InvalidOperationException(string.Format("Request id {0} already exists", userRequest.Id));
                }

                _activeUserRequests.Add(userRequest.Id, Task.Factory.StartNew(new Action<object>(o =>
                {

                    RunUserRequest(o);

                }), userRequest));
            }
        }