Per my observation, this behavior is due to some internal expection occured because of HTTPContext.Current is invalid, As a work-around execute the code within a web-based application.