Skip to content

Build ServiceDiscovery library and tests against .NET Framework #10470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
<PackageVersion Include="Microsoft.Extensions.Http" Version="$(MicrosoftExtensionsHttpLTSVersion)" />
<PackageVersion Include="System.Formats.Asn1" Version="$(SystemFormatsAsn1LTSVersion)" />
<PackageVersion Include="System.Text.Json" Version="$(SystemTextJsonLTSVersion)" />
<PackageVersion Include="System.Threading.Channels" Version="$(SystemThreadingChannelsLTSVersion)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<!-- EF -->
Expand Down Expand Up @@ -227,5 +228,12 @@
<PackageVersion Update="Microsoft.Extensions.Http" Version="$(MicrosoftExtensionsHttpVersion)" />
<PackageVersion Update="System.Formats.Asn1" Version="$(SystemFormatsAsn1Version)" />
<PackageVersion Update="System.Text.Json" Version="$(SystemTextJsonVersion)" />
<PackageVersion Update="System.Threading.Channels" Version="$(SystemThreadingChannelsVersion)" />
</ItemGroup>

<!-- These are 9.x versions which are STS so should ONLY be referenced by netfx projects -->
<ItemGroup Condition="'$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'net472'">
<PackageVersion Include="Microsoft.Bcl.Memory" Version="$(MicrosoftBclMemoryVersion)" />
<PackageVersion Include="Microsoft.Bcl.TimeProvider" Version="$(MicrosoftBclTimeProviderVersion)" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>3c298d9f00936d651cc47d221762474e25277672</Sha>
</Dependency>
<Dependency Name="System.Threading.Channels" Version="9.0.7">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>3c298d9f00936d651cc47d221762474e25277672</Sha>
</Dependency>
<Dependency Name="Microsoft.Bcl.Memory" Version="9.0.7">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>3c298d9f00936d651cc47d221762474e25277672</Sha>
</Dependency>
<Dependency Name="Microsoft.Bcl.TimeProvider" Version="9.0.7">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>3c298d9f00936d651cc47d221762474e25277672</Sha>
</Dependency>
</ProductDependencies>
<ToolsetDependencies>
<Dependency Name="Microsoft.DotNet.Arcade.Sdk" Version="10.0.0-beta.25351.1">
Expand Down
5 changes: 5 additions & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<MicrosoftExtensionsHttpResilienceVersion>9.7.0</MicrosoftExtensionsHttpResilienceVersion>
<MicrosoftExtensionsDiagnosticsTestingVersion>9.7.0</MicrosoftExtensionsDiagnosticsTestingVersion>
<MicrosoftExtensionsTimeProviderTestingVersion>9.7.0</MicrosoftExtensionsTimeProviderTestingVersion>
<!-- dotnet/runtime BCL compat packages for netfx -->
<MicrosoftBclMemoryVersion>9.0.7</MicrosoftBclMemoryVersion>
<MicrosoftBclTimeProviderVersion>9.0.7</MicrosoftBclTimeProviderVersion>
<!-- for templates -->
<MicrosoftAspNetCorePackageVersionForNet9>9.0.6</MicrosoftAspNetCorePackageVersionForNet9>
<MicrosoftAspNetCorePackageVersionForNet10>10.0.0-preview.5.25277.114</MicrosoftAspNetCorePackageVersionForNet10>
Expand Down Expand Up @@ -86,6 +89,7 @@
<MicrosoftExtensionsHttpVersion>9.0.7</MicrosoftExtensionsHttpVersion>
<SystemFormatsAsn1Version>9.0.7</SystemFormatsAsn1Version>
<SystemTextJsonVersion>9.0.7</SystemTextJsonVersion>
<SystemThreadingChannelsVersion>9.0.7</SystemThreadingChannelsVersion>
<!-- OpenTelemetry (OTel) -->
<OpenTelemetryInstrumentationAspNetCoreVersion>1.12.0</OpenTelemetryInstrumentationAspNetCoreVersion>
<OpenTelemetryInstrumentationHttpVersion>1.12.0</OpenTelemetryInstrumentationHttpVersion>
Expand Down Expand Up @@ -125,6 +129,7 @@
<MicrosoftExtensionsHttpLTSVersion>8.0.1</MicrosoftExtensionsHttpLTSVersion>
<SystemFormatsAsn1LTSVersion>8.0.2</SystemFormatsAsn1LTSVersion>
<SystemTextJsonLTSVersion>8.0.6</SystemTextJsonLTSVersion>
<SystemThreadingChannelsLTSVersion>8.0.0</SystemThreadingChannelsLTSVersion>
<!-- OpenTelemetry (OTel) -->
<OpenTelemetryLTSVersion>1.9.0</OpenTelemetryLTSVersion>
<OpenTelemetryInstrumentationGrpcNetClientLTSVersion>1.9.0-beta.1</OpenTelemetryInstrumentationGrpcNetClientLTSVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<TargetFrameworks>$(DefaultTargetFramework);net462</TargetFrameworks>
<IsPackable>true</IsPackable>
<IsAotCompatible>true</IsAotCompatible>
<IsAotCompatible Condition=" '$(TargetFramework)' != 'net462' ">true</IsAotCompatible>
<Description>Provides abstractions for service discovery. Interfaces defined in this package are implemented in Microsoft.Extensions.ServiceDiscovery and other service discovery packages.</Description>
<PackageIconFullPath>$(DefaultDotnetIconFullPath)</PackageIconFullPath>
<RootNamespace>Microsoft.Extensions.ServiceDiscovery</RootNamespace>
Expand All @@ -19,4 +19,10 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<PackageReference Include="Microsoft.Bcl.Memory" />
</ItemGroup>

