| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using CoreWCF.Channels; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Configuration |
| | | 9 | | { |
| | | 10 | | internal class ConfigurationHolder : IConfigurationHolder |
| | | 11 | | { |
| | 40 | 12 | | private readonly IDictionary<string, Binding> _bindings = new Dictionary<string, Binding>(); |
| | 40 | 13 | | private readonly ISet<ServiceEndpoint> _endpoints = new HashSet<ServiceEndpoint>(); |
| | | 14 | | private readonly IServiceProvider _provider; |
| | | 15 | | private readonly IBindingFactory _factoryBinding; |
| | | 16 | | |
| | 51 | 17 | | public ISet<ServiceEndpoint> Endpoints => _endpoints; |
| | | 18 | | |
| | 40 | 19 | | public ConfigurationHolder(IServiceProvider serviceProvider, IBindingFactory factory) |
| | | 20 | | { |
| | 40 | 21 | | _provider = serviceProvider; |
| | 40 | 22 | | _factoryBinding = factory; |
| | 40 | 23 | | } |
| | | 24 | | |
| | | 25 | | public void AddBinding(Binding binding) |
| | | 26 | | { |
| | 37 | 27 | | _bindings.Add(binding.Name, binding); |
| | 37 | 28 | | } |
| | | 29 | | |
| | | 30 | | public Binding ResolveBinding(string bindingType, string name, string bindingNamespace = null) |
| | | 31 | | { |
| | 55 | 32 | | if (string.IsNullOrEmpty(bindingType)) |
| | | 33 | | { |
| | 0 | 34 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(bindingType); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | Binding binding; |
| | 55 | 38 | | if (string.IsNullOrEmpty(name)) |
| | | 39 | | { |
| | 6 | 40 | | binding = _factoryBinding.Create(bindingType); |
| | | 41 | | } |
| | 49 | 42 | | else if (!_bindings.TryGetValue(name, out binding)) |
| | | 43 | | { |
| | 1 | 44 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new BindingNotFoundException()); |
| | | 45 | | } |
| | | 46 | | |
| | 54 | 47 | | SetBindingNamespace(bindingNamespace, binding); |
| | | 48 | | |
| | 54 | 49 | | return binding; |
| | | 50 | | |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public IXmlConfigEndpoint GetXmlConfigEndpoint(ServiceEndpoint endpoint) |
| | | 54 | | { |
| | 22 | 55 | | if (endpoint == null) |
| | | 56 | | { |
| | 0 | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpoint)); |
| | | 58 | | } |
| | | 59 | | |
| | 22 | 60 | | Type contract = ServiceReflector.ResolveTypeFromName(endpoint.Contract); |
| | 22 | 61 | | Type service = ServiceReflector.ResolveTypeFromName(endpoint.ServiceName); |
| | 22 | 62 | | Binding binding = ResolveBinding(endpoint.Binding, endpoint.BindingConfiguration, endpoint.BindingNamespace) |
| | 22 | 63 | | return new XmlConfigEndpoint(service, contract, binding, endpoint.Address); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public void AddServiceEndpoint(string name, string serviceName, Uri address, string contract, string bindingType |
| | | 67 | | { |
| | 13 | 68 | | var endpoint = new ServiceEndpoint |
| | 13 | 69 | | { |
| | 13 | 70 | | ServiceName = serviceName, |
| | 13 | 71 | | Address = address, |
| | 13 | 72 | | Binding = bindingType, |
| | 13 | 73 | | Contract = contract, |
| | 13 | 74 | | Name = name, |
| | 13 | 75 | | BindingConfiguration = bindingName, |
| | 13 | 76 | | BindingNamespace = bindingNamespace |
| | 13 | 77 | | }; |
| | | 78 | | |
| | 13 | 79 | | _endpoints.Add(endpoint); |
| | 13 | 80 | | } |
| | | 81 | | |
| | | 82 | | private static void SetBindingNamespace(string bindingNamespace, Binding binding) |
| | | 83 | | { |
| | 54 | 84 | | if (binding != null && bindingNamespace != null) |
| | | 85 | | { |
| | 23 | 86 | | binding.Namespace = bindingNamespace; |
| | | 87 | | } |
| | 54 | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |