From 8caec74f03fb5622040245912f782d94dcfe54ad Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Fri, 14 Nov 2014 16:35:05 -0800 Subject: [PATCH] Add a HotAdd/Remove prefix sample. --- WebListener.sln | 17 ++++- samples/HotAddSample/HotAddSample.kproj | 21 ++++++ samples/HotAddSample/Startup.cs | 87 +++++++++++++++++++++++++ samples/HotAddSample/project.json | 23 +++++++ samples/SelfHostServer/project.json | 2 +- 5 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 samples/HotAddSample/HotAddSample.kproj create mode 100644 samples/HotAddSample/Startup.cs create mode 100644 samples/HotAddSample/project.json diff --git a/WebListener.sln b/WebListener.sln index c55169b..ddac12e 100644 --- a/WebListener.sln +++ b/WebListener.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.22115.0 +VisualStudioVersion = 14.0.22310.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestClient", "samples\TestClient\TestClient.csproj", "{8B828433-B333-4C19-96AE-00BFFF9D8841}" EndProject @@ -30,6 +30,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution global.json = global.json EndProjectSection EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "HotAddSample", "samples\HotAddSample\HotAddSample.kproj", "{8BFA392A-8B67-4454-916B-67C545EDFAEF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -120,6 +122,18 @@ Global {DCB6E0B1-223D-44E6-8696-4767E5B6E6A1}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {DCB6E0B1-223D-44E6-8696-4767E5B6E6A1}.Release|Mixed Platforms.Build.0 = Release|Any CPU {DCB6E0B1-223D-44E6-8696-4767E5B6E6A1}.Release|x86.ActiveCfg = Release|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Debug|x86.ActiveCfg = Debug|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Debug|x86.Build.0 = Debug|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Release|Any CPU.Build.0 = Release|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Release|x86.ActiveCfg = Release|Any CPU + {8BFA392A-8B67-4454-916B-67C545EDFAEF}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -133,5 +147,6 @@ Global {4492FF4C-9032-411D-853F-46B01755E504} = {E183C826-1360-4DFF-9994-F33CED5C8525} {B9F45F9D-D206-47F0-8E5F-54CE2F0BDF92} = {99D5E5F3-88F5-4CCF-8D8C-717C8925DF09} {DCB6E0B1-223D-44E6-8696-4767E5B6E6A1} = {E183C826-1360-4DFF-9994-F33CED5C8525} + {8BFA392A-8B67-4454-916B-67C545EDFAEF} = {3A1E31E3-2794-4CA3-B8E2-253E96BDE514} EndGlobalSection EndGlobal diff --git a/samples/HotAddSample/HotAddSample.kproj b/samples/HotAddSample/HotAddSample.kproj new file mode 100644 index 0000000..89120d7 --- /dev/null +++ b/samples/HotAddSample/HotAddSample.kproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 8bfa392a-8b67-4454-916b-67c545edfaef + HotAddSample + ..\..\artifacts\obj\$(MSBuildProjectName) + ..\..\artifacts\bin\$(MSBuildProjectName)\ + + + 2.0 + 59517 + + web + + + \ No newline at end of file diff --git a/samples/HotAddSample/Startup.cs b/samples/HotAddSample/Startup.cs new file mode 100644 index 0000000..2092308 --- /dev/null +++ b/samples/HotAddSample/Startup.cs @@ -0,0 +1,87 @@ +using System; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Server.WebListener; + +namespace HotAddSample +{ + // This sample shows how to dynamically add or remove prefixes for the underlying server. + // Be careful not to remove the prefix you're currently accessing because the connection + // will be reset before the end of the request. + public class Startup + { + public void Configure(IApplicationBuilder app) + { + var server = (ServerInformation)app.Server; + var listener = server.Listener; + listener.UrlPrefixes.Add("http://localhost:12346/pathBase/"); + + app.Use(async (context, next) => + { + // Note: To add any prefix other than localhost you must run this sample as an administrator. + var toAdd = context.Request.Query["add"]; + if (!string.IsNullOrEmpty(toAdd)) + { + context.Response.ContentType = "text/html"; + await context.Response.WriteAsync(""); + try + { + listener.UrlPrefixes.Add(toAdd); + await context.Response.WriteAsync("Added: " + toAdd); + } + catch (Exception ex) + { + await context.Response.WriteAsync("Error adding: " + toAdd + "
"); + await context.Response.WriteAsync(ex.ToString().Replace(Environment.NewLine, "
")); + } + await context.Response.WriteAsync("
back"); + await context.Response.WriteAsync(""); + return; + } + await next(); + }); + + app.Use(async (context, next) => + { + // Be careful not to remove the prefix you're currently accessing because the connection + // will be reset before the response is sent. + var toRemove = context.Request.Query["remove"]; + if (!string.IsNullOrEmpty(toRemove)) + { + context.Response.ContentType = "text/html"; + await context.Response.WriteAsync(""); + if (listener.UrlPrefixes.Remove(toRemove)) + { + await context.Response.WriteAsync("Removed: " + toRemove); + } + else + { + await context.Response.WriteAsync("Not found: " + toRemove); + } + await context.Response.WriteAsync("
back"); + await context.Response.WriteAsync(""); + return; + } + await next(); + }); + + app.Run(async context => + { + context.Response.ContentType = "text/html"; + await context.Response.WriteAsync(""); + await context.Response.WriteAsync("Listening on these prefixes:
"); + foreach (var prefix in listener.UrlPrefixes) + { + await context.Response.WriteAsync("" + prefix + " (remove)
"); + } + + await context.Response.WriteAsync("
"); + await context.Response.WriteAsync(""); + await context.Response.WriteAsync(""); + await context.Response.WriteAsync("
"); + + await context.Response.WriteAsync(""); + }); + } + } +} diff --git a/samples/HotAddSample/project.json b/samples/HotAddSample/project.json new file mode 100644 index 0000000..54bd01b --- /dev/null +++ b/samples/HotAddSample/project.json @@ -0,0 +1,23 @@ +{ + "webroot": "wwwroot", + "version": "1.0.0-*", + "exclude": [ + "wwwroot" + ], + "packExclude": [ + "**.kproj", + "**.user", + "**.vspscc" + ], + "dependencies": { + "Microsoft.AspNet.Hosting": "1.0.0-*", + "Microsoft.AspNet.Server.WebListener": "1.0.0-*" + }, + "commands": { + "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:12345" + }, + "frameworks" : { + "aspnet50" : { }, + "aspnetcore50" : { } + } +} diff --git a/samples/SelfHostServer/project.json b/samples/SelfHostServer/project.json index cbb2e6e..5d32685 100644 --- a/samples/SelfHostServer/project.json +++ b/samples/SelfHostServer/project.json @@ -3,7 +3,7 @@ "Microsoft.AspNet.Http": "1.0.0-*", "Microsoft.AspNet.Server.WebListener": "1.0.0-*" }, - "commands": { "web": "Microsoft.AspNet.Hosting --server=Microsoft.AspNet.Server.WebListener --server.urls=http://localhost:8080" }, + "commands": { "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:8080" }, "frameworks": { "aspnet50": { }, "aspnetcore50": { }