3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
#if NET
6
+ using System . Diagnostics ;
7
+ using System . Linq ;
8
+ using System . Net ;
6
9
using System . Security . Claims ;
10
+ using System . Security . Cryptography . X509Certificates ;
7
11
using CoreWCF . Configuration ;
8
12
using CoreWCF . Description ;
9
13
using idunno . Authentication . Basic ;
@@ -26,7 +30,7 @@ internal class TestDefinitionHelper
26
30
private const int DefaultWebSocketPort = 8083 ;
27
31
private const int DefaultWebSocketSPort = 8084 ;
28
32
29
- private static IDictionary < ServiceSchema , string > BaseAddresses
33
+ internal static IDictionary < ServiceSchema , string > BaseAddresses
30
34
{
31
35
get
32
36
{
@@ -57,10 +61,47 @@ private static IDictionary<ServiceSchema, string> BaseAddresses
57
61
}
58
62
59
63
#if NET
64
+ internal class ServiceTestHostOptions
65
+ {
66
+ public List < Uri > serviceBaseAddresses = new List < Uri > ( ) ;
67
+ public Dictionary < Enum , string > endpointBasePath = new Dictionary < Enum , string > ( ) ;
68
+ }
69
+
60
70
internal static async Task StartHosts ( )
61
71
{
62
72
bool success = true ;
63
- var webHost = WebHost . CreateDefaultBuilder ( )
73
+ var serviceTestHostOptionsDict = new Dictionary < string , ServiceTestHostOptions > ( ) ;
74
+
75
+ var webHost = new WebHostBuilder ( )
76
+ . UseKestrel ( options => {
77
+ options . Listen ( IPAddress . IPv6Any , new Uri ( BaseAddresses [ ServiceSchema . HTTP ] ) . Port ) ;
78
+ options . Listen ( address : IPAddress . IPv6Any , new Uri ( BaseAddresses [ ServiceSchema . HTTPS ] ) . Port , listenOptions =>
79
+ {
80
+ listenOptions . UseHttps ( httpsOptions =>
81
+ {
82
+ X509Certificate2 cert = TestHost . CertificateFromFriendlyName ( StoreName . My , StoreLocation . LocalMachine , "WCF Bridge - Machine certificate generated by the CertificateManager" ) ;
83
+ httpsOptions . ServerCertificate = cert ;
84
+ } ) ;
85
+ if ( Debugger . IsAttached )
86
+ {
87
+ listenOptions . UseConnectionLogging ( ) ;
88
+ }
89
+ } ) ;
90
+ options . Listen ( IPAddress . IPv6Any , new Uri ( BaseAddresses [ ServiceSchema . WS ] ) . Port ) ;
91
+ options . Listen ( address : IPAddress . IPv6Any , new Uri ( BaseAddresses [ ServiceSchema . WSS ] ) . Port , listenOptions =>
92
+ {
93
+ listenOptions . UseHttps ( httpsOptions =>
94
+ {
95
+ X509Certificate2 cert = TestHost . CertificateFromFriendlyName ( StoreName . My , StoreLocation . LocalMachine , "WCF Bridge - Machine certificate generated by the CertificateManager" ) ;
96
+ httpsOptions . ServerCertificate = cert ;
97
+ } ) ;
98
+ if ( Debugger . IsAttached )
99
+ {
100
+ listenOptions . UseConnectionLogging ( ) ;
101
+ }
102
+ } ) ;
103
+ } )
104
+ . UseNetTcp ( IPAddress . IPv6Any , new Uri ( BaseAddresses [ ServiceSchema . NETTCP ] ) . Port )
64
105
. ConfigureServices ( services =>
65
106
{
66
107
services . AddAuthentication ( BasicAuthenticationDefaults . AuthenticationScheme )
@@ -100,8 +141,7 @@ internal static async Task StartHosts()
100
141
{
101
142
foreach ( var serviceTestHost in GetAttributedServiceHostTypes ( ) )
102
143
{
103
- var serviceBaseAddresses = new List < Uri > ( ) ;
104
- var endpointBasePath = new Dictionary < Enum , string > ( ) ;
144
+ var serviceTestHostOptions = new ServiceTestHostOptions ( ) ;
105
145
foreach ( TestServiceDefinitionAttribute attr in serviceTestHost . GetCustomAttributes ( typeof ( TestServiceDefinitionAttribute ) , false ) )
106
146
{
107
147
Uri serviceBaseAddress = null ;
@@ -111,9 +151,9 @@ internal static async Task StartHosts()
111
151
{
112
152
if ( attr . Schema . HasFlag ( schema ) )
113
153
{
114
- endpointBasePath . Add ( schema , attr . BasePath ) ;
154
+ serviceTestHostOptions . endpointBasePath . Add ( schema , attr . BasePath ) ;
115
155
serviceBaseAddress = new Uri ( BaseAddresses [ ( ServiceSchema ) schema ] ) ;
116
- serviceBaseAddresses . Add ( serviceBaseAddress ) ;
156
+ serviceTestHostOptions . serviceBaseAddresses . Add ( serviceBaseAddress ) ;
117
157
}
118
158
}
119
159
}
@@ -132,30 +172,37 @@ internal static async Task StartHosts()
132
172
}
133
173
}
134
174
175
+ serviceTestHostOptionsDict . Add ( serviceTestHost . Name , serviceTestHostOptions ) ;
135
176
try
136
177
{
137
178
if ( success )
138
179
{
139
- var serviceHost = ( ServiceHost ) Activator . CreateInstance ( serviceTestHost , serviceBaseAddresses . ToArray ( ) ) ;
180
+ string serviceHostTypeName = serviceTestHost . Name ;
181
+ var serviceHost = ( ServiceHost ) Activator . CreateInstance ( serviceTestHost , serviceTestHostOptionsDict [ serviceHostTypeName ] . serviceBaseAddresses . ToArray ( ) ) ;
140
182
serviceBuilder . AddService ( serviceHost . ServiceType , options =>
141
183
{
142
- foreach ( var baseAddress in serviceBaseAddresses )
184
+ var localHostTypeName = serviceHostTypeName ;
185
+ //options.BaseAddresses.Clear();
186
+ foreach ( var baseAddress in serviceTestHostOptionsDict [ localHostTypeName ] . serviceBaseAddresses )
143
187
{
144
188
if ( ! options . BaseAddresses . Contains ( baseAddress ) )
145
189
options . BaseAddresses . Add ( baseAddress ) ;
146
190
}
191
+
192
+ foreach ( var endpoint in serviceHost . Endpoints )
193
+ {
194
+ Enum schema = ServiceHostHelper . ToServiceSchema ( endpoint . Binding . Scheme ) ;
195
+ string basePath = serviceTestHostOptionsDict [ localHostTypeName ] . endpointBasePath [ schema ] ;
196
+ string endpointAddress = string . Format ( "{0}/{1}" , basePath , endpoint . Address ) ;
197
+ serviceBuilder . AddServiceEndpoint ( serviceHost . ServiceType , endpoint . ContractType , endpoint . Binding , new Uri ( endpointAddress , UriKind . RelativeOrAbsolute ) , null ) ;
198
+ }
147
199
} ) ;
148
- foreach ( var endpoint in serviceHost . Endpoints )
149
- {
150
- Enum schema = ServiceHostHelper . ToServiceSchema ( endpoint . Binding . Scheme ) ;
151
- string basePath = endpointBasePath [ schema ] ;
152
- string endpointAddress = string . Format ( "{0}/{1}" , basePath , endpoint . Address ) ;
153
- serviceBuilder . AddServiceEndpoint ( serviceHost . ServiceType , endpoint . ContractType , endpoint . Binding , new Uri ( endpointAddress , UriKind . RelativeOrAbsolute ) , null ) ;
154
- }
200
+
155
201
serviceBuilder . ConfigureServiceHostBase ( serviceHost . ServiceType , serviceHostBase =>
156
202
{
203
+ var localHostTypeName = serviceHostTypeName ;
157
204
var smb = serviceHostBase . Description . Behaviors . Find < ServiceMetadataBehavior > ( ) ;
158
- if ( serviceBaseAddresses . Where ( uri => uri . Scheme == Uri . UriSchemeHttps ) . Any ( ) )
205
+ if ( serviceTestHostOptionsDict [ localHostTypeName ] . serviceBaseAddresses . Where ( uri => uri . Scheme == Uri . UriSchemeHttps ) . Any ( ) )
159
206
{
160
207
smb . HttpsGetEnabled = true ;
161
208
}
0 commit comments