Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Do NOT use static service location in NancyMiddleware. The will cause…
Browse files Browse the repository at this point in the history
… all sort of problems if there is more than one Nancy instance / bootstrapper in a owin pipeline.
  • Loading branch information
damianh committed Mar 12, 2015
1 parent 7e3bb35 commit e8ae7c4
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Nancy.Owin.Tests/AppBuilderExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void When_host_Nancy_via_IAppBuilder_then_should_handle_requests()
// Given
var bootstrapper = new ConfigurableBootstrapper(config => config.Module<TestModule>());

using(var server = TestServer.Create(app => app.UseNancy(opts => opts.Bootstrapper = bootstrapper)))
using(var server = TestServer.Create(app => app.UseNancy(bootstrapper)))
{

// When
Expand Down
32 changes: 23 additions & 9 deletions src/Nancy.Owin/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Owin
using System;
using System.Threading;

using Nancy.Bootstrapper;
using Nancy.Owin;

/// <summary>
Expand All @@ -18,27 +19,40 @@ public static class AppBuilderExtensions
/// Adds Nancy to the OWIN pipeline.
/// </summary>
/// <param name="builder">The application builder.</param>
/// <param name="options">The Nancy options.</param>
/// <param name="bootstrapper">The Nancy bootstrapper.</param>
/// <returns>IAppBuilder.</returns>
public static IAppBuilder UseNancy(this IAppBuilder builder, NancyOptions options = null)
public static IAppBuilder UseNancy(this IAppBuilder builder, INancyBootstrapper bootstrapper)
{
var nancyOptions = options ?? new NancyOptions();
var options = new NancyOptions(bootstrapper);

HookDisposal(builder, options);

return builder.Use(NancyMiddleware.UseNancy(options));
}

HookDisposal(builder, nancyOptions);
/// <summary>
/// Adds Nancy to the OWIN pipeline.
/// </summary>
/// <param name="builder">The application builder.</param>
/// <param name="options">The Nancy options.</param>
/// <returns>IAppBuilder.</returns>
public static IAppBuilder UseNancy(this IAppBuilder builder, NancyOptions options)
{
HookDisposal(builder, options);

return builder.Use(NancyMiddleware.UseNancy(nancyOptions));
return builder.Use(NancyMiddleware.UseNancy(options));
}

/// <summary>
/// Adds Nancy to the OWIN pipeline.
/// </summary>
/// <param name="builder">The application builder.</param>
/// <param name="configuration">A configuration builder action.</param>
/// <param name="configureOptions">A configuration builder action.</param>
/// <returns>IAppBuilder.</returns>
public static IAppBuilder UseNancy(this IAppBuilder builder, Action<NancyOptions> configuration)
public static IAppBuilder UseNancy(this IAppBuilder builder, INancyBootstrapper bootstrapper, Action<NancyOptions> configureOptions)
{
var options = new NancyOptions();
configuration(options);
var options = new NancyOptions(bootstrapper);
configureOptions(options);
return UseNancy(builder, options);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Nancy.Tests/Unit/NancyMiddlewareFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public NancyMiddlewareFixture()
this.fakeEngine = A.Fake<INancyEngine>();
this.fakeBootstrapper = A.Fake<INancyBootstrapper>();
A.CallTo(() => this.fakeBootstrapper.GetEngine()).Returns(this.fakeEngine);
this.host = NancyMiddleware.UseNancy(new NancyOptions {Bootstrapper = this.fakeBootstrapper})(null);
this.host = NancyMiddleware.UseNancy(new NancyOptions(this.fakeBootstrapper))(null);
this.environment = new Dictionary<string, object>
{
{"owin.RequestMethod", "GET"},
Expand Down
2 changes: 1 addition & 1 deletion src/Nancy.Tests/Unit/NancyOptionsExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class NancyOptionsExtensionsFixture
public void When_response_status_code_match_then_should_perform_pass_through()
{
// Given
var options = new NancyOptions();
var options = new NancyOptions(new DefaultNancyBootstrapper());
options.PassThroughWhenStatusCodesAre(HttpStatusCode.NotFound);
var nancyContext = new NancyContext { Response = new Response {StatusCode = HttpStatusCode.NotFound} };

Expand Down
2 changes: 1 addition & 1 deletion src/Nancy.Tests/Unit/NancyOptionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class NancyOptionsFixture

public NancyOptionsFixture()
{
this.nancyOptions = new NancyOptions();
this.nancyOptions = new NancyOptions(new DefaultNancyBootstrapper());
}

[Fact]
Expand Down
29 changes: 22 additions & 7 deletions src/Nancy/Owin/DelegateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
using System;

using Nancy.Bootstrapper;

using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>,
System.Threading.Tasks.Task>;

Expand All @@ -18,13 +20,23 @@ public static class DelegateExtensions
/// Adds Nancy to the OWIN pipeline.
/// </summary>
/// <param name="builder">The application builder delegate.</param>
/// <param name="action">A configuration builder action.</param>
/// <param name="nancyBootstrapper">A Nancy bootstrapper.</param>
/// <param name="configure">A configuration builder action.</param>
/// <returns>The application builder delegate.</returns>
public static Action<MidFunc> UseNancy(this Action<MidFunc> builder, Action<NancyOptions> action)
public static Action<MidFunc> UseNancy(this Action<MidFunc> builder, INancyBootstrapper nancyBootstrapper, Action<NancyOptions> configure)
{
var options = new NancyOptions();
if (nancyBootstrapper == null)
{
throw new ArgumentNullException("nancyBootstrapper");
}
if (configure == null)
{
throw new ArgumentNullException("configure");
}

var options = new NancyOptions(nancyBootstrapper);

action(options);
configure(options);

return builder.UseNancy(options);
}
Expand All @@ -35,11 +47,14 @@ public static Action<MidFunc> UseNancy(this Action<MidFunc> builder, Action<Nanc
/// <param name="builder">The application builder delegate.</param>
/// <param name="options">The Nancy options.</param>
/// <returns>The application builder delegate.</returns>
public static Action<MidFunc> UseNancy(this Action<MidFunc> builder, NancyOptions options = null)
public static Action<MidFunc> UseNancy(this Action<MidFunc> builder, NancyOptions options)
{
var nancyOptions = options ?? new NancyOptions();
if (options == null)
{
throw new ArgumentNullException("options");
}

builder(NancyMiddleware.UseNancy(nancyOptions).Invoke);
builder(NancyMiddleware.UseNancy(options).Invoke);

return builder;
}
Expand Down
28 changes: 22 additions & 6 deletions src/Nancy/Owin/NancyMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading;
using System.Threading.Tasks;

using Nancy.Bootstrapper;
using Nancy.Helpers;
using Nancy.IO;

Expand All @@ -34,12 +35,23 @@ public static class NancyMiddleware
/// <summary>
/// Use Nancy in an OWIN pipeline
/// </summary>
/// <param name="configuration">A delegate to configure the <see cref="NancyOptions"/>.</param>
/// <param name="bootstrapper">A Nancy bootstrapper.</param>
/// <param name="configure">A delegate to configure the <see cref="NancyOptions"/>.</param>
/// <returns>An OWIN middleware delegate.</returns>
public static MidFunc UseNancy(Action<NancyOptions> configuration)
public static MidFunc UseNancy(INancyBootstrapper bootstrapper, Action<NancyOptions> configure)
{
var options = new NancyOptions();
configuration(options);
if (bootstrapper == null)
{
throw new ArgumentNullException("bootstrapper");
}

if (configure == null)
{
throw new ArgumentNullException("configure");
}

var options = new NancyOptions(bootstrapper);
configure(options);
return UseNancy(options);
}

Expand All @@ -48,9 +60,13 @@ public static MidFunc UseNancy(Action<NancyOptions> configuration)
/// </summary>
/// <param name="options">An <see cref="NancyOptions"/> to configure the Nancy middleware</param>
/// <returns>An OWIN middleware delegate.</returns>
public static MidFunc UseNancy(NancyOptions options = null)
public static MidFunc UseNancy(NancyOptions options)
{
options = options ?? new NancyOptions();
if (options == null)
{
throw new ArgumentNullException("options");
}

options.Bootstrapper.Initialise();
var engine = options.Bootstrapper.GetEngine();

Expand Down
22 changes: 18 additions & 4 deletions src/Nancy/Owin/NancyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,30 @@
/// </summary>
public class NancyOptions
{
private INancyBootstrapper bootstrapper;
private readonly INancyBootstrapper bootstrapper;
private Func<NancyContext, bool> performPassThrough;

/// <summary>
/// Gets or sets the bootstrapper. If none is set, NancyBootstrapperLocator.Bootstrapper is used.
/// Initializes a new instance of the <see cref="NancyOptions"/> class.
/// </summary>
/// <param name="bootstrapper">A Nancy bootstrapper.</param>
/// <exception cref="System.ArgumentNullException">bootstrapper</exception>
public NancyOptions(INancyBootstrapper bootstrapper)
{
if(bootstrapper == null)
{
throw new ArgumentNullException("bootstrapper");
}

this.bootstrapper = bootstrapper;
}

/// <summary>
/// Gets the bootstrapper.
/// </summary>
public INancyBootstrapper Bootstrapper
{
get { return this.bootstrapper ?? NancyBootstrapperLocator.Bootstrapper; }
set { this.bootstrapper = value; }
get { return this.bootstrapper; }
}

/// <summary>
Expand Down

0 comments on commit e8ae7c4

Please sign in to comment.