-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartHttpListenerCommand.cs
More file actions
54 lines (48 loc) · 1.41 KB
/
StartHttpListenerCommand.cs
File metadata and controls
54 lines (48 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
namespace SimpleHttpListener
{
using System;
using System.Management.Automation;
[Cmdlet(VerbsLifecycle.Start,"Listener")]
[OutputType(typeof(Listener))]
public class StartHttpListenerCommand : PSCmdlet
{
private Listener listener;
[Parameter(Mandatory = true)]
[ValidateNotNull()]
public Uri Prefix { get; set; }
[Parameter()]
public SwitchParameter AnyHost { get; set; }
[Parameter()]
public SwitchParameter RandomPort { get; set; }
public StartHttpListenerCommand()
{
}
protected override void BeginProcessing()
{
Uri prefix = Prefix;
if(RandomPort)
{
UriBuilder b = new UriBuilder(prefix);
b.Port = Listener.FindFreePort(16384, true);
prefix = b.Uri;
}
listener = new Listener(prefix, AnyHost);
}
protected override void ProcessRecord()
{
if (listener != null)
{
WriteVerbose("Start-Listener " + listener);
WriteObject(listener);
}
}
protected override void StopProcessing()
{
if (listener != null)
{
listener.Close();
}
base.StopProcessing();
}
}
}