private async Task StartBulkInsertAsync(BulkInsertOptions options) { using (ConnectionOptions.Expect100Continue(operationClient.Url)) { var operationUrl = CreateOperationUrl(options); var token = await GetToken().ConfigureAwait(false); try { token = await ValidateThatWeCanUseAuthenticateTokens(token).ConfigureAwait(false); } catch (Exception e) { throw new InvalidOperationException("Could not authenticate token for bulk insert, if you are using ravendb in IIS make sure you have Anonymous Authentication enabled in the IIS configuration", e); } using (operationRequest = CreateOperationRequest(operationUrl, token)) { var cancellationToken = CreateCancellationToken(); var response = await operationRequest.ExecuteRawRequestAsync((stream, source) => Task.Factory.StartNew(() => { try { WriteQueueToServer(stream, options, cancellationToken); var x = source.TrySetResult(null); } catch (Exception e) { source.TrySetException(e); } }, TaskCreationOptions.LongRunning)).ConfigureAwait(false); await response.AssertNotFailingResponse(); long operationId; using (response) using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) using (var streamReader = new StreamReader(stream)) { var result = RavenJObject.Load(new JsonTextReader(streamReader)); operationId = result.Value <long>("OperationId"); } if (await IsOperationCompleted(operationId).ConfigureAwait(false)) { responseOperationId = operationId; } } } }