<Import Condition=" '$(TargetFramework)' == 'net462' " Project="$(SharedDir)FxPolyfills\FxPolyfills.targets" />

</Project>
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<TargetFrameworks>net462;$(DefaultTargetFramework)</TargetFrameworks>
<IsPackable>true</IsPackable>
<IsAotCompatible>true</IsAotCompatible>
<IsAotCompatible Condition=" '$(TargetFramework)' != 'net462' ">true</IsAotCompatible>
<Description>Provides extensions to HttpClient that enable service discovery based on configuration.</Description>
<PackageIconFullPath>$(DefaultDotnetIconFullPath)</PackageIconFullPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" />
<InternalsVisibleTo Include="Microsoft.Extensions.ServiceDiscovery.Tests"/>
<InternalsVisibleTo Include="Microsoft.Extensions.ServiceDiscovery.Dns.Tests"/>
<InternalsVisibleTo Include="Microsoft.Extensions.ServiceDiscovery.Tests" />
<InternalsVisibleTo Include="Microsoft.Extensions.ServiceDiscovery.Dns.Tests" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.Extensions.ServiceDiscovery.Abstractions\Microsoft.Extensions.ServiceDiscovery.Abstractions.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<PackageReference Include="Microsoft.Bcl.TimeProvider" />
</ItemGroup>

<Import Condition=" '$(TargetFramework)' == 'net462' " Project="$(SharedDir)FxPolyfills\FxPolyfills.targets" />

</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.ServiceDiscovery;
using Microsoft.Extensions.ServiceDiscovery.Http;

#if NET
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
#endif

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
Expand Down Expand Up @@ -34,13 +37,15 @@ public static IHttpClientBuilder AddServiceDiscovery(this IHttpClientBuilder htt
return new ResolvingHttpDelegatingHandler(registry, options);
});

#if NET
// Configure the HttpClient to disable gRPC load balancing.
// This is done on all HttpClient instances but only impacts gRPC clients.
AddDisableGrpcLoadBalancingFilter(httpClientBuilder.Services, httpClientBuilder.Name);

#endif
return httpClientBuilder;
}

#if NET
private static void AddDisableGrpcLoadBalancingFilter(IServiceCollection services, string? name)
{
// A filter is used because it will always run last. This is important because the disable
Expand Down Expand Up @@ -86,4 +91,5 @@ public Action<HttpMessageHandlerBuilder> Configure(Action<HttpMessageHandlerBuil
};
}
}
#endif
}
28 changes: 28 additions & 0 deletions src/Shared/FxPolyfills/ArgumentException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System;

internal static partial class FxPolyfillArgumentException
{
extension(ArgumentException)
{
public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (string.IsNullOrEmpty(argument))
{
ThrowNullOrEmptyException(argument, paramName);
}
}
}

[DoesNotReturn]
private static void ThrowNullOrEmptyException(string? argument, string? paramName)
{
ArgumentNullException.ThrowIfNull(argument, paramName);
throw new ArgumentException("The value cannot be an empty string.", paramName);
}
}
24 changes: 24 additions & 0 deletions src/Shared/FxPolyfills/ArgumentNullException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System;

internal static partial class FxPolyfillArgumentNullException
{
extension(ArgumentNullException)
{
public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (argument is null)
{
Throw(paramName);
}
}
}

