-
Notifications
You must be signed in to change notification settings - Fork 50
/
KestrelCommunicationListener.cs
112 lines (102 loc) · 6.69 KB
/
KestrelCommunicationListener.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------
namespace Microsoft.ServiceFabric.Services.Communication.AspNetCore
{
using System;
using System.Fabric;
using System.Globalization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
/// <summary>
/// An AspNetCore Kestrel based communication listener for Service Fabric stateless or stateful service.
/// </summary>
public class KestrelCommunicationListener : AspNetCoreCommunicationListener
{
private readonly string endpointName;
/// <summary>
/// Initializes a new instance of the <see cref="KestrelCommunicationListener"/> class using a default address with http protocol and port 0.
/// Kestrel will dynamically bind to an unspecified, available port when port 0 is specified in url.
/// </summary>
/// <param name="serviceContext">The context of the service for which this communication listener is being constructed.</param>
/// <param name="build">Delegate to build Microsoft.AspNetCore.Hosting.IWebHost, endpoint url generated by the listener is given as input to this delegate.
/// This gives the flexibility to change the url before creating Microsoft.AspNetCore.Hosting.IWebHost if needed.</param>
public KestrelCommunicationListener(ServiceContext serviceContext, Func<string, AspNetCoreCommunicationListener, IWebHost> build)
: this(serviceContext, null, build)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="KestrelCommunicationListener"/> class using a default address with http protocol and port 0.
/// Kestrel will dynamically bind to an unspecified, available port when port 0 is specified in url.
/// </summary>
/// <param name="serviceContext">The context of the service for which this communication listener is being constructed.</param>
/// <param name="build">Delegate to build Microsoft.Extensions.Hosting.IHost, endpoint url generated by the listener is given as input to this delegate.
/// This gives the flexibility to change the url before creating Microsoft.Extensions.Hosting.IHost if needed.</param>
public KestrelCommunicationListener(ServiceContext serviceContext, Func<string, AspNetCoreCommunicationListener, IHost> build)
: this(serviceContext, null, build)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="KestrelCommunicationListener"/> class.
/// </summary>
/// <param name="serviceContext">The context of the service for which this communication listener is being constructed.</param>
/// <param name="endpointName">Name of endpoint resource defined in service manifest that should be used to create the address for listener.
/// Protocol and port specified in this endpoint is used to create the url.
/// If the endpointName is null, a default address with http protocol and port 0 will be used.
/// Kestrel will dynamically bind to an unspecified, available port when port 0 is specified in url.
/// If the specified endpointName is not found in service manifest, an InvalidOperationException indicating this will be thrown.</param>
/// <param name="build">Delegate to build Microsoft.AspNetCore.Hosting.IWebHost, endpoint url generated by the listener is given as input to this delegate.
/// This gives the flexibility to change the url before creating Microsoft.AspNetCore.Hosting.IWebHost if needed.</param>
public KestrelCommunicationListener(ServiceContext serviceContext, string endpointName, Func<string, AspNetCoreCommunicationListener, IWebHost> build)
: base(serviceContext, build)
{
if (endpointName?.Length == 0)
{
throw new ArgumentException(SR.EndpointNameEmptyExceptionMessage);
}
this.endpointName = endpointName;
}
/// <summary>
/// Initializes a new instance of the <see cref="KestrelCommunicationListener"/> class.
/// </summary>
/// <param name="serviceContext">The context of the service for which this communication listener is being constructed.</param>
/// <param name="endpointName">Name of endpoint resource defined in service manifest that should be used to create the address for listener.
/// Protocol and port specified in this endpoint is used to create the url.
/// If the endpointName is null, a default address with http protocol and port 0 will be used.
/// Kestrel will dynamically bind to an unspecified, available port when port 0 is specified in url.
/// If the specified endpointName is not found in service manifest, an InvalidOperationException indicating this will be thrown.</param>
/// <param name="build">Delegate to build Microsoft.Extensions.Hosting.IHost, endpoint url generated by the listener is given as input to this delegate.
/// This gives the flexibility to change the url before creating Microsoft.Extensions.Hosting.IHost if needed.</param>
public KestrelCommunicationListener(ServiceContext serviceContext, string endpointName, Func<string, AspNetCoreCommunicationListener, IHost> build)
: base(serviceContext, build)
{
if (endpointName?.Length == 0)
{
throw new ArgumentException(SR.EndpointNameEmptyExceptionMessage);
}
this.endpointName = endpointName;
}
/// <summary>
/// Gets url for the listener. Listener url is created using the endpointName passed in the constructor.
/// If the endpointName was null, a default url with http protocol and port zero is returned.
/// </summary>
/// <returns>url for the listener.</returns>
protected override string GetListenerUrl()
{
// url with WebServer is always registered as http://+:port.
var listenUrl = "http://+:0";
// Get protocol and port from endpoint resource if specified.
if (this.endpointName != null)
{
var serviceEndpoint = this.GetEndpointResourceDescription(this.endpointName);
listenUrl = string.Format(
CultureInfo.InvariantCulture,
"{0}://+:{1}",
serviceEndpoint.Protocol.ToString().ToLowerInvariant(),
serviceEndpoint.Port);
}
return listenUrl;
}
}
}