Skip to content

Commit

Permalink
Adjust wcf to general API
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxantron committed Sep 16, 2021
1 parent 1caa5d5 commit 71f7fae
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 57 deletions.
12 changes: 11 additions & 1 deletion src/Moryx.Runtime.Wcf/ConfiguredHostFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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);
}
}
}
21 changes: 11 additions & 10 deletions src/Moryx.Runtime.Wcf/ConfiguredServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// Di container of parent plugin for component resolution
/// </summary>
private readonly IEndpointCollector _collector;
private readonly EndpointCollector _collector;
private readonly PortConfig _portConfig;
private readonly IModuleLogger _logger;

Expand All @@ -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;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void Setup(Type contract, HostConfig config)
// Create service host
_service = _factory.CreateServiceHost(contract);
_contract = contract;
_endpointVersion = _contract.GetCustomAttribute<ServiceVersionAttribute>();
_endpointAttribute = _contract.GetCustomAttribute<EndpointAttribute>();

// Configure host
_service.CloseTimeout = TimeSpan.Zero;
Expand Down Expand Up @@ -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
});
}
Expand Down
18 changes: 7 additions & 11 deletions src/Moryx.Runtime.Wcf/ContainerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Castle.Facilities.WcfIntegration;
using Moryx.Communication.Endpoints;
using Moryx.Container;
using Moryx.Logging;
using Moryx.Tools.Wcf;
Expand All @@ -17,20 +18,15 @@ public static class ContainerExtensions
/// <summary>
/// Register wcf to the local module container
/// </summary>
[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<WcfFacility>();
container.Register<ITypedHostFactory, TypedHostFactory>();
var logger = container.Resolve<IModuleLogger>();
var typedFactory = container.Resolve<ITypedHostFactory>();

container.SetInstance((IConfiguredHostFactory)new ConfiguredHostFactory(wcfHostFactory)
{
Factory = typedFactory,
Logger = logger
});
container.SetInstance(wcfHostFactory);
container.Register<IConfiguredHostFactory, ConfiguredHostFactory>();
container.Register<IEndpointHostFactory, ConfiguredHostFactory>();
return container;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Moryx.Runtime.Wcf/EndpointCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Moryx.Runtime.Wcf
{
internal class EndpointCollector : IEndpointCollector
internal class EndpointCollector
{
private readonly Dictionary<string, Endpoint> _endpoints = new Dictionary<string, Endpoint>();

Expand Down
2 changes: 2 additions & 0 deletions src/Moryx.Runtime.Wcf/HostFactoryConfig.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,6 +11,7 @@ namespace Moryx.Runtime.Wcf
/// Configuration for the host factory.
/// </summary>
[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
{
/// <summary>
Expand Down
17 changes: 0 additions & 17 deletions src/Moryx.Runtime.Wcf/IEndpointCollector.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Moryx.Runtime.Wcf/VersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
21 changes: 11 additions & 10 deletions src/Moryx.Runtime.Wcf/WcfHostFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,8 +16,8 @@ namespace Moryx.Runtime.Wcf
/// <summary>
/// Factory to create service hosts and provides hosts for the version service.
/// </summary>
[InitializableKernelComponent(typeof(IWcfHostFactory))]
internal class WcfHostFactory : IWcfHostFactory, IInitializable, ILoggingHost
[InitializableKernelComponent(typeof(IWcfHostFactory), typeof(IEndpointHosting))]
internal class WcfHostFactory : IWcfHostFactory, IInitializable, ILoggingHost, IEndpointHosting
{
#region Dependencies

Expand All @@ -42,16 +43,11 @@ public void Initialize()
LoggerManagement.ActivateLogging(this);

_container = new LocalContainer();
var factoryConfig = ConfigManager.GetConfiguration<HostFactoryConfig>();
_portConfig = ConfigManager.GetConfiguration<PortConfig>();

// In minimal core setups with no WCF service this can be disabled
if (factoryConfig.VersionServiceDisabled)
return;

_container.Register<IVersionService, VersionService>(nameof(VersionService), LifeCycle.Transient);
_container.Register<IEndpointCollector, EndpointCollector>();
var collector = _container.Resolve<IEndpointCollector>();
_container.Register<EndpointCollector>();
var collector = _container.Resolve<EndpointCollector>();

_container.Extend<WcfFacility>();
_container.Register<ITypedHostFactory, TypedHostFactory>();
Expand All @@ -75,13 +71,18 @@ public IConfiguredServiceHost CreateHost<TContract>(HostConfig config, ITypedHos
public IConfiguredServiceHost CreateHost(Type contract, HostConfig config, ITypedHostFactory hostFactory,
IModuleLogger logger)
{
var collector = _container.Resolve<IEndpointCollector>();
var collector = _container.Resolve<EndpointCollector>();

// Create instance and fill using given container
var host = new ConfiguredServiceHost(hostFactory, logger, collector, _portConfig);
host.Setup(contract, config);

return host;
}

public void ActivateHosting(IContainer container)
{
container.RegisterWcf(this);
}
}
}
9 changes: 3 additions & 6 deletions src/Moryx.Tools.Wcf/Service/ServiceVersionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
// Licensed under the Apache License, Version 2.0

using System;
using Moryx.Communication.Endpoints;

namespace Moryx.Tools.Wcf
{
/// <summary>
/// Attribute used to declare a services version for clients to check compliance
/// </summary>
[AttributeUsage(AttributeTargets.Interface)]
public class ServiceVersionAttribute : Attribute
[Obsolete("Use hosting independent {EndpointAttribute} instead")]
public class ServiceVersionAttribute : EndpointAttribute
{
/// <summary>
/// Set version of the service
Expand All @@ -19,10 +21,5 @@ public ServiceVersionAttribute(string version)
{
Version = version;
}

/// <summary>
/// Version of the server
/// </summary>
public string Version { get; }
}
}

0 comments on commit 71f7fae

Please sign in to comment.