[DoesNotReturn]
internal static void Throw(string? paramName) => throw new ArgumentNullException(paramName);
}
10 changes: 10 additions & 0 deletions src/Shared/FxPolyfills/CallerArgumentExpressionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.CompilerServices;

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
internal sealed class CallerArgumentExpressionAttribute(string parameterName) : Attribute
{
public string ParameterName => parameterName;
}
43 changes: 43 additions & 0 deletions src/Shared/FxPolyfills/ConcurrentDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Collections.Concurrent;

internal static partial class FxPolyfillConcurrentDictionary
{
extension<TKey, TValue>(ConcurrentDictionary<TKey, TValue> dictionary)
{
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
{
if (dictionary.TryGetValue(key, out var existing))
{
return existing;
}

return dictionary.GetOrAdd(key, valueFactory(key));
}

public TValue GetOrAdd<TState>(TKey key, Func<TKey, TState, TValue> valueFactory, TState state)
{
if (dictionary.TryGetValue(key, out var existing))
{
return existing;
}

return dictionary.GetOrAdd(key, valueFactory(key, state));
}

public void TryRemove(TKey key)
{
dictionary.TryRemove(key, out _);
}

public void TryRemove(KeyValuePair<TKey, TValue> pair)
{
if (dictionary.TryRemove(pair.Key, out var existing) && !EqualityComparer<TValue>.Default.Equals(existing, pair.Value))
{
dictionary.TryAdd(pair.Key, pair.Value);
}
}
}
}
18 changes: 18 additions & 0 deletions src/Shared/FxPolyfills/ExceptionDispatchInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.ExceptionServices;

internal static partial class FxPolyfillExceptionDispatchInfo
{
extension(ExceptionDispatchInfo)
{
[DoesNotReturn]
public static void Throw(Exception ex)
{
ExceptionDispatchInfo.Capture(ex).Throw();
}
}
}
23 changes: 23 additions & 0 deletions src/Shared/FxPolyfills/FxPolyfills.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project>
<PropertyGroup>
<SharedFxPolyfillsDir>$(MSBuildThisFileDirectory)</SharedFxPolyfillsDir>
</PropertyGroup>

<ItemGroup>
<Reference Include="System.Net.Http" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: should this go directly into projects that need it as opposed to globally? It helps with readability when references are all in the same project file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it here because these are usings that are already automatically added to the projects for .net 8/9, so it continues to be transparent to the consuming library.


<Using Include="System.Net.Http" />
<Using Include="System.Threading.Tasks" />
</ItemGroup>

<ItemGroup>
<ExcludedFrameworkPolyfills
Condition="'@(PackageReference->WithMetadataValue('Identity', 'Microsoft.Bcl.TimeProvider'))' == ''"
Include="$(SharedFxPolyfillsDir)Task.TimeProvider.cs" />

<Compile Include="$(SharedFxPolyfillsDir)*.cs"
Exclude="@(ExcludedFrameworkPolyfills)"
Link="FxPolyfills/%(Filename).cs" />
</ItemGroup>

</Project>
59 changes: 59 additions & 0 deletions src/Shared/FxPolyfills/IPEndPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;

namespace System.Net;

internal static partial class FxPolyfillIPEndPoint
{
extension(IPEndPoint)
{
public static IPEndPoint Parse(string endpoint)
{
if (TryParse(endpoint.AsSpan(), out var result))
{
return result;
}

throw new FormatException("The endpoint format is invalid.");
}

public static bool TryParse(ReadOnlySpan<char> s, out IPEndPoint? result)
{
const int MaxPort = 0x0000FFFF;

int addressLength = s.Length; // If there's no port then send the entire string to the address parser
int lastColonPos = s.LastIndexOf(':');

// Look to see if this is an IPv6 address with a port.
if (lastColonPos > 0)
{
if (s[lastColonPos - 1] == ']')
{
addressLength = lastColonPos;
}
// Look to see if this is IPv4 with a port (IPv6 will have another colon)
else if (s.Slice(0, lastColonPos).LastIndexOf(':') == -1)
{
addressLength = lastColonPos;
}
}

if (IPAddress.TryParse(s.Slice(0, addressLength).ToString(), out IPAddress? address))
{
uint port = 0;
if (addressLength == s.Length ||
(uint.TryParse(s.Slice(addressLength + 1).ToString(), NumberStyles.None, CultureInfo.InvariantCulture, out port) && port <= MaxPort))

{
result = new IPEndPoint(address, (int)port);
return true;
}
}

result = null;
return false;
}
}
}
Loading
Loading