File: ServiceConfiguration.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (348 lines) | stat: -rw-r--r-- 14,249 bytes parent folder | download | duplicates (9)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// <copyright>
//   Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>

namespace System.ServiceModel
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.IdentityModel.Configuration;
    using System.Linq;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;

    /// <summary>
    /// Facade over ServiceHost to use in programmatic configuration.
    /// </summary>
    public class ServiceConfiguration
    {
        private ServiceHost host;

        // ServiceConfiguration is a facade over a ServiceHost
        internal ServiceConfiguration(ServiceHost host)
        {
            CheckArgument(host, "host");
            this.host = host;
        }

        /// <summary>
        /// Gets a complete, in-memory description of the service being configured, including all the endpoints for the service and specifications for their respective addresses, bindings, contracts and behaviors.
        /// </summary>
        public ServiceDescription Description
        {
            get
            {
                return this.host.Description;
            }
        }

        /// <summary>
        /// Gets the ServiceAuthenticationBehavior currently enabled on the service's Description.
        /// </summary>
        public ServiceAuthenticationBehavior Authentication
        {
            get
            {
                return this.host.Authentication;
            }
        }

        /// <summary>
        /// Gets the ServiceAuthorizationBehavior currently enabled on the service's Description.
        /// </summary>
        public ServiceAuthorizationBehavior Authorization
        {
            get
            {
                return this.host.Authorization;
            }
        }

        /// <summary>
        /// Gets the ServiceCredentials currently enabled on the service's Description.
        /// </summary>
        public ServiceCredentials Credentials
        {
            get
            {
                return this.host.Credentials;
            }
        }

        /// <summary>
        /// Gets the base addresses for the service as specified by the underlying ServiceHost.
        /// </summary>
        public ReadOnlyCollection<Uri> BaseAddresses
        {
            get
            {
                return this.host.BaseAddresses;
            }
        }

        /// <summary>
        /// Gets or sets OpenTimeout for the underlying ServiceHost
        /// </summary>
        public TimeSpan OpenTimeout
        {
            get
            {
                return this.host.OpenTimeout;
            }

            set
            {
                this.host.OpenTimeout = value;
            }
        }

        /// <summary>
        /// Gets or sets CloseTimeout for the underlying ServiceHost
        /// </summary>
        public TimeSpan CloseTimeout
        {
            get
            {
                return this.host.CloseTimeout;
            }

            set
            {
                this.host.CloseTimeout = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether to use token handlers
        /// </summary>
        public bool UseIdentityConfiguration
        {
            get
            {
                return this.Credentials.UseIdentityConfiguration;
            }

            set
            {
                this.Credentials.UseIdentityConfiguration = value;
            }
        }

        /// <summary>
        /// Gets or sets IdentityConfiguration for the underlying ServiceHost
        /// </summary>
        public IdentityConfiguration IdentityConfiguration
        {
            get
            {
                return this.Credentials.IdentityConfiguration;
            }
            
            set
            {
                this.Credentials.IdentityConfiguration = value;
            }
        }

        /// <summary>
        /// Validate a service endpoint and add it to Description
        /// </summary>
        /// <param name="endpoint">endpoint to add</param>
        public void AddServiceEndpoint(ServiceEndpoint endpoint)
        {
            CheckArgument(endpoint, "endpoint");

            // Do some other checks to match ServiceHostBase.AddServiceEndpoint
            if ((this.host.State != CommunicationState.Created) && (this.host.State != CommunicationState.Opening))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString("SFxServiceHostBaseCannotAddEndpointAfterOpen")));
            }

            if (this.Description == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString("SFxServiceHostBaseCannotAddEndpointWithoutDescription")));
            }

            if (endpoint.Address == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString("SFxEndpointAddressNotSpecified"));
            }

            if (endpoint.Contract == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString("SFxEndpointContractNotSpecified"));
            }

            if (endpoint.Binding == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString("SFxEndpointBindingNotSpecified"));
            }

            if (!endpoint.IsSystemEndpoint || (endpoint.Contract.ContractType == typeof(IMetadataExchange)))
            {
                // Throw if contract is not valid for this service
                //   i.e. if contract is not implemented by service, unless endpoint is a standard endpoint 
                //   note: (metadata endpoints require metadata behavior to implement IMetadataExchange even though it's a standard endpoint)            
                IContractResolver resolver = this.host.GetContractResolver(this.host.ImplementedContracts);
                ConfigLoader configLoader = new ConfigLoader(resolver);
                configLoader.LookupContract(endpoint.Contract.ConfigurationName, this.Description.Name); // throws on failure
            }

            this.Description.Endpoints.Add(endpoint);
        }

        /// <summary>
        /// Create a new service endpoint and add it to Description
        /// </summary>
        /// <param name="contractType">interface annotated with [ServiceContract]</param>
        /// <param name="binding">protocol to use for communication</param>
        /// <param name="address">absolute address for service, or address relative to base address for supplied binding</param>
        /// <returns>The endpoint which was created</returns>
        public ServiceEndpoint AddServiceEndpoint(Type contractType, Binding binding, string address)
        {
            CheckArgument(address, "address");
            return this.AddServiceEndpoint(contractType, binding, new Uri(address, UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// Create a new service endpoint and add it to Description
        /// </summary>
        /// <param name="contractType">interface annotated with [ServiceContract]</param>
        /// <param name="binding">protocol to use for communication</param>
        /// <param name="address">absolute address for service, or address relative to base address for supplied binding</param>
        /// <returns>The endpoint which was created</returns>
        public ServiceEndpoint AddServiceEndpoint(Type contractType, Binding binding, Uri address)
        {
            CheckArgument(contractType, "contractType");
            CheckArgument(binding, "binding");
            CheckArgument(address, "address");

            ContractDescription contract = this.host.ImplementedContracts == null
                ? null
                : this.host.ImplementedContracts.Values.FirstOrDefault(implementedContract => implementedContract.ContractType == contractType);
            if (contract == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("contractType", SR.GetString(SR.SFxMethodNotSupportedByType2, this.host.Description.ServiceType, contractType));
            }

            ServiceEndpoint endpoint = new ServiceEndpoint(contract, binding, new EndpointAddress(ServiceHost.MakeAbsoluteUri(address, binding, this.host.InternalBaseAddresses)));
            this.AddServiceEndpoint(endpoint);
            return endpoint;
        }

        /// <summary>
        /// Create a new service endpoint and add it to Description
        /// </summary>
        /// <param name="contractType">interface annotated with [ServiceContract]</param>
        /// <param name="binding">protocol to use for communication</param>
        /// <param name="address">absolute logical address for service, or address relative to base address for supplied binding</param>
        /// <param name="listenUri">absolute physical address for service, or address relative to base address for supplied binding</param>
        /// <returns>The endpoint which was created</returns>
        public ServiceEndpoint AddServiceEndpoint(Type contractType, Binding binding, string address, Uri listenUri)
        {
            CheckArgument(listenUri, "listenUri");

            ServiceEndpoint endpoint = this.AddServiceEndpoint(contractType, binding, address);
            this.SetListenUri(endpoint, binding, listenUri);
            return endpoint;
        }

        /// <summary>
        /// Create a new service endpoint and add it to Description
        /// </summary>
        /// <param name="contractType">interface annotated with [ServiceContract]</param>
        /// <param name="binding">protocol to use for communication</param>
        /// <param name="address">absolute logical address for service, or address relative to base address for supplied binding</param>
        /// <param name="listenUri">absolute physical address for service, or address relative to base address for supplied binding</param>
        /// <returns>The endpoint which was created</returns>
        public ServiceEndpoint AddServiceEndpoint(Type contractType, Binding binding, Uri address, Uri listenUri)
        {
            CheckArgument(listenUri, "listenUri");

            ServiceEndpoint endpoint = this.AddServiceEndpoint(contractType, binding, address);
            this.SetListenUri(endpoint, binding, listenUri);
            return endpoint;
        }

        /// <summary>
        /// Convenience method to compute and set endpoint's address
        /// </summary>
        /// <param name="endpoint">endpoint to set</param>
        /// <param name="relativeAddress">address relative to the ServiceHost's base address, if any, for endpoint's current binding</param>
        public void SetEndpointAddress(ServiceEndpoint endpoint, string relativeAddress)
        {
            CheckArgument(endpoint, "endpoint");
            CheckArgument(relativeAddress, "relativeAddress");

            this.host.SetEndpointAddress(endpoint, relativeAddress);
        }

        /// <summary>
        /// Automatically add endpoints for all of a service's enabled contracts, for all of its enabled base addresses that match the specified binding.
        /// </summary>
        /// <param name="protocol">Binding to add endpoints for</param>
        /// <returns>Endpoints created</returns>
        public Collection<ServiceEndpoint> EnableProtocol(Binding protocol)
        {
            CheckArgument(protocol, "protocol");
            Collection<ServiceEndpoint> generatedEndpoints = new Collection<ServiceEndpoint>();

            if (this.host.ImplementedContracts != null)
            {
                // don't generate endpoints for contracts that serve as the base type for other reflected contracts            
                IEnumerable<ContractDescription> contracts = this.host.ImplementedContracts.Values;
                IEnumerable<ContractDescription> mostSpecificContracts = contracts.Where(contract
                    => contracts.All(otherContract
                        => object.ReferenceEquals(contract, otherContract)
                            || !contract.ContractType.IsAssignableFrom(otherContract.ContractType)));

                foreach (var uri in this.host.BaseAddresses)
                {
                    if (uri.Scheme.Equals(protocol.Scheme))
                    {
                        foreach (ContractDescription contract in mostSpecificContracts)
                        {
                            ServiceEndpoint endpoint = new ServiceEndpoint(contract, protocol, new EndpointAddress(uri));
                            this.AddServiceEndpoint(endpoint);
                            generatedEndpoints.Add(endpoint);
                        }
                    }
                }
            }

            return generatedEndpoints;
        }

        /// <summary>
        /// Load endpoints and behaviors into service Description from current AppDomain's configuration
        /// </summary>
        public void LoadFromConfiguration()
        {
            this.host.LoadFromConfiguration();
        }

        /// <summary>
        /// Load endpoints and behaviors into service Description from supplied configuration
        /// </summary>
        /// <param name="configuration">configuration to load from</param>
        public void LoadFromConfiguration(System.Configuration.Configuration configuration)
        {
            CheckArgument(configuration, "configuration");
            this.host.LoadFromConfiguration(configuration);
        }

        private static void CheckArgument<T>(T argument, string argumentName)
        {
            if (argument == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(argumentName);
            }
        }

        private void SetListenUri(ServiceEndpoint endpoint, Binding binding, Uri listenUri)
        {
            endpoint.UnresolvedListenUri = listenUri;
            endpoint.ListenUri = ServiceHost.MakeAbsoluteUri(listenUri, binding, this.host.InternalBaseAddresses);
        }
    }
}