Пример #1
0
        /// <summary>
        /// Add an item to a specific place in the pipeline.
        /// </summary>
        /// <param name="index">Index to add at</param>
        /// <param name="item">Item to add</param>
        /// <param name="replaceInPlace">
        /// Whether to replace an existing item with the same name in its current place,
        /// rather than at the position requested. Defaults to false.
        /// </param>
        public virtual void InsertItemAtPipelineIndex(int index, PipelineItem <TAsyncDelegate> item, bool replaceInPlace = false)
        {
            var existingIndex = this.RemoveByName(item.Name);

            var newIndex = (replaceInPlace && existingIndex != -1) ? existingIndex : index;

            this.pipelineItems.Insert(newIndex, item);
        }
Пример #2
0
        /// <summary>
        /// Perform any initialisation tasks
        /// </summary>
        public void Initialize(IApplicationPipelines pipelines)
        {
            var item = new PipelineItem <Func <NancyContext, Response> >("Static content", ctx =>
            {
                return(conventions
                       .Select(convention => convention.Invoke(ctx, rootPathProvider.GetRootPath()))
                       .FirstOrDefault(response => response != null));
            });

            pipelines.BeforeRequest.AddItemToStartOfPipeline(item);
        }
Пример #3
0
        /// <summary>
        /// Insert an item before a named item.
        /// If the named item does not exist the item is inserted at the start of the pipeline.
        /// </summary>
        /// <param name="name">Name of the item to insert before</param>
        /// <param name="item">Item to insert</param>
        public virtual void InsertBefore(string name, PipelineItem <TAsyncDelegate> item)
        {
            var existingIndex =
                this.pipelineItems.FindIndex(i => string.Equals(name, i.Name, StringComparison.Ordinal));

            if (existingIndex == -1)
            {
                existingIndex = 0;
            }

            this.InsertItemAtPipelineIndex(existingIndex, item);
        }
Пример #4
0
        /// <summary>
        /// Add an item to the end of the pipeline
        /// </summary>
        /// <param name="item">Item to add</param>
        /// <param name="replaceInPlace">
        /// Whether to replace an existing item with the same name in its current place,
        /// rather than at the position requested. Defaults to false.
        /// </param>
        public virtual void AddItemToEndOfPipeline(PipelineItem <TAsyncDelegate> item, bool replaceInPlace = false)
        {
            var existingIndex = this.RemoveByName(item.Name);

            if (replaceInPlace && existingIndex != -1)
            {
                this.InsertItemAtPipelineIndex(existingIndex, item);
            }
            else
            {
                this.pipelineItems.Add(item);
            }
        }
Пример #5
0
        /// <summary>
        /// Insert an item after a named item.
        /// If the named item does not exist the item is inserted at the end of the pipeline.
        /// </summary>
        /// <param name="name">Name of the item to insert after</param>
        /// <param name="item">Item to insert</param>
        public virtual void InsertAfter(string name, PipelineItem <TAsyncDelegate> item)
        {
            var existingIndex =
                this.pipelineItems.FindIndex(i => string.Equals(name, i.Name, StringComparison.Ordinal));

            if (existingIndex == -1)
            {
                existingIndex = this.pipelineItems.Count;
            }

            existingIndex++;

            if (existingIndex > this.pipelineItems.Count)
            {
                this.AddItemToEndOfPipeline(item);
            }
            else
            {
                this.InsertItemAtPipelineIndex(existingIndex, item);
            }
        }
Пример #6
0
 /// <summary>
 /// Add an item to the start of the pipeline
 /// </summary>
 /// <param name="item">Item to add</param>
 /// <param name="replaceInPlace">
 /// Whether to replace an existing item with the same name in its current place,
 /// rather than at the position requested. Defaults to false.
 /// </param>
 public virtual void AddItemToStartOfPipeline(PipelineItem <TSyncDelegate> item, bool replaceInPlace = false)
 {
     this.AddItemToStartOfPipeline(this.Wrap(item), replaceInPlace);
 }
Пример #7
0
 /// <summary>
 /// Add an item to the start of the pipeline
 /// </summary>
 /// <param name="item">Item to add</param>
 /// <param name="replaceInPlace">
 /// Whether to replace an existing item with the same name in its current place,
 /// rather than at the position requested. Defaults to false.
 /// </param>
 public virtual void AddItemToStartOfPipeline(PipelineItem <TAsyncDelegate> item, bool replaceInPlace = false)
 {
     this.InsertItemAtPipelineIndex(0, item, replaceInPlace);
 }
Пример #8
0
 /// <summary>
 /// Wraps a sync delegate into it's async form
 /// </summary>
 /// <param name="syncDelegate">Sync pipeline instance</param>
 /// <returns>Async pipeline instance</returns>
 protected abstract PipelineItem <TAsyncDelegate> Wrap(PipelineItem <TSyncDelegate> syncDelegate);
