From 71f7faecbd91e7edf87ccd8a5ce9caec7802db43 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Tue, 24 Aug 2021 10:52:48 +0200 Subject: [PATCH] Adjust wcf to general API --- .../ConfiguredHostFactory.cs | 12 ++++++++++- .../ConfiguredServiceHost.cs | 21 ++++++++++--------- src/Moryx.Runtime.Wcf/ContainerExtensions.cs | 18 +++++++--------- src/Moryx.Runtime.Wcf/EndpointCollector.cs | 2 +- src/Moryx.Runtime.Wcf/HostFactoryConfig.cs | 2 ++ src/Moryx.Runtime.Wcf/IEndpointCollector.cs | 17 --------------- src/Moryx.Runtime.Wcf/VersionService.cs | 2 +- src/Moryx.Runtime.Wcf/WcfHostFactory.cs | 21 ++++++++++--------- .../Service/ServiceVersionAttribute.cs | 9 +++----- 9 files changed, 47 insertions(+), 57 deletions(-) delete mode 100644 src/Moryx.Runtime.Wcf/IEndpointCollector.cs diff --git a/src/Moryx.Runtime.Wcf/ConfiguredHostFactory.cs b/src/Moryx.Runtime.Wcf/ConfiguredHostFactory.cs index b684602b6..099fc3f1e 100644 --- a/src/Moryx.Runtime.Wcf/ConfiguredHostFactory.cs +++ b/src/Moryx.Runtime.Wcf/ConfiguredHostFactory.cs @@ -2,12 +2,13 @@ // Licensed under the Apache License, Version 2.0 using System; +using Moryx.Communication.Endpoints; using Moryx.Logging; using Moryx.Tools.Wcf; namespace Moryx.Runtime.Wcf { - internal class ConfiguredHostFactory : IConfiguredHostFactory + internal class ConfiguredHostFactory : IConfiguredHostFactory, IEndpointHostFactory { public IModuleLogger Logger { get; set; } @@ -28,5 +29,14 @@ public IConfiguredServiceHost CreateHost(Type contract, HostConfig config) { return _hostFactory.CreateHost(contract, config, Factory, Logger); } + + public IEndpointHost CreateHost(Type endpoint, object config) + { + var hostConfig = config as HostConfig; + if (hostConfig == null) + throw new ArgumentException("Wcf hosting requires config of type HostConfig"); + + return (IEndpointHost)_hostFactory.CreateHost(endpoint, hostConfig, Factory, Logger); + } } } diff --git a/src/Moryx.Runtime.Wcf/ConfiguredServiceHost.cs b/src/Moryx.Runtime.Wcf/ConfiguredServiceHost.cs index f0e99e122..f5f98df89 100644 --- a/src/Moryx.Runtime.Wcf/ConfiguredServiceHost.cs +++ b/src/Moryx.Runtime.Wcf/ConfiguredServiceHost.cs @@ -7,17 +7,19 @@ using System.ServiceModel.Channels; using System.ServiceModel.Description; using Moryx.Communication; +using Moryx.Communication.Endpoints; using Moryx.Logging; using Moryx.Tools.Wcf; +using Endpoint = Moryx.Tools.Wcf.Endpoint; namespace Moryx.Runtime.Wcf { - internal class ConfiguredServiceHost : IConfiguredServiceHost + internal class ConfiguredServiceHost : IConfiguredServiceHost, IEndpointHost { /// /// Di container of parent plugin for component resolution /// - private readonly IEndpointCollector _collector; + private readonly EndpointCollector _collector; private readonly PortConfig _portConfig; private readonly IModuleLogger _logger; @@ -30,10 +32,9 @@ internal class ConfiguredServiceHost : IConfiguredServiceHost private HostConfig _hostConfig; private Type _contract; private string _endpointAddress; - private ServiceVersionAttribute _endpointVersion; + private EndpointAttribute _endpointAttribute; - public ConfiguredServiceHost(ITypedHostFactory factory, IModuleLogger parentLogger, - IEndpointCollector endpointCollector, PortConfig portConfig) + public ConfiguredServiceHost(ITypedHostFactory factory, IModuleLogger parentLogger, EndpointCollector endpointCollector, PortConfig portConfig) { _factory = factory; _collector = endpointCollector; @@ -65,7 +66,7 @@ public void Setup(Type contract, HostConfig config) // Create service host _service = _factory.CreateServiceHost(contract); _contract = contract; - _endpointVersion = _contract.GetCustomAttribute(); + _endpointAttribute = _contract.GetCustomAttribute(); // Configure host _service.CloseTimeout = TimeSpan.Zero; @@ -164,19 +165,19 @@ public void Setup(Type contract, HostConfig config) public void Start() { _logger?.Log(LogLevel.Info, "Starting wcf service {0} with version {1}", _endpointAddress, - _endpointVersion?.Version ?? "1.0.0.0"); + _endpointAttribute?.Version ?? "1.0.0.0"); _service.Open(); - if (_endpointVersion != null) + if (_endpointAttribute != null) { _collector.AddEndpoint(_endpointAddress, new Endpoint { - Service = _contract.Name, + Service = _endpointAttribute.Name ?? _contract.Name, Path = _hostConfig.Endpoint, Address = _endpointAddress, Binding = _hostConfig.BindingType, - Version = _endpointVersion.Version, + Version = _endpointAttribute.Version, RequiresAuthentication = _hostConfig.RequiresAuthentification }); } diff --git a/src/Moryx.Runtime.Wcf/ContainerExtensions.cs b/src/Moryx.Runtime.Wcf/ContainerExtensions.cs index 6593dc119..c19adb56a 100644 --- a/src/Moryx.Runtime.Wcf/ContainerExtensions.cs +++ b/src/Moryx.Runtime.Wcf/ContainerExtensions.cs @@ -3,6 +3,7 @@ using System; using Castle.Facilities.WcfIntegration; +using Moryx.Communication.Endpoints; using Moryx.Container; using Moryx.Logging; using Moryx.Tools.Wcf; @@ -17,20 +18,15 @@ public static class ContainerExtensions /// /// Register wcf to the local module container /// - [Obsolete("Extension with WCF factory was replaced by IEndpointHosting.ConfigureFactory !")] - public static IContainer RegisterWcf(this IContainer container, - IWcfHostFactory wcfHostFactory) + [Obsolete("Extension with WCF factory was replaced by hosting independent ActivateHosting extension !")] + public static IContainer RegisterWcf(this IContainer container, IWcfHostFactory wcfHostFactory) { + // TODO: Move to WcfHostFactory on removal container.Extend(); container.Register(); - var logger = container.Resolve(); - var typedFactory = container.Resolve(); - - container.SetInstance((IConfiguredHostFactory)new ConfiguredHostFactory(wcfHostFactory) - { - Factory = typedFactory, - Logger = logger - }); + container.SetInstance(wcfHostFactory); + container.Register(); + container.Register(); return container; } } diff --git a/src/Moryx.Runtime.Wcf/EndpointCollector.cs b/src/Moryx.Runtime.Wcf/EndpointCollector.cs index 11dd32d63..f688f4381 100644 --- a/src/Moryx.Runtime.Wcf/EndpointCollector.cs +++ b/src/Moryx.Runtime.Wcf/EndpointCollector.cs @@ -9,7 +9,7 @@ namespace Moryx.Runtime.Wcf { - internal class EndpointCollector : IEndpointCollector + internal class EndpointCollector { private readonly Dictionary _endpoints = new Dictionary(); diff --git a/src/Moryx.Runtime.Wcf/HostFactoryConfig.cs b/src/Moryx.Runtime.Wcf/HostFactoryConfig.cs index 7f8093a11..d6f1287e8 100644 --- a/src/Moryx.Runtime.Wcf/HostFactoryConfig.cs +++ b/src/Moryx.Runtime.Wcf/HostFactoryConfig.cs @@ -1,6 +1,7 @@ // Copyright (c) 2020, Phoenix Contact GmbH & Co. KG // Licensed under the Apache License, Version 2.0 +using System; using System.Runtime.Serialization; using Moryx.Configuration; @@ -10,6 +11,7 @@ namespace Moryx.Runtime.Wcf /// Configuration for the host factory. /// [DataContract] + [Obsolete("The version service is required as soon as at least one WCF endpoint is availabe, so the flag is ignored now!")] public class HostFactoryConfig : ConfigBase { /// diff --git a/src/Moryx.Runtime.Wcf/IEndpointCollector.cs b/src/Moryx.Runtime.Wcf/IEndpointCollector.cs deleted file mode 100644 index c8cbf39b5..000000000 --- a/src/Moryx.Runtime.Wcf/IEndpointCollector.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) 2020, Phoenix Contact GmbH & Co. KG -// Licensed under the Apache License, Version 2.0 - -using System; -using Moryx.Tools.Wcf; - -namespace Moryx.Runtime.Wcf -{ - internal interface IEndpointCollector - { - Endpoint[] AllEndpoints { get; } - - void AddEndpoint(string address, Endpoint endpoint); - - void RemoveEndpoint(string address); - } -} \ No newline at end of file diff --git a/src/Moryx.Runtime.Wcf/VersionService.cs b/src/Moryx.Runtime.Wcf/VersionService.cs index 02bd1d5ef..5bbbc0a8c 100644 --- a/src/Moryx.Runtime.Wcf/VersionService.cs +++ b/src/Moryx.Runtime.Wcf/VersionService.cs @@ -11,7 +11,7 @@ namespace Moryx.Runtime.Wcf [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true)] internal class VersionService : IVersionService { - public IEndpointCollector Collector { get; set; } + public EndpointCollector Collector { get; set; } public Endpoint[] AllEndpoints() { diff --git a/src/Moryx.Runtime.Wcf/WcfHostFactory.cs b/src/Moryx.Runtime.Wcf/WcfHostFactory.cs index b6f93a27e..0c760c579 100644 --- a/src/Moryx.Runtime.Wcf/WcfHostFactory.cs +++ b/src/Moryx.Runtime.Wcf/WcfHostFactory.cs @@ -4,6 +4,7 @@ using System; using Castle.Facilities.WcfIntegration; using Moryx.Communication; +using Moryx.Communication.Endpoints; using Moryx.Configuration; using Moryx.Container; using Moryx.Logging; @@ -15,8 +16,8 @@ namespace Moryx.Runtime.Wcf /// /// Factory to create service hosts and provides hosts for the version service. /// - [InitializableKernelComponent(typeof(IWcfHostFactory))] - internal class WcfHostFactory : IWcfHostFactory, IInitializable, ILoggingHost + [InitializableKernelComponent(typeof(IWcfHostFactory), typeof(IEndpointHosting))] + internal class WcfHostFactory : IWcfHostFactory, IInitializable, ILoggingHost, IEndpointHosting { #region Dependencies @@ -42,16 +43,11 @@ public void Initialize() LoggerManagement.ActivateLogging(this); _container = new LocalContainer(); - var factoryConfig = ConfigManager.GetConfiguration(); _portConfig = ConfigManager.GetConfiguration(); - // In minimal core setups with no WCF service this can be disabled - if (factoryConfig.VersionServiceDisabled) - return; - _container.Register(nameof(VersionService), LifeCycle.Transient); - _container.Register(); - var collector = _container.Resolve(); + _container.Register(); + var collector = _container.Resolve(); _container.Extend(); _container.Register(); @@ -75,7 +71,7 @@ public IConfiguredServiceHost CreateHost(HostConfig config, ITypedHos public IConfiguredServiceHost CreateHost(Type contract, HostConfig config, ITypedHostFactory hostFactory, IModuleLogger logger) { - var collector = _container.Resolve(); + var collector = _container.Resolve(); // Create instance and fill using given container var host = new ConfiguredServiceHost(hostFactory, logger, collector, _portConfig); @@ -83,5 +79,10 @@ public IConfiguredServiceHost CreateHost(Type contract, HostConfig config, IType return host; } + + public void ActivateHosting(IContainer container) + { + container.RegisterWcf(this); + } } } \ No newline at end of file diff --git a/src/Moryx.Tools.Wcf/Service/ServiceVersionAttribute.cs b/src/Moryx.Tools.Wcf/Service/ServiceVersionAttribute.cs index ba371ab6e..26dfee267 100644 --- a/src/Moryx.Tools.Wcf/Service/ServiceVersionAttribute.cs +++ b/src/Moryx.Tools.Wcf/Service/ServiceVersionAttribute.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0 using System; +using Moryx.Communication.Endpoints; namespace Moryx.Tools.Wcf { @@ -9,7 +10,8 @@ namespace Moryx.Tools.Wcf /// Attribute used to declare a services version for clients to check compliance /// [AttributeUsage(AttributeTargets.Interface)] - public class ServiceVersionAttribute : Attribute + [Obsolete("Use hosting independent {EndpointAttribute} instead")] + public class ServiceVersionAttribute : EndpointAttribute { /// /// Set version of the service @@ -19,10 +21,5 @@ public ServiceVersionAttribute(string version) { Version = version; } - - /// - /// Version of the server - /// - public string Version { get; } } }