Skip to content

Commit fe41412

Browse files
committed
Stardardizing option initialization using lambdas
1 parent cb4c050 commit fe41412

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

src/Microsoft.AspNet.WebSockets.Server/WebSocketMiddlewareExtensions.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Microsoft.AspNet.WebSockets.Server;
56

67
namespace Microsoft.AspNet.Builder
@@ -9,11 +10,28 @@ public static class WebSocketMiddlewareExtensions
910
{
1011
public static IApplicationBuilder UseWebSockets(this IApplicationBuilder builder)
1112
{
12-
return builder.UseWebSockets(new WebSocketOptions());
13+
if (builder == null)
14+
{
15+
throw new ArgumentNullException(nameof(builder));
16+
}
17+
18+
return builder.UseWebSockets(options => { });
1319
}
1420

15-
public static IApplicationBuilder UseWebSockets(this IApplicationBuilder builder, WebSocketOptions options) // TODO: [NotNull]
21+
public static IApplicationBuilder UseWebSockets(this IApplicationBuilder builder, Action<WebSocketOptions> configureOptions)
1622
{
23+
if (builder == null)
24+
{
25+
throw new ArgumentNullException(nameof(builder));
26+
}
27+
if (configureOptions == null)
28+
{
29+
throw new ArgumentNullException(nameof(configureOptions));
30+
}
31+
32+
var options = new WebSocketOptions();
33+
configureOptions(options);
34+
1735
return builder.Use(next => new WebSocketMiddleware(next, options).Invoke);
1836
}
1937
}

test/AutobahnTestServer/Startup.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.AspNet.Builder;
99
using Microsoft.AspNet.Hosting;
1010
using Microsoft.AspNet.Http;
11-
using Microsoft.AspNet.WebSockets.Server;
1211

1312
namespace AutobahnTestServer
1413
{
@@ -19,9 +18,9 @@ public void Configure(IApplicationBuilder app)
1918
app.Map("/Managed", managedWebSocketsApp =>
2019
{
2120
// Comment this out to test native server implementations
22-
managedWebSocketsApp.UseWebSockets(new WebSocketOptions()
21+
managedWebSocketsApp.UseWebSockets(options =>
2322
{
24-
ReplaceFeature = true,
23+
options.ReplaceFeature = true;
2524
});
2625

2726
managedWebSocketsApp.Use(async (context, next) =>

0 commit comments

Comments
 (0)