Пример #9
0
 /// <summary>
 /// Insert an item after a named item.
 /// If the named item does not exist the item is inserted at the end of the pipeline.
 /// </summary>
 /// <param name="name">Name of the item to insert after</param>
 /// <param name="item">Item to insert</param>
 public virtual void InsertAfter(string name, PipelineItem <TSyncDelegate> item)
 {
     this.InsertAfter(name, this.Wrap(item));
 }
Пример #10
0
 /// <summary>
 /// Add an item to a specific place in the pipeline.
 /// </summary>
 /// <param name="index">Index to add at</param>
 /// <param name="item">Item to add</param>
 /// <param name="replaceInPlace">
 /// Whether to replace an existing item with the same name in its current place,
 /// rather than at the position requested. Defaults to false.
 /// </param>
 public virtual void InsertItemAtPipelineIndex(int index, PipelineItem <TSyncDelegate> item, bool replaceInPlace = false)
 {
     this.InsertItemAtPipelineIndex(index, this.Wrap(item), replaceInPlace);
 }
Пример #11
0
 static Jsonp()
 {
     JsonpItem = new PipelineItem <Action <NancyContext> >("JSONP", PrepareJsonp);
 }
        /// <summary>
        /// Wraps a sync delegate into it's async form
        /// </summary>
        /// <param name="pipelineItem">Sync pipeline item instance</param>
        /// <returns>Async pipeline item instance</returns>
        protected override PipelineItem <Func <NancyContext, CancellationToken, Task <Response> > > Wrap(PipelineItem <Func <NancyContext, Response> > pipelineItem)
        {
            var syncDelegate = pipelineItem.Delegate;
            Func <NancyContext, CancellationToken, Task <Response> > asyncDelegate = (ctx, ct) =>
            {
                var tcs = new TaskCompletionSource <Response>();
                try
                {
                    var result = syncDelegate.Invoke(ctx);
                    tcs.SetResult(result);
                }
                catch (Exception e)
                {
                    tcs.SetException(e);
                }
                return(tcs.Task);
            };

            return(new PipelineItem <Func <NancyContext, CancellationToken, Task <Response> > >(pipelineItem.Name, asyncDelegate));
        }
 /// <summary>
 /// Wraps the specified <see cref="PipelineItem{T}"/> into its async form.
 /// </summary>
 /// <param name="pipelineItem">The <see cref="PipelineItem{T}"/>.</param>
 /// <returns>Async <see cref="PipelineItem{T}"/> instance</returns>
 protected override PipelineItem <Func <NancyContext, CancellationToken, Task <Response> > > Wrap(PipelineItem <Func <NancyContext, Response> > pipelineItem)
 {
     return(new PipelineItem <Func <NancyContext, CancellationToken, Task <Response> > >(pipelineItem.Name, (ctx, ct) => Task.FromResult(pipelineItem.Delegate(ctx))));
 }
        /// <summary>
        /// Wraps a sync delegate into it's async form
        /// </summary>
        /// <param name="pipelineItem">Sync pipeline item instance</param>
        /// <returns>Async pipeline item instance</returns>
        protected override PipelineItem <Func <NancyContext, CancellationToken, Task> > Wrap(PipelineItem <Action <NancyContext> > pipelineItem)
        {
            var syncDelegate = pipelineItem.Delegate;
            Func <NancyContext, CancellationToken, Task> asyncDelegate = (ctx, ct) =>
            {
                try
                {
                    syncDelegate.Invoke(ctx);
                    return(completeTask);
                }
                catch (Exception e)
                {
                    var tcs = new TaskCompletionSource <object>();
                    tcs.SetException(e);
                    return(tcs.Task);
                }
            };

            return(new PipelineItem <Func <NancyContext, CancellationToken, Task> >(pipelineItem.Name, asyncDelegate));
        }
Пример #15
0
 /// <summary>
 /// Wraps the specified <see cref="PipelineItem{T}"/> instance into its async form.
 /// </summary>
 /// <param name="pipelineItem">The <see cref="PipelineItem{T}"/> instance.</param>
 /// <returns>Async <see cref="PipelineItem{T}"/> instance</returns>
 protected override PipelineItem <Func <NancyContext, CancellationToken, Task> > Wrap(PipelineItem <Action <NancyContext> > pipelineItem)
 {
     return(new PipelineItem <Func <NancyContext, CancellationToken, Task> >(pipelineItem.Name, (ctx, ct) =>
     {
         pipelineItem.Delegate(ctx);
         return TaskHelpers.CompletedTask;
     }));
 }