Updating to new options pattern

This commit is contained in:
John Luo 2016-01-07 16:37:39 -08:00
parent 07785c9ae0
commit cd813844a0
5 changed files with 20 additions and 26 deletions

View File

@ -10,6 +10,7 @@ using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.WebSockets.Protocol; using Microsoft.AspNet.WebSockets.Protocol;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.WebSockets.Server namespace Microsoft.AspNet.WebSockets.Server
{ {
@ -18,10 +19,19 @@ namespace Microsoft.AspNet.WebSockets.Server
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly WebSocketOptions _options; private readonly WebSocketOptions _options;
public WebSocketMiddleware(RequestDelegate next, WebSocketOptions options) public WebSocketMiddleware(RequestDelegate next, IOptions<WebSocketOptions> options)
{ {
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
_next = next; _next = next;
_options = options; _options = options.Value;
// TODO: validate options. // TODO: validate options.
} }

View File

@ -3,6 +3,7 @@
using System; using System;
using Microsoft.AspNet.WebSockets.Server; using Microsoft.AspNet.WebSockets.Server;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNet.Builder namespace Microsoft.AspNet.Builder
{ {
@ -15,24 +16,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
return app.UseWebSockets(options => { }); return app.UseMiddleware<WebSocketMiddleware>();
}
public static IApplicationBuilder UseWebSockets(this IApplicationBuilder app, Action<WebSocketOptions> configureOptions)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new WebSocketOptions();
configureOptions(options);
return app.UseMiddleware<WebSocketMiddleware>(options);
} }
public static IApplicationBuilder UseWebSockets(this IApplicationBuilder app, WebSocketOptions options) public static IApplicationBuilder UseWebSockets(this IApplicationBuilder app, WebSocketOptions options)
@ -46,7 +30,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
} }
return app.UseMiddleware<WebSocketMiddleware>(options); return app.UseMiddleware<WebSocketMiddleware>(Options.Create(options));
} }
} }
} }

View File

@ -3,7 +3,7 @@
using System; using System;
namespace Microsoft.AspNet.WebSockets.Server namespace Microsoft.AspNet.Builder
{ {
/// <summary> /// <summary>
/// Configuration options for the WebSocketMiddleware /// Configuration options for the WebSocketMiddleware

View File

@ -11,7 +11,8 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNet.Http.Extensions": "1.0.0-*", "Microsoft.AspNet.Http.Extensions": "1.0.0-*",
"Microsoft.AspNet.WebSockets.Protocol": "1.0.0-*" "Microsoft.AspNet.WebSockets.Protocol": "1.0.0-*",
"Microsoft.Extensions.Options": "1.0.0-*"
}, },
"frameworks": { "frameworks": {
"net451": {}, "net451": {},

View File

@ -7,7 +7,6 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
namespace AutobahnTestServer namespace AutobahnTestServer
{ {
@ -18,9 +17,9 @@ namespace AutobahnTestServer
app.Map("/Managed", managedWebSocketsApp => app.Map("/Managed", managedWebSocketsApp =>
{ {
// Comment this out to test native server implementations // Comment this out to test native server implementations
managedWebSocketsApp.UseWebSockets(options => managedWebSocketsApp.UseWebSockets(new WebSocketOptions
{ {
options.ReplaceFeature = true; ReplaceFeature = true
}); });
managedWebSocketsApp.Use(async (context, next) => managedWebSocketsApp.Use(async (context, next) =>