Adding option to configure services when exposing the ASP.NET 5 pipeline via OWIN #398
This commit is contained in:
parent
9e5ba94804
commit
fbec068f5c
|
|
@ -66,22 +66,27 @@ namespace Microsoft.AspNet.Builder
|
|||
}
|
||||
|
||||
public static IApplicationBuilder UseBuilder(this AddMiddleware app)
|
||||
{
|
||||
return app.UseBuilder(serviceProvider: null);
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseBuilder(this AddMiddleware app, IServiceProvider serviceProvider)
|
||||
{
|
||||
// Adapt WebSockets by default.
|
||||
app(OwinWebSocketAcceptAdapter.AdaptWebSockets);
|
||||
var builder = new ApplicationBuilder(serviceProvider: null);
|
||||
var builder = new ApplicationBuilder(serviceProvider: serviceProvider);
|
||||
|
||||
CreateMiddleware middleware = CreateMiddlewareFactory(exit =>
|
||||
var 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 =>
|
||||
{
|
||||
|
|
@ -105,6 +110,7 @@ namespace Microsoft.AspNet.Builder
|
|||
context = new DefaultHttpContext(
|
||||
new FeatureCollection(
|
||||
new OwinFeatureCollection(env)));
|
||||
context.ApplicationServices = applicationServices;
|
||||
}
|
||||
|
||||
return app.Invoke(context);
|
||||
|
|
@ -114,7 +120,12 @@ namespace Microsoft.AspNet.Builder
|
|||
|
||||
public static AddMiddleware UseBuilder(this AddMiddleware app, Action<IApplicationBuilder> pipeline)
|
||||
{
|
||||
var builder = app.UseBuilder();
|
||||
return app.UseBuilder(pipeline, serviceProvider: null);
|
||||
}
|
||||
|
||||
public static AddMiddleware UseBuilder(this AddMiddleware app, Action<IApplicationBuilder> pipeline, IServiceProvider serviceProvider)
|
||||
{
|
||||
var builder = app.UseBuilder(serviceProvider);
|
||||
pipeline(builder);
|
||||
return app;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,87 @@
|
|||
// 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.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Owin
|
||||
{
|
||||
using AppFunc = Func<IDictionary<string, object>, Task>;
|
||||
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
|
||||
{
|
||||
static AppFunc notFound = env => new Task(() => { env["owin.ResponseStatusCode"] = 404; });
|
||||
|
||||
[Fact]
|
||||
public void OwinConfigureServiceProviderAddsServices()
|
||||
{
|
||||
var list = new List<CreateMiddleware>();
|
||||
AddMiddleware build = list.Add;
|
||||
IServiceProvider serviceProvider = null;
|
||||
FakeService fakeService = null;
|
||||
|
||||
var builder = build.UseBuilder(applicationBuilder =>
|
||||
{
|
||||
serviceProvider = applicationBuilder.ApplicationServices;
|
||||
applicationBuilder.Run(async context =>
|
||||
{
|
||||
fakeService = context.ApplicationServices.GetService<FakeService>();
|
||||
});
|
||||
}, new ServiceCollection().AddSingleton(new FakeService()).BuildServiceProvider());
|
||||
|
||||
list.Reverse();
|
||||
list.Aggregate(notFound, (next, middleware) => middleware(next)).Invoke(new Dictionary<string, object>());
|
||||
|
||||
Assert.NotNull(fakeService);
|
||||
Assert.NotNull(serviceProvider.GetService<FakeService>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OwinDefaultNoServices()
|
||||
{
|
||||
var list = new List<CreateMiddleware>();
|
||||
AddMiddleware build = list.Add;
|
||||
IServiceProvider serviceProvider = null;
|
||||
FakeService fakeService = null;
|
||||
bool builderExecuted = false;
|
||||
bool applicationExecuted = false;
|
||||
|
||||
var builder = build.UseBuilder(applicationBuilder =>
|
||||
{
|
||||
builderExecuted = true;
|
||||
serviceProvider = applicationBuilder.ApplicationServices;
|
||||
applicationBuilder.Run(async context =>
|
||||
{
|
||||
applicationExecuted = true;
|
||||
fakeService = context.ApplicationServices.GetService<FakeService>();
|
||||
});
|
||||
});
|
||||
|
||||
list.Reverse();
|
||||
list.Aggregate(notFound, (next, middleware) => middleware(next)).Invoke(new Dictionary<string, object>());
|
||||
|
||||
Assert.True(builderExecuted);
|
||||
Assert.Null(fakeService);
|
||||
Assert.True(applicationExecuted);
|
||||
Assert.Null(serviceProvider);
|
||||
}
|
||||
|
||||
private class FakeService
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.Http.Features;
|
||||
using Xunit;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
"dependencies": {
|
||||
"Microsoft.AspNet.Http": "1.0.0-*",
|
||||
"Microsoft.AspNet.Owin": "1.0.0-*",
|
||||
"Microsoft.Extensions.DependencyInjection": "1.0.0-*",
|
||||
"xunit.runner.aspnet": "2.0.0-aspnet-*"
|
||||
},
|
||||
"commands": {
|
||||
|
|
|
|||
Loading…
Reference in New Issue