IronPigeon.Relay.Controllers.InboxController.PostNotificationAsync C# (CSharp) Method

PostNotificationAsync() private method

private PostNotificationAsync ( string id, int lifetime ) : Task
id string
lifetime int
return Task
        public async Task<ActionResult> PostNotificationAsync(string id, int lifetime)
        {
            Requires.NotNullOrEmpty(id, "id");
            Requires.Range(lifetime > 0, "lifetime");

            if (this.Request.ContentLength > MaxNotificationSize)
            {
                throw new ArgumentException("Maximum message notification size exceeded.");
            }

            InboxEntity inbox = await this.GetInboxAsync(id);
            if (inbox == null)
            {
                return new HttpNotFoundResult();
            }

            var directory = this.InboxContainer.GetDirectoryReference(id);
            var blob = directory.GetBlockBlobReference(Utilities.CreateRandomWebSafeName(24));
            Debug.WriteLine("Defining blob: {0} ({1})", blob.Name, blob.Uri);

            var requestedLifeSpan = TimeSpan.FromMinutes(lifetime);
            var actualLifespan = requestedLifeSpan > MaxLifetimeCeiling ? MaxLifetimeCeiling : requestedLifeSpan;
            var expirationDate = DateTime.UtcNow + actualLifespan;
            blob.Metadata[ExpirationDateMetadataKey] = expirationDate.ToString(CultureInfo.InvariantCulture);

            await blob.UploadFromStreamAsync(this.Request.InputStream);

            // One more last ditch check that the max size was not exceeded, in case
            // the client is lying in the HTTP headers.
            if (blob.Properties.Length > MaxNotificationSize)
            {
                await blob.DeleteAsync();
                throw new ArgumentException("Maximum message notification size exceeded.");
            }

            // Notifying the receiver isn't something the sender needs to wait for.
            var nowait = Task.Run(async delegate
            {
                await this.AlertLongPollWaiterAsync(inbox);
                await this.InboxTable.SaveChangesWithMergeAsync(inbox);
            });
            return new HttpStatusCodeResult(HttpStatusCode.Created);
        }

Usage Example

示例#1
0
        private async Task PostNotificationHelper(InboxController controller, Stream inputStream = null, int lifetimeInMinutes = 10)
        {
            SetupNextRequest(controller, "POST", inputStream);

            var result = await controller.PostNotificationAsync(this.inboxId, lifetimeInMinutes);
            Assert.IsType(typeof(HttpStatusCodeResult), result);
            var actualStatus = (HttpStatusCode)((HttpStatusCodeResult)result).StatusCode;
            Assert.Equal(HttpStatusCode.Created, actualStatus);
        }