1
0

Removed DNS dependency, added ReflectionExtensions.

This commit is contained in:
2022-09-18 22:54:32 +02:00
parent ba8dd516e6
commit 36ddaf02ad
9 changed files with 135 additions and 48 deletions

View File

@@ -6,7 +6,7 @@
<AssemblyName>AMWD.Common.AspNetCore</AssemblyName> <AssemblyName>AMWD.Common.AspNetCore</AssemblyName>
<RootNamespace>AMWD.Common.AspNetCore</RootNamespace> <RootNamespace>AMWD.Common.AspNetCore</RootNamespace>
<NrtRevisionFormat>{semvertag:main}{!:~dirty}</NrtRevisionFormat> <NrtRevisionFormat>{semvertag:main}{!:-dirty}</NrtRevisionFormat>
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath> <AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory> <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
@@ -33,17 +33,21 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(CI)' == 'true'"> <PropertyGroup Condition="'$(GITLAB_CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(GITLAB_CI)' == 'true'">
<SourceLinkGitLabHost Include="git.am-wd.de" Version="$(CI_SERVER_VERSION)" />
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="../icon.png" Pack="true" PackagePath="/" /> <None Include="../icon.png" Pack="true" PackagePath="/" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" /> <FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Unclassified.DeepConvert" Version="1.4.0" /> <PackageReference Include="Unclassified.DeepConvert" Version="1.4.0" />
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3"> <PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3">

View File

@@ -6,7 +6,7 @@
<AssemblyName>AMWD.Common.EntityFrameworkCore</AssemblyName> <AssemblyName>AMWD.Common.EntityFrameworkCore</AssemblyName>
<RootNamespace>AMWD.Common.EntityFrameworkCore</RootNamespace> <RootNamespace>AMWD.Common.EntityFrameworkCore</RootNamespace>
<NrtRevisionFormat>{semvertag:main}{!:~dirty}</NrtRevisionFormat> <NrtRevisionFormat>{semvertag:main}{!:-dirty}</NrtRevisionFormat>
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath> <AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory> <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
@@ -33,29 +33,33 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(CI)' == 'true'"> <PropertyGroup Condition="'$(GITLAB_CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(GITLAB_CI)' == 'true'">
<SourceLinkGitLabHost Include="git.am-wd.de" Version="$(CI_SERVER_VERSION)" />
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="../icon.png" Pack="true" PackagePath="/" /> <None Include="../icon.png" Pack="true" PackagePath="/" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'"> <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.27" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.27" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.29" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.27" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.29" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'"> <ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.7" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.9" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3"> <PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -6,7 +6,7 @@
<AssemblyName>AMWD.Common.Moq</AssemblyName> <AssemblyName>AMWD.Common.Moq</AssemblyName>
<RootNamespace>AMWD.Common.Moq</RootNamespace> <RootNamespace>AMWD.Common.Moq</RootNamespace>
<NrtRevisionFormat>{semvertag:main}{!:~dirty}</NrtRevisionFormat> <NrtRevisionFormat>{semvertag:main}{!:-dirty}</NrtRevisionFormat>
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath> <AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory> <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
@@ -33,17 +33,21 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(CI)' == 'true'"> <PropertyGroup Condition="'$(GITLAB_CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(GITLAB_CI)' == 'true'">
<SourceLinkGitLabHost Include="git.am-wd.de" Version="$(CI_SERVER_VERSION)" />
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="../icon.png" Pack="true" PackagePath="/" /> <None Include="../icon.png" Pack="true" PackagePath="/" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="all" /> <PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3"> <PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -6,7 +6,7 @@
<AssemblyName>AMWD.Common</AssemblyName> <AssemblyName>AMWD.Common</AssemblyName>
<RootNamespace>AMWD.Common</RootNamespace> <RootNamespace>AMWD.Common</RootNamespace>
<NrtRevisionFormat>{semvertag:main}{!:~dirty}</NrtRevisionFormat> <NrtRevisionFormat>{semvertag:main}{!:-dirty}</NrtRevisionFormat>
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath> <AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory> <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
@@ -33,19 +33,22 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(CI)' == 'true'"> <PropertyGroup Condition="'$(GITLAB_CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(GITLAB_CI)' == 'true'">
<SourceLinkGitLabHost Include="git.am-wd.de" Version="$(CI_SERVER_VERSION)" />
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="../icon.png" Pack="true" PackagePath="/" /> <None Include="../icon.png" Pack="true" PackagePath="/" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Dns" Version="7.0.0" />
<PackageReference Include="MessagePack" Version="2.4.35" /> <PackageReference Include="MessagePack" Version="2.4.35" />
<PackageReference Include="Microsoft.AspNetCore.HttpOverrides" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.HttpOverrides" Version="2.2.0" />
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Unclassified.DeepConvert" Version="1.4.0" /> <PackageReference Include="Unclassified.DeepConvert" Version="1.4.0" />
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3"> <PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3">

View File

@@ -0,0 +1,42 @@
using System.Reflection;
using System.Threading.Tasks;
namespace AMWD.Common.Extensions
{
/// <summary>
/// Extension methods for <see cref="System.Reflection"/>.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public static class ReflectionExtensions
{
/// <summary>
/// Calls a method from it's reflection asynchronously without result.
/// </summary>
/// <param name="methodInfo">The <see cref="MethodInfo"/> to call on an object.</param>
/// <param name="obj">The reflected instance to call the method on.</param>
/// <param name="parameters">The parameters of the called method.</param>
/// <returns>An awaitable <see cref="Task"/>.</returns>
public static async Task CallAsync(this MethodInfo methodInfo, object obj, params object[] parameters)
{
var task = (Task)methodInfo.Invoke(obj, parameters);
await task.ConfigureAwait(false);
}
/// <summary>
/// Invokes a method from it's reflection asynchronously with a result.
/// </summary>
/// <typeparam name="TResult">The result type, that is expected (and casted to).</typeparam>
/// <param name="methodInfo">The <see cref="MethodInfo"/> to invoke on an object.</param>
/// <param name="obj">The reflected instance to invoke the method on.</param>
/// <param name="parameters">The parameters of the called method.</param>
/// <returns>An awaitable <see cref="Task"/> with result.</returns>
public static async Task<TResult> InvokeAsync<TResult>(this MethodInfo methodInfo, object obj, params object[] parameters)
{
var task = (Task)methodInfo.Invoke(obj, parameters);
await task.ConfigureAwait(false);
var resultPropertyInfo = task.GetType().GetProperty("Result");
return (TResult)resultPropertyInfo.GetValue(task);
}
}
}

View File

@@ -1,13 +1,14 @@
using System.Collections.Generic; using System.Collections;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Mail; using System.Net.Mail;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using DNS.Client; using AMWD.Common.Extensions;
using DNS.Protocol;
namespace System namespace System
{ {
@@ -150,47 +151,67 @@ namespace System
/// You can enhance the check by requesting the MX record of the domain. /// You can enhance the check by requesting the MX record of the domain.
/// </remarks> /// </remarks>
/// <param name="email">The email address as string to validate.</param> /// <param name="email">The email address as string to validate.</param>
/// <param name="checkRecordExists">A value indicating whether to resolve a MX record (Google DNS is used).</param> /// <param name="checkForDnsRecord">A value indicating whether to resolve a MX record (Google DNS is used).</param>
/// <returns><c>true</c> when the email address is valid, other wise <c>false</c>.</returns> /// <returns><c>true</c> when the email address is valid, other wise <c>false</c>.</returns>
public static bool IsValidEmailAddress(this string email, bool checkRecordExists = false) public static bool IsValidEmailAddress(this string email, bool checkForDnsRecord = false)
=> email.IsValidEmailAddress(checkRecordExists ? new[] { new IPEndPoint(IPAddress.Parse("8.8.8.8"), 53) } : null); => email.IsValidEmailAddress(checkForDnsRecord ? new[] { new IPEndPoint(IPAddress.Parse("8.8.8.8"), 53) } : null);
/// <summary> /// <summary>
/// Checks whether the given <see cref="string"/> is a valid <see cref="MailAddress"/>. /// Checks whether the given <see cref="string"/> is a valid <see cref="MailAddress"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The check is enhanced by a request for MX records on the defined <paramref name="nameservers"/>. /// The check is enhanced by a request for MX records on the defined <paramref name="nameservers"/>.
/// <br/>
/// The DNS resolution is only used when the DNS NuGet package is available.
/// See: https://www.nuget.org/packages/DNS/7.0.0
/// </remarks> /// </remarks>
/// <param name="email">The email address as string to validate.</param> /// <param name="emailAddress">The email address as string to validate.</param>
/// <param name="nameservers">A list of <see cref="IPEndPoint"/>s of nameservers.</param> /// <param name="nameservers">A list of <see cref="IPEndPoint"/>s of nameservers.</param>
/// <returns><c>true</c> when the email address is valid, other wise <c>false</c>.</returns> /// <returns><c>true</c> when the email address is valid, other wise <c>false</c>.</returns>
public static bool IsValidEmailAddress(this string email, IEnumerable<IPEndPoint> nameservers) public static bool IsValidEmailAddress(this string emailAddress, IEnumerable<IPEndPoint> nameservers)
{ {
try try
{ {
var mailAddress = new MailAddress(email); var mailAddress = new MailAddress(emailAddress);
bool isValid = mailAddress.Address == email; bool isValid = mailAddress.Address == emailAddress;
if (isValid && nameservers?.Any() == true) if (isValid && nameservers?.Any() == true)
{ {
var dnsClientType = Type.GetType("DNS.Client.DnsClient, DNS");
if (dnsClientType == null)
throw new DllNotFoundException("The DNS NuGet package is required: https://www.nuget.org/packages/DNS/7.0.0");
var recordTypeType = Type.GetType("DNS.Protocol.RecordType, DNS");
var resolveMethodInfo = dnsClientType.GetMethod("Resolve", new[] { typeof(string), recordTypeType, typeof(CancellationToken) });
bool exists = false; bool exists = false;
foreach (var nameserver in nameservers) foreach (var nameserver in nameservers)
{ {
var client = new DnsClient(nameserver); object dnsClient = Activator.CreateInstance(dnsClientType, new object[] { nameserver });
var waitTask = Task.Run(async () => await client.Resolve(mailAddress.Host, RecordType.MX));
var waitTask = Task.Run(async () => await resolveMethodInfo.InvokeAsync<object>(dnsClient, new object[] { mailAddress.Host, 15, CancellationToken.None })); // 15 = MX Record
waitTask.Wait(); waitTask.Wait();
var response = waitTask.Result; object response = waitTask.Result;
waitTask.Dispose(); waitTask.Dispose();
if (response.ResponseCode != ResponseCode.NoError) int responseCode = (int)response.GetType().GetProperty("ResponseCode").GetValue(response);
if (responseCode != 0)
continue; continue;
exists = response.AnswerRecords object list = response.GetType().GetProperty("AnswerRecords").GetValue(response);
.Where(r => r.Type == RecordType.MX) foreach (object item in (list as IEnumerable))
.Any(); {
int type = (int)item.GetType().GetProperty("Type").GetValue(item);
if (type == 15)
{
exists = true;
break;
}
}
break; if (exists)
break;
} }
isValid &= exists; isValid &= exists;

View File

@@ -5,13 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased](https://git.am-wd.de/AM.WD/common/compare/v1.9.0...main) - 0000-00-00 ## [Unreleased](https://git.am-wd.de/AM.WD/common/compare/v1.10.0...main) - 0000-00-00
_nothing changed yet_
## [v1.10.0](https://git.am-wd.de/AM.WD/common/compare/v1.9.0...v1.10.0) - 2022-09-18
### Fixed ### Fixed
- Over-/Underflow behaviour for IPAddress de-/increment - Over-/Underflow behaviour for IPAddress de-/increment
- Await behaviour for AsyncQueue (not required to marshal back to the original captured context) - Await behaviour for AsyncQueue (not required to marshal back to the original captured context)
### Removed
- Removing dependency from DNS package
It's now used via reflection when possible (otherwise exeption)
## [v1.9.0](https://git.am-wd.de/AM.WD/common/compare/v1.8.1...v1.9.0) - 2022-08-14 ## [v1.9.0](https://git.am-wd.de/AM.WD/common/compare/v1.8.1...v1.9.0) - 2022-08-14

View File

@@ -267,11 +267,11 @@ namespace UnitTests.Common.Extensions
string nullStr = null; string nullStr = null;
// act // act
bool validWithoutTag = validEmailWithoutTag.IsValidEmailAddress(checkRecordExists: false); bool validWithoutTag = validEmailWithoutTag.IsValidEmailAddress(checkForDnsRecord: false);
bool validWithTag = validEmailWithTag.IsValidEmailAddress(checkRecordExists: false); bool validWithTag = validEmailWithTag.IsValidEmailAddress(checkForDnsRecord: false);
bool invalidWithoutTag = !invalidEmailWithoutTag.IsValidEmailAddress(checkRecordExists: false); bool invalidWithoutTag = !invalidEmailWithoutTag.IsValidEmailAddress(checkForDnsRecord: false);
bool invalidWithTag = !invalidEmailWithTag.IsValidEmailAddress(checkRecordExists: false); bool invalidWithTag = !invalidEmailWithTag.IsValidEmailAddress(checkForDnsRecord: false);
bool nullTest = nullStr.IsValidEmailAddress(checkRecordExists: false); bool nullTest = nullStr.IsValidEmailAddress(checkForDnsRecord: false);
// assert // assert
Assert.IsTrue(validWithoutTag); Assert.IsTrue(validWithoutTag);
@@ -289,8 +289,8 @@ namespace UnitTests.Common.Extensions
string invalidEmail = "test@not.exists"; string invalidEmail = "test@not.exists";
// act // act
bool valid = validEmail.IsValidEmailAddress(checkRecordExists: true); bool valid = validEmail.IsValidEmailAddress(checkForDnsRecord: true);
bool invalid = !invalidEmail.IsValidEmailAddress(checkRecordExists: true); bool invalid = !invalidEmail.IsValidEmailAddress(checkForDnsRecord: true);
// assert // assert
Assert.IsTrue(valid); Assert.IsTrue(valid);

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
@@ -11,8 +11,9 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" /> <PackageReference Include="DNS" Version="7.0.0" />
<PackageReference Include="Moq" Version="4.18.1" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" /> <PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" /> <PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="ReflectionMagic" Version="4.1.0" /> <PackageReference Include="ReflectionMagic" Version="4.1.0" />