Пример #1
0
        /// <summary>
        /// Begins the async batch operation
        /// </summary>
        /// <param name="commandDatas">The command data.</param>
        /// <returns></returns>
        public Task<BatchResult[]> BatchAsync(ICommandData[] commandDatas)
        {
            var metadata = new JObject();
            AddTransactionInformation(metadata);
            var req = HttpJsonRequest.CreateHttpJsonRequest(this, url + "/bulk_docs", "POST", metadata, credentials);
            var jArray = new JArray(commandDatas.Select(x => x.ToJson()));
            var data = Encoding.UTF8.GetBytes(jArray.ToString(Formatting.None));

            return Task.Factory.FromAsync(req.BeginWrite, req.EndWrite, data, null)
                .ContinueWith(writeTask => Task.Factory.FromAsync<string>(req.BeginReadResponseString, req.EndReadResponseString,null))
                .Unwrap()
                .ContinueWith(task =>
                {
                    string response;
                    try
                    {
                        response = task.Result;
                    }
                    catch (WebException e)
                    {
                        var httpWebResponse = e.Response as HttpWebResponse;
                        if (httpWebResponse == null ||
                            httpWebResponse.StatusCode != HttpStatusCode.Conflict)
                            throw;
                        throw ThrowConcurrencyException(e);
                    }
                    return JsonConvert.DeserializeObject<BatchResult[]>(response);
                });
        }
Пример #2
0
        public BatchResult[] Batch(ICommandData[] commandDatas)
        {
            var metadata = new JObject();
            AddTransactionInformation(metadata);
            var req = new HttpJsonRequest(url + "/bulk_docs", "POST",metadata);
            var jArray = new JArray(commandDatas.Select(x => x.ToJson()));
            req.Write(jArray.ToString(Formatting.None));

            string response;
            try
            {
                response = req.ReadResponseString();
            }
            catch (WebException e)
            {
                var httpWebResponse = e.Response as HttpWebResponse;
                if (httpWebResponse == null ||
                    httpWebResponse.StatusCode != HttpStatusCode.Conflict)
                    throw;
                throw ThrowConcurrencyException(e);
            }
            return JsonConvert.DeserializeObject<BatchResult[]>(response);
        }
Пример #3
0
		/// <summary>
		/// Begins the async batch operation
		/// </summary>
		/// <param name="commandDatas">The command data.</param>
		/// <returns></returns>
		public Task<BatchResult[]> BatchAsync(ICommandData[] commandDatas)
		{
			var metadata = new RavenJObject();
			AddTransactionInformation(metadata);
			var req = jsonRequestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(this, url + "/bulk_docs", "POST", metadata, credentials, convention));
			var jArray = new RavenJArray(commandDatas.Select(x => x.ToJson()));
			var data = jArray.ToString(Formatting.None);

			return Task.Factory.FromAsync(req.BeginWrite, req.EndWrite, data, null)
				.ContinueWith(writeTask => req.ReadResponseJsonAsync())
				.Unwrap()
				.ContinueWith(task =>
				{
					RavenJArray response;
					try
					{
						response = (RavenJArray)task.Result;
					}
					catch (AggregateException e)
					{
						var we = e.ExtractSingleInnerException() as WebException;
						if (we == null)
							throw;
						var httpWebResponse = we.Response as HttpWebResponse;
						if (httpWebResponse == null ||
							httpWebResponse.StatusCode != HttpStatusCode.Conflict)
							throw;
						throw ThrowConcurrencyException(we);
					}
					return convention.CreateSerializer().Deserialize<BatchResult[]>(new RavenJTokenReader(response));
				});

		}
Пример #4
0
		/// <summary>
		/// Begins the async batch operation
		/// </summary>
		/// <param name="commandDatas">The command data.</param>
		/// <returns></returns>
		public Task<BatchResult[]> BatchAsync(ICommandData[] commandDatas)
		{
			var metadata = new JObject();
			var req = HttpJsonRequest.CreateHttpJsonRequest(this, url + "/bulk_docs", "POST", metadata, credentials, convention);
			var jArray = new JArray(commandDatas.Select(x => x.ToJson()));
			var data = Encoding.UTF8.GetBytes(jArray.ToString(Formatting.None));

			return req.WriteAsync(data)
				.ContinueWith(writeTask => req.ReadResponseStringAsync())
				.ContinueWith(task =>
				{
					string response;
					try
					{
						response = task.Result.Result;
					}
					catch (WebException e)
					{
						var httpWebResponse = e.Response as HttpWebResponse;
						if (httpWebResponse == null ||
							httpWebResponse.StatusCode != HttpStatusCode.Conflict)
							throw;
						throw ThrowConcurrencyException(e);
					}
					return JsonConvert.DeserializeObject<BatchResult[]>(response);
				});

		}
Пример #5
0
		public IAsyncResult BeginBatch(ICommandData[] commandDatas, AsyncCallback callback, object state)
		{
			var metadata = new JObject();
			AddTransactionInformation(metadata);
			var req = HttpJsonRequest.CreateHttpJsonRequest(this, url + "/bulk_docs", "POST", metadata, credentials);
			var jArray = new JArray(commandDatas.Select(x => x.ToJson()));
			var data = Encoding.UTF8.GetBytes(jArray.ToString(Formatting.None));
			var multiStepAsyncResult = new MultiStepAsyncResult(state, req);

			var asyncResult = req.BeginWrite(data, ContinueOperation, new Contiuation
			{
				Callback = callback,
				State = state,
				Request = req,
				MultiAsyncResult = multiStepAsyncResult
			});
			
			if (asyncResult.CompletedSynchronously)
			{
				ContinueOperation(asyncResult);
			}

			return multiStepAsyncResult;
		}