From 56eff03ec4b358c931db09e8f4454e0a0058a2b1 Mon Sep 17 00:00:00 2001 From: andreniggemann <54938689+andreniggemann@users.noreply.github.com> Date: Mon, 16 May 2022 15:42:45 +0200 Subject: [PATCH 01/12] Inherit ActiveLevel from Parentlogger. --- src/Moryx/Logging/Logger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Moryx/Logging/Logger.cs b/src/Moryx/Logging/Logger.cs index 17ef5c7c1..b7270dea4 100644 --- a/src/Moryx/Logging/Logger.cs +++ b/src/Moryx/Logging/Logger.cs @@ -136,7 +136,7 @@ public IModuleLogger GetChild(string name, Type targetType) var config = _config.ChildConfigs.FirstOrDefault(item => item.LoggerName == childName); if (config == null) { - config = new ModuleLoggerConfig { LoggerName = childName }; + config = new ModuleLoggerConfig { LoggerName = childName, ActiveLevel = Parent.ActiveLevel }; _config.ChildConfigs.Add(config); } From a3847214cf059a91d97fd1bb0185cece8098c34e Mon Sep 17 00:00:00 2001 From: andreniggemann <54938689+andreniggemann@users.noreply.github.com> Date: Tue, 17 May 2022 15:29:27 +0200 Subject: [PATCH 02/12] Fixed null reference exception when no Parent is present --- src/Moryx/Logging/Logger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Moryx/Logging/Logger.cs b/src/Moryx/Logging/Logger.cs index b7270dea4..20da90dcb 100644 --- a/src/Moryx/Logging/Logger.cs +++ b/src/Moryx/Logging/Logger.cs @@ -136,7 +136,7 @@ public IModuleLogger GetChild(string name, Type targetType) var config = _config.ChildConfigs.FirstOrDefault(item => item.LoggerName == childName); if (config == null) { - config = new ModuleLoggerConfig { LoggerName = childName, ActiveLevel = Parent.ActiveLevel }; + config = new ModuleLoggerConfig { LoggerName = childName, ActiveLevel = Parent?.ActiveLevel ?? ActiveLevel.Info }; _config.ChildConfigs.Add(config); } From 9540b280b3b8a436a4a748ce0cb362b655ef5a6f Mon Sep 17 00:00:00 2001 From: andreniggemann <54938689+andreniggemann@users.noreply.github.com> Date: Thu, 19 May 2022 09:54:02 +0200 Subject: [PATCH 03/12] Fixed build I really should have done this in an IDE --- src/Moryx/Logging/Logger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Moryx/Logging/Logger.cs b/src/Moryx/Logging/Logger.cs index 20da90dcb..f641c3ade 100644 --- a/src/Moryx/Logging/Logger.cs +++ b/src/Moryx/Logging/Logger.cs @@ -136,7 +136,7 @@ public IModuleLogger GetChild(string name, Type targetType) var config = _config.ChildConfigs.FirstOrDefault(item => item.LoggerName == childName); if (config == null) { - config = new ModuleLoggerConfig { LoggerName = childName, ActiveLevel = Parent?.ActiveLevel ?? ActiveLevel.Info }; + config = new ModuleLoggerConfig { LoggerName = childName, ActiveLevel = Parent?.ActiveLevel ?? LogLevel.Info }; _config.ChildConfigs.Add(config); } From e1a3b33a3a69196f0a7bfc0d35f4c75ed4c97bd7 Mon Sep 17 00:00:00 2001 From: Marcel Vielhaus Date: Thu, 19 May 2022 13:15:06 +0200 Subject: [PATCH 04/12] Add initialization of EntryValidation to cover maximum range for number validations --- .../Serialization/EntryConvert/EntryValidation.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Moryx/Serialization/EntryConvert/EntryValidation.cs b/src/Moryx/Serialization/EntryConvert/EntryValidation.cs index a32b9634c..2cbbd89ac 100644 --- a/src/Moryx/Serialization/EntryConvert/EntryValidation.cs +++ b/src/Moryx/Serialization/EntryConvert/EntryValidation.cs @@ -36,6 +36,16 @@ public class EntryValidation : ICloneable [DataMember] public bool IsRequired { get; set; } + /// + /// Creates a new instance initializing + /// and validation to the largest possible range. + /// + public EntryValidation() + { + Minimum = double.MinValue; + Maximum = double.MaxValue; + } + /// public object Clone() { From bbf60de37b5a4401f055d647774e3927f7e3c212 Mon Sep 17 00:00:00 2001 From: Marisa Goergen Date: Mon, 23 May 2022 13:44:48 +0200 Subject: [PATCH 05/12] Add ICollectionStrategy for ILists which are Arrays --- .../CollectionStrategy/ArrayIListStrategy.cs | 84 +++++++++++++++++++ .../EntryConvert/EntryConvert.cs | 18 +++- .../Serialization/DummyClassIList.cs | 43 ++++++++++ .../EntryToModelConverterTests.cs | 22 +++++ 4 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 src/Moryx/Serialization/EntryConvert/CollectionStrategy/ArrayIListStrategy.cs create mode 100644 src/Tests/Moryx.Tests/Serialization/DummyClassIList.cs diff --git a/src/Moryx/Serialization/EntryConvert/CollectionStrategy/ArrayIListStrategy.cs b/src/Moryx/Serialization/EntryConvert/CollectionStrategy/ArrayIListStrategy.cs new file mode 100644 index 000000000..7b0e55353 --- /dev/null +++ b/src/Moryx/Serialization/EntryConvert/CollectionStrategy/ArrayIListStrategy.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Reflection; + +namespace Moryx.Serialization +{ + public class ArrayIListStrategy : ICollectionStrategy + { + private readonly IList _list; + private readonly IList _toDelete = new List(); + private readonly ICustomSerialization _customSerialization; + private readonly ICustomAttributeProvider _property; + private readonly object _instance; + + public ArrayIListStrategy(IList list, ICustomSerialization customSerialization, + ICustomAttributeProvider attributeProvider, object instance) + { + _list = list; + _customSerialization = customSerialization; + _property = attributeProvider; + _instance = instance; + } + + public IEnumerable Serialize() + { + var entries = new List(); + for (int index = 0; index < _list.Count; index++) + { + var entry = CollectionStrategyTools.CreateSub(_list[index], index, _customSerialization); + entries.Add(entry); + } + return entries; + } + + public IEnumerable Keys() + { + return CollectionStrategyTools.GenerateKeys(_list.Count); + } + + public object ElementAt(string key) + { + return _list[int.Parse(key)]; + } + + public void Added(Entry entry, object addedValue) + { + _list.Add(addedValue); + } + + public void Updated(Entry entry, object updatedValue) + { + _list[int.Parse(entry.Identifier)] = updatedValue; + } + + public void Removed(string key) + { + _toDelete.Add(_list[int.Parse(key)]); + } + + public void Flush() + { + + if(_property is PropertyInfo propertyInfo) + { + + var type = propertyInfo.PropertyType; + var elementType = type.GenericTypeArguments[0]; + var list = Array.CreateInstance(elementType,_list.Count - _toDelete.Count); + var index = 0; + foreach (var e in _list) + { + if (!_toDelete.Contains(e)) + { + list.SetValue(e, index++); + } + + } + + propertyInfo.SetValue(_instance, list); + } + } + } +} diff --git a/src/Moryx/Serialization/EntryConvert/EntryConvert.cs b/src/Moryx/Serialization/EntryConvert/EntryConvert.cs index 6bf59b349..19a2c23bd 100644 --- a/src/Moryx/Serialization/EntryConvert/EntryConvert.cs +++ b/src/Moryx/Serialization/EntryConvert/EntryConvert.cs @@ -570,6 +570,7 @@ public static object UpdateInstance(object instance, Entry encoded, ICustomSeria { var property = mapped.Property; var propertyType = mapped.Property.PropertyType; + var doNotUpdateProperty = false; // Do not operate on faulty properties or read-only properties // For security reasons read the flag from the property again @@ -591,8 +592,10 @@ public static object UpdateInstance(object instance, Entry encoded, ICustomSeria else if (IsCollection(propertyType) && mapped.Entry != null) { // Pick collection strategy - var strategy = CreateStrategy(value, currentValue, propertyType, customSerialization); + var strategy = CreateStrategy(value, currentValue, propertyType, customSerialization, mapped.Property, instance); UpdateCollection(currentValue, mapped.Property.PropertyType, mapped.Property, mapped.Entry, strategy, customSerialization); + if (strategy is ArrayIListStrategy) + doNotUpdateProperty = true; } // Update class else if (propertyType.IsClass) @@ -605,7 +608,7 @@ public static object UpdateInstance(object instance, Entry encoded, ICustomSeria } // Write new value to property - if (property.CanWrite) + if (property.CanWrite && !doNotUpdateProperty) { mapped.Property.SetValue(instance, value); } @@ -693,7 +696,9 @@ private static object CreatePrimitiveOrEnum(Type propertyType, EntryValue entryV /// /// Create strategy for collection /// - private static ICollectionStrategy CreateStrategy(object collection, object currentCollection, Type collectionType, ICustomSerialization serialization) + private static ICollectionStrategy CreateStrategy(object collection, object currentCollection, + Type collectionType, ICustomSerialization serialization, ICustomAttributeProvider attributeProvider = null, + object instance = null) { ICollectionStrategy strategy; if (collectionType.IsArray) @@ -706,7 +711,12 @@ private static ICollectionStrategy CreateStrategy(object collection, object curr } else if (collection is IList) { - strategy = new ListStrategy((IList)collection, serialization); + if(currentCollection is Array) + { + strategy = new ArrayIListStrategy( + (IList)collection, serialization, attributeProvider, instance); + }else + strategy = new ListStrategy((IList)collection, serialization); } else { diff --git a/src/Tests/Moryx.Tests/Serialization/DummyClassIList.cs b/src/Tests/Moryx.Tests/Serialization/DummyClassIList.cs new file mode 100644 index 000000000..1e07fe0df --- /dev/null +++ b/src/Tests/Moryx.Tests/Serialization/DummyClassIList.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Moryx.Tests +{ + public class DummyClassIList + { + public int Number { get; set; } + + public string Name { get; set; } + + public int ReadOnly => 10; + + public SubClass SingleClass { get; set; } + + public SubClass[] SubArray { get; set; } + + public List SubList { get; set; } + + public IList SubIList { get; set; } + + public IEnumerable SubEnumerable { get; set; } + + public IDictionary SubDictionary { get; set; } + + public DummyEnum[] EnumArray { get; set; } + + public List EnumList { get; set; } + + public IEnumerable EnumEnumerable { get; set; } + + public bool[] BoolArray { get; set; } + + public List BoolList { get; set; } + + public IEnumerable BoolEnumerable { get; set; } + + public SubClass SingleClassNonLocalized { get; set; } + } +} diff --git a/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs b/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs index a0cf7b3ea..7aef6d2cc 100644 --- a/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs +++ b/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs @@ -96,6 +96,28 @@ public void AddItemToEntryCollection() Assert.AreEqual(typeof(EntryModelSubClassDummyClient).Name, serverEntry.SubEntries[4].SubEntries[2].Value.Current); } + [Test] + public void RemoveItemFromCollectionWhichIsArray() + { + var dummy = new DummyClassIList(); + + dummy.Number = 10; + dummy.Name = "Thomas"; + dummy.SingleClass = null; + dummy.SubArray = new[] { new SubClass { Foo = (float)1.2, Enum = DummyEnum.ValueB } }; + dummy.SubList = new List { new SubClass { Foo = (float)3.4, Enum = DummyEnum.ValueA } }; + dummy.SubEnumerable = new List { new SubClass { Foo = (float)3.4, Enum = DummyEnum.ValueA } }; + dummy.SubDictionary = new Dictionary(); + dummy.SubIList = new int[] {1,2,3,7}; + + var entry = EntryConvert.EncodeObject(dummy); + var x = entry.SubEntries.FirstOrDefault(e => e.Identifier == "SubIList"); + x.SubEntries.RemoveAt(0); + EntryConvert.UpdateInstance(dummy, entry); + + Assert.AreEqual(dummy.SubIList.Count, 3); + } + [Test] public void RemoveItemFromEntryCollection() { From 0fbb8be22e7ce4e05add28053354c2360ae97af3 Mon Sep 17 00:00:00 2001 From: Marisa Goergen Date: Wed, 25 May 2022 09:48:30 +0200 Subject: [PATCH 06/12] Fix elements can be added to IList properties even if they are arrays --- .../CollectionStrategy/ArrayIListStrategy.cs | 13 ++++++----- .../EntryToModelConverterTests.cs | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Moryx/Serialization/EntryConvert/CollectionStrategy/ArrayIListStrategy.cs b/src/Moryx/Serialization/EntryConvert/CollectionStrategy/ArrayIListStrategy.cs index 7b0e55353..ab8385a66 100644 --- a/src/Moryx/Serialization/EntryConvert/CollectionStrategy/ArrayIListStrategy.cs +++ b/src/Moryx/Serialization/EntryConvert/CollectionStrategy/ArrayIListStrategy.cs @@ -12,6 +12,7 @@ public class ArrayIListStrategy : ICollectionStrategy private readonly ICustomSerialization _customSerialization; private readonly ICustomAttributeProvider _property; private readonly object _instance; + private readonly IList _toAdd = new List(); public ArrayIListStrategy(IList list, ICustomSerialization customSerialization, ICustomAttributeProvider attributeProvider, object instance) @@ -45,7 +46,7 @@ public object ElementAt(string key) public void Added(Entry entry, object addedValue) { - _list.Add(addedValue); + _toAdd.Add(addedValue); } public void Updated(Entry entry, object updatedValue) @@ -63,18 +64,20 @@ public void Flush() if(_property is PropertyInfo propertyInfo) { - var type = propertyInfo.PropertyType; var elementType = type.GenericTypeArguments[0]; - var list = Array.CreateInstance(elementType,_list.Count - _toDelete.Count); + var list = Array.CreateInstance(elementType,_list.Count - _toDelete.Count + _toAdd.Count); var index = 0; foreach (var e in _list) { if (!_toDelete.Contains(e)) { list.SetValue(e, index++); - } - + } + } + foreach(var e in _toAdd) + { + list.SetValue(e, index++); } propertyInfo.SetValue(_instance, list); diff --git a/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs b/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs index 7aef6d2cc..0b1405946 100644 --- a/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs +++ b/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs @@ -118,6 +118,28 @@ public void RemoveItemFromCollectionWhichIsArray() Assert.AreEqual(dummy.SubIList.Count, 3); } + [Test] + public void AddItemToCollectionWhichIsArray() + { + var dummy = new DummyClassIList(); + + dummy.Number = 10; + dummy.Name = "Thomas"; + dummy.SingleClass = null; + dummy.SubArray = new[] { new SubClass { Foo = (float)1.2, Enum = DummyEnum.ValueB } }; + dummy.SubList = new List { new SubClass { Foo = (float)3.4, Enum = DummyEnum.ValueA } }; + dummy.SubEnumerable = new List { new SubClass { Foo = (float)3.4, Enum = DummyEnum.ValueA } }; + dummy.SubDictionary = new Dictionary(); + dummy.SubIList = new int[] { 1, 2, 3, 7 }; + + var entry = EntryConvert.EncodeObject(dummy); + var x = entry.SubEntries.FirstOrDefault(e => e.Identifier == "SubIList"); + x.SubEntries.Add(x.Prototypes.First()); + EntryConvert.UpdateInstance(dummy, entry); + + Assert.AreEqual(dummy.SubIList.Count, 5); + } + [Test] public void RemoveItemFromEntryCollection() { From 4554b67e9a3e84f3c5bd97bf6613155083f740a5 Mon Sep 17 00:00:00 2001 From: Marisa Goergen Date: Wed, 25 May 2022 14:44:10 +0200 Subject: [PATCH 07/12] Add comments to tests --- .../Serialization/EntryToModelConverterTests.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs b/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs index 0b1405946..e66850d84 100644 --- a/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs +++ b/src/Tests/Moryx.Tests/Serialization/EntryToModelConverterTests.cs @@ -99,8 +99,8 @@ public void AddItemToEntryCollection() [Test] public void RemoveItemFromCollectionWhichIsArray() { + //Arrange var dummy = new DummyClassIList(); - dummy.Number = 10; dummy.Name = "Thomas"; dummy.SingleClass = null; @@ -113,16 +113,19 @@ public void RemoveItemFromCollectionWhichIsArray() var entry = EntryConvert.EncodeObject(dummy); var x = entry.SubEntries.FirstOrDefault(e => e.Identifier == "SubIList"); x.SubEntries.RemoveAt(0); + + //Act EntryConvert.UpdateInstance(dummy, entry); + //Assert Assert.AreEqual(dummy.SubIList.Count, 3); } [Test] public void AddItemToCollectionWhichIsArray() { + //Arrange var dummy = new DummyClassIList(); - dummy.Number = 10; dummy.Name = "Thomas"; dummy.SingleClass = null; @@ -135,8 +138,11 @@ public void AddItemToCollectionWhichIsArray() var entry = EntryConvert.EncodeObject(dummy); var x = entry.SubEntries.FirstOrDefault(e => e.Identifier == "SubIList"); x.SubEntries.Add(x.Prototypes.First()); + + //Act EntryConvert.UpdateInstance(dummy, entry); + //Assert Assert.AreEqual(dummy.SubIList.Count, 5); } From d3e648e0e00ade7562ac18a2e040543c7f58828c Mon Sep 17 00:00:00 2001 From: Wjatscheslaw Sujev Date: Tue, 7 Jun 2022 11:16:27 +0200 Subject: [PATCH 08/12] Extends IVersionServiceManager to provide the proxy configuration and use it for the base connectors --- .../Connector/WebHttpServiceConnectorBase.cs | 21 +++++++++++++++++-- .../Endpoints/Connector/IProxyConfigAccess.cs | 16 ++++++++++++++ .../Connector/VersionServiceManager.cs | 11 +++++++--- .../Connector/WebServiceConnectorBase.cs | 21 +++++++++++++++++-- 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 src/Moryx/Communication/Endpoints/Connector/IProxyConfigAccess.cs diff --git a/src/Moryx.Tools.Wcf/Client/Connector/WebHttpServiceConnectorBase.cs b/src/Moryx.Tools.Wcf/Client/Connector/WebHttpServiceConnectorBase.cs index 4c0f21410..cb6006b0a 100644 --- a/src/Moryx.Tools.Wcf/Client/Connector/WebHttpServiceConnectorBase.cs +++ b/src/Moryx.Tools.Wcf/Client/Connector/WebHttpServiceConnectorBase.cs @@ -118,8 +118,25 @@ private async Task EvaluateResponse(Task res // Compare version if (VersionCompare.ClientMatch(serverVersion, clientVersion)) { - // Create new base address client - HttpClient = new HttpClient { BaseAddress = new Uri(endpoint.Address) }; + // Create HttpClient + var proxyConfig = (_endpointService as IProxyConfigAccess)?.ProxyConfig; + if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy) + { + var proxy = new WebProxy + { + Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"), + BypassProxyOnLocal = false, + UseDefaultCredentials = true + }; + + HttpClient = new HttpClient(new HttpClientHandler { Proxy = proxy }); + } + else + { + HttpClient = new HttpClient(); + } + + HttpClient.BaseAddress = new Uri(endpoint.Address); if (!string.IsNullOrEmpty(_myCulture.IetfLanguageTag)) HttpClient.DefaultRequestHeaders.AcceptLanguage diff --git a/src/Moryx/Communication/Endpoints/Connector/IProxyConfigAccess.cs b/src/Moryx/Communication/Endpoints/Connector/IProxyConfigAccess.cs new file mode 100644 index 000000000..05178d7dc --- /dev/null +++ b/src/Moryx/Communication/Endpoints/Connector/IProxyConfigAccess.cs @@ -0,0 +1,16 @@ +// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG +// Licensed under the Apache License, Version 2.0 + +namespace Moryx.Communication.Endpoints +{ + /// + /// Enables the access to the used proxy configuration + /// + public interface IProxyConfigAccess + { + /// + /// Used proxy configuration + /// + IProxyConfig ProxyConfig { get; } + } +} diff --git a/src/Moryx/Communication/Endpoints/Connector/VersionServiceManager.cs b/src/Moryx/Communication/Endpoints/Connector/VersionServiceManager.cs index 4c5cc4186..76999ae57 100644 --- a/src/Moryx/Communication/Endpoints/Connector/VersionServiceManager.cs +++ b/src/Moryx/Communication/Endpoints/Connector/VersionServiceManager.cs @@ -13,7 +13,7 @@ namespace Moryx.Communication.Endpoints /// /// Service manager base class for provide active endpoints of the current application runtime /// - public abstract class VersionServiceManager : IVersionServiceManager + public abstract class VersionServiceManager : IVersionServiceManager, IProxyConfigAccess where TEndpoint : Endpoint { private const string ServiceName = "endpoints"; @@ -22,18 +22,23 @@ public abstract class VersionServiceManager : IVersionServiceManager /// Underlying http client for the requests /// protected HttpClient Client { get; set; } + + /// + public IProxyConfig ProxyConfig { get; private set; } /// /// Creates a new instance of the /// protected VersionServiceManager(IProxyConfig proxyConfig, string host, int port) { + ProxyConfig = proxyConfig; + // Create HttpClient - if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy) + if (ProxyConfig?.EnableProxy == true && !ProxyConfig.UseDefaultWebProxy) { var proxy = new WebProxy { - Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"), + Address = new Uri($"http://{ProxyConfig.Address}:{ProxyConfig.Port}"), BypassProxyOnLocal = false, UseDefaultCredentials = true }; diff --git a/src/Moryx/Communication/Endpoints/Connector/WebServiceConnectorBase.cs b/src/Moryx/Communication/Endpoints/Connector/WebServiceConnectorBase.cs index bde092ff3..ee7288191 100644 --- a/src/Moryx/Communication/Endpoints/Connector/WebServiceConnectorBase.cs +++ b/src/Moryx/Communication/Endpoints/Connector/WebServiceConnectorBase.cs @@ -106,8 +106,25 @@ private async Task EvaluateResponse(Task resp) // Compare version if (VersionCompare.ClientMatch(serverVersion, clientVersion)) { - // Create new base address client - HttpClient = new HttpClient { BaseAddress = new Uri(endpoint.Address) }; + // Create HttpClient + var proxyConfig = (_endpointService as IProxyConfigAccess)?.ProxyConfig; + if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy) + { + var proxy = new WebProxy + { + Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"), + BypassProxyOnLocal = false, + UseDefaultCredentials = true + }; + + HttpClient = new HttpClient(new HttpClientHandler { Proxy = proxy }); + } + else + { + HttpClient = new HttpClient(); + } + + HttpClient.BaseAddress = new Uri(endpoint.Address); if (!string.IsNullOrEmpty(_myCulture.IetfLanguageTag)) HttpClient.DefaultRequestHeaders.AcceptLanguage From 8d47c2952198ef4295ab724964590d3f1f67df15 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Tue, 7 Jun 2022 14:48:05 +0200 Subject: [PATCH 09/12] Update VERSION --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 4d9d11cf5..1545d9665 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.4.2 +3.5.0 From 15d61d549b5e774704d8216bb39a8a674b05dada Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Fri, 10 Jun 2022 08:23:07 +0200 Subject: [PATCH 10/12] Move WebModule attribute for new web UIs --- .../MoryxServiceCollectionExtensions.cs | 5 ++- .../WebModuleAttribute.cs | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/Moryx.Asp.Extensions/WebModuleAttribute.cs diff --git a/src/Moryx.Asp.Extensions/MoryxServiceCollectionExtensions.cs b/src/Moryx.Asp.Extensions/MoryxServiceCollectionExtensions.cs index b39b65924..9a8c90693 100644 --- a/src/Moryx.Asp.Extensions/MoryxServiceCollectionExtensions.cs +++ b/src/Moryx.Asp.Extensions/MoryxServiceCollectionExtensions.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) 2022, Phoenix Contact GmbH & Co. KG +// Licensed under the Apache License, Version 2.0 + +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; diff --git a/src/Moryx.Asp.Extensions/WebModuleAttribute.cs b/src/Moryx.Asp.Extensions/WebModuleAttribute.cs new file mode 100644 index 000000000..f6ee7f402 --- /dev/null +++ b/src/Moryx.Asp.Extensions/WebModuleAttribute.cs @@ -0,0 +1,38 @@ +// Copyright (c) 2022, Phoenix Contact GmbH & Co. KG +// Licensed under the Apache License, Version 2.0 + +namespace Moryx.Asp.Integration +{ + /// + /// Decorator for web modules + /// + public class WebModuleAttribute + { + /// + /// Unique route of this module + /// + public string Route { get; } + + /// + /// Recognizable icon of this module + /// + public string Icon { get; } + + /// + /// Export page as web module under given route + /// + public WebModuleAttribute(string route) : this(route, "favorite") + { + + } + + /// + /// Export page as web module under given route and icon + /// + public WebModuleAttribute(string route, string icon) + { + Route = route; + Icon = icon; + } + } +} \ No newline at end of file From 1084ff66ebbd586ee76fb6bbdd886f96c672ee71 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Fri, 10 Jun 2022 10:36:59 +0200 Subject: [PATCH 11/12] Add missing attribute base class --- src/Moryx.Asp.Extensions/WebModuleAttribute.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Moryx.Asp.Extensions/WebModuleAttribute.cs b/src/Moryx.Asp.Extensions/WebModuleAttribute.cs index f6ee7f402..890b3506d 100644 --- a/src/Moryx.Asp.Extensions/WebModuleAttribute.cs +++ b/src/Moryx.Asp.Extensions/WebModuleAttribute.cs @@ -1,12 +1,14 @@ // Copyright (c) 2022, Phoenix Contact GmbH & Co. KG // Licensed under the Apache License, Version 2.0 +using System; + namespace Moryx.Asp.Integration { /// /// Decorator for web modules /// - public class WebModuleAttribute + public class WebModuleAttribute : Attribute { /// /// Unique route of this module From d63cdc8cde4f61151d0e00da864935417d10c9fc Mon Sep 17 00:00:00 2001 From: Wjatscheslaw Sujev Date: Fri, 10 Jun 2022 14:01:14 +0200 Subject: [PATCH 12/12] Created a static class to create HttpClient with proxy settings for reuse cases --- .../Connector/WebHttpServiceConnectorBase.cs | 18 +-------- .../Endpoints/Connector/HttpClientBuilder.cs | 39 +++++++++++++++++++ .../Connector/VersionServiceManager.cs | 21 +--------- .../Connector/WebServiceConnectorBase.cs | 18 +-------- 4 files changed, 42 insertions(+), 54 deletions(-) create mode 100644 src/Moryx/Communication/Endpoints/Connector/HttpClientBuilder.cs diff --git a/src/Moryx.Tools.Wcf/Client/Connector/WebHttpServiceConnectorBase.cs b/src/Moryx.Tools.Wcf/Client/Connector/WebHttpServiceConnectorBase.cs index cb6006b0a..f7cd4718e 100644 --- a/src/Moryx.Tools.Wcf/Client/Connector/WebHttpServiceConnectorBase.cs +++ b/src/Moryx.Tools.Wcf/Client/Connector/WebHttpServiceConnectorBase.cs @@ -120,23 +120,7 @@ private async Task EvaluateResponse(Task res { // Create HttpClient var proxyConfig = (_endpointService as IProxyConfigAccess)?.ProxyConfig; - if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy) - { - var proxy = new WebProxy - { - Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"), - BypassProxyOnLocal = false, - UseDefaultCredentials = true - }; - - HttpClient = new HttpClient(new HttpClientHandler { Proxy = proxy }); - } - else - { - HttpClient = new HttpClient(); - } - - HttpClient.BaseAddress = new Uri(endpoint.Address); + HttpClient = HttpClientBuilder.GetClient(endpoint.Address, proxyConfig); if (!string.IsNullOrEmpty(_myCulture.IetfLanguageTag)) HttpClient.DefaultRequestHeaders.AcceptLanguage diff --git a/src/Moryx/Communication/Endpoints/Connector/HttpClientBuilder.cs b/src/Moryx/Communication/Endpoints/Connector/HttpClientBuilder.cs new file mode 100644 index 000000000..141131311 --- /dev/null +++ b/src/Moryx/Communication/Endpoints/Connector/HttpClientBuilder.cs @@ -0,0 +1,39 @@ +// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG +// Licensed under the Apache License, Version 2.0 + +using System; +using System.Net; +using System.Net.Http; + +namespace Moryx.Communication.Endpoints +{ + /// + /// Helper class to get a configured HttpClient + /// + public static class HttpClientBuilder + { + /// + /// Creates a HttpClient based on the proxy config and base address + /// + public static HttpClient GetClient(string baseAddress, IProxyConfig proxyConfig) + { + var httpClient = new HttpClient(); + + if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy) + { + var proxy = new WebProxy + { + Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"), + BypassProxyOnLocal = false, + UseDefaultCredentials = true + }; + + httpClient = new HttpClient(new HttpClientHandler { Proxy = proxy }); + } + + httpClient.BaseAddress = new Uri(baseAddress); + + return httpClient; + } + } +} \ No newline at end of file diff --git a/src/Moryx/Communication/Endpoints/Connector/VersionServiceManager.cs b/src/Moryx/Communication/Endpoints/Connector/VersionServiceManager.cs index 76999ae57..1e2ee0598 100644 --- a/src/Moryx/Communication/Endpoints/Connector/VersionServiceManager.cs +++ b/src/Moryx/Communication/Endpoints/Connector/VersionServiceManager.cs @@ -3,7 +3,6 @@ using System; using System.Linq; -using System.Net; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json; @@ -32,25 +31,7 @@ public abstract class VersionServiceManager : IVersionServiceManager, protected VersionServiceManager(IProxyConfig proxyConfig, string host, int port) { ProxyConfig = proxyConfig; - - // Create HttpClient - if (ProxyConfig?.EnableProxy == true && !ProxyConfig.UseDefaultWebProxy) - { - var proxy = new WebProxy - { - Address = new Uri($"http://{ProxyConfig.Address}:{ProxyConfig.Port}"), - BypassProxyOnLocal = false, - UseDefaultCredentials = true - }; - - Client = new HttpClient(new HttpClientHandler { Proxy = proxy }); - } - else - { - Client = new HttpClient(); - } - - Client.BaseAddress = new Uri($"http://{host}:{port}/{ServiceName}/"); + Client = HttpClientBuilder.GetClient($"http://{host}:{port}/{ServiceName}/", ProxyConfig); } /// diff --git a/src/Moryx/Communication/Endpoints/Connector/WebServiceConnectorBase.cs b/src/Moryx/Communication/Endpoints/Connector/WebServiceConnectorBase.cs index ee7288191..448d6489a 100644 --- a/src/Moryx/Communication/Endpoints/Connector/WebServiceConnectorBase.cs +++ b/src/Moryx/Communication/Endpoints/Connector/WebServiceConnectorBase.cs @@ -108,23 +108,7 @@ private async Task EvaluateResponse(Task resp) { // Create HttpClient var proxyConfig = (_endpointService as IProxyConfigAccess)?.ProxyConfig; - if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy) - { - var proxy = new WebProxy - { - Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"), - BypassProxyOnLocal = false, - UseDefaultCredentials = true - }; - - HttpClient = new HttpClient(new HttpClientHandler { Proxy = proxy }); - } - else - { - HttpClient = new HttpClient(); - } - - HttpClient.BaseAddress = new Uri(endpoint.Address); + HttpClient = HttpClientBuilder.GetClient(endpoint.Address, proxyConfig); if (!string.IsNullOrEmpty(_myCulture.IetfLanguageTag)) HttpClient.DefaultRequestHeaders.AcceptLanguage