Stardardizing option initialization using lambdas

This commit is contained in:
John Luo 2015-12-21 16:37:49 -08:00
parent cb4c05084b
commit fe41412eeb
2 changed files with 22 additions and 5 deletions

View File

@ -1,6 +1,7 @@
// 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 Microsoft.AspNet.WebSockets.Server;
namespace Microsoft.AspNet.Builder
@ -9,11 +10,28 @@ namespace Microsoft.AspNet.Builder
{
public static IApplicationBuilder UseWebSockets(this IApplicationBuilder builder)
{
return builder.UseWebSockets(new WebSocketOptions());
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.UseWebSockets(options => { });
}
public static IApplicationBuilder UseWebSockets(this IApplicationBuilder builder, WebSocketOptions options) // TODO: [NotNull]
public static IApplicationBuilder UseWebSockets(this IApplicationBuilder builder, Action<WebSocketOptions> configureOptions)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
var options = new WebSocketOptions();
configureOptions(options);
return builder.Use(next => new WebSocketMiddleware(next, options).Invoke);
}
}

View File

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