Пример #1
0
        public Task <HttpResponseMessage> Post(bool asynch, [FromBody] DatedValue value) //Insert Value
        {
            try
            {
                //Simple types are harder to post than complex types!
                //http://weblog.west-wind.com/posts/2012/Mar/21/ASPNET-Web-API-and-Simple-Value-Parameters-from-POSTed-data
                if (value == null)
                {
                    throw new InvalidOperationException("Expected value in body, got " + Request.Content.ReadAsStringAsync().Result);
                }

                //Throw message on Queue

                Guid key = GuidUtils.NewSequential();

                //Want this to run to completion, even if response associated with parent thread is done.
                Task toWait = Task.Factory.StartNew(() =>
                {
                    queue.Add(key, 0);
                    DatedValue justCreated = Insert(value);
                    queue[key]             = justCreated.Id;
                });

                //Return address to resource just created.
                Task <HttpResponseMessage> sender = Task.Factory.StartNew(() =>
                {
                    HttpResponseMessage message = Request.CreateResponse(HttpStatusCode.Accepted);
                    message.Headers.Location    = new Uri("/ValueQueue/" + key);

                    return(message);
                });

                Task.WaitAll((new[] { toWait, sender }));

                return(sender);
            }
            catch (Exception ex)
            {
                Tracing.Err.TraceEvent(TraceEventType.Error, 0, ex.ToString());
                throw;
            }
        }