Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
Adding option to configure services when exposing the ASP.NET 5 pipel…
Browse files Browse the repository at this point in the history
…ine via OWIN #398
  • Loading branch information
JunTaoLuo committed Nov 9, 2015
1 parent 308dd10 commit 128e135
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/Microsoft.AspNet.Owin/OwinExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Owin;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNet.Builder
{
Expand Down Expand Up @@ -65,23 +66,29 @@ public static IApplicationBuilder UseOwin(this IApplicationBuilder builder, Acti
return builder;
}

public static IApplicationBuilder UseBuilder(this AddMiddleware app)
public static IApplicationBuilder UseBuilder(this AddMiddleware app, Action<IServiceCollection> configureServices = null)
{
// Adapt WebSockets by default.
app(OwinWebSocketAcceptAdapter.AdaptWebSockets);
var builder = new ApplicationBuilder(serviceProvider: null);
IServiceCollection services = null;
if (configureServices != null)
{
services = new ServiceCollection();
configureServices(services);
}
var builder = new ApplicationBuilder(serviceProvider: services?.BuildServiceProvider());

CreateMiddleware middleware = CreateMiddlewareFactory(exit =>
{
builder.Use(ignored => exit);
return builder.Build();
});
}, builder.ApplicationServices);

app(middleware);
return builder;
}

private static CreateMiddleware CreateMiddlewareFactory(Func<RequestDelegate, RequestDelegate> middleware)
private static CreateMiddleware CreateMiddlewareFactory(Func<RequestDelegate, RequestDelegate> middleware, IServiceProvider applicationServices)
{
return next =>
{
Expand All @@ -105,16 +112,17 @@ private static CreateMiddleware CreateMiddlewareFactory(Func<RequestDelegate, Re
context = new DefaultHttpContext(
new FeatureCollection(
new OwinFeatureCollection(env)));
context.ApplicationServices = applicationServices;
}

return app.Invoke(context);
};
};
}

public static AddMiddleware UseBuilder(this AddMiddleware app, Action<IApplicationBuilder> pipeline)
public static AddMiddleware UseBuilder(this AddMiddleware app, Action<IApplicationBuilder> pipeline, Action<IServiceCollection> configureServices = null)
{
var builder = app.UseBuilder();
var builder = app.UseBuilder(configureServices);
pipeline(builder);
return app;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Owin/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"keyFile": "../../tools/Key.snk"
},
"dependencies": {
"Microsoft.AspNet.Http": "1.0.0-*"
"Microsoft.AspNet.Http": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection": "1.0.0-*"
},
"frameworks": {
"net451": {},
Expand Down
48 changes: 48 additions & 0 deletions test/Microsoft.AspNet.Owin.Tests/OwinExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.Extensions.DependencyInjection;
using Xunit;


namespace Microsoft.AspNet.Owin
{
using CreateMiddleware = Func<
Func<IDictionary<string, object>, Task>,
Func<IDictionary<string, object>, Task>
>;
using AddMiddleware = Action<Func<
Func<IDictionary<string, object>, Task>,
Func<IDictionary<string, object>, Task>
>>;

public class OwinExtensionTests
{
[Fact]
public void OwinConfigureServicesAddsServices()
{
AddMiddleware build = new List<CreateMiddleware>().Add;
IServiceProvider serviceProvider = null;

var builder = build.UseBuilder(applicationBuilder =>
{
serviceProvider = applicationBuilder.ApplicationServices;
}
, serviceCollection =>
{
serviceCollection.AddInstance(new FakeService());
});

Assert.NotNull(serviceProvider);
}

public class FakeService
{

}
}
}

0 comments on commit 128e135

Please sign in to comment.