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