Skip to content
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

Make Extensions linker friendly. #40431

Merged
merged 5 commits into from
Aug 7, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Reflection;

namespace Microsoft.Extensions.Logging
{
internal static class ProviderAliasUtilities
{
private const string AliasAttibuteTypeFullName = "Microsoft.Extensions.Logging.ProviderAliasAttribute";
private const string AliasAttibuteAliasProperty = "Alias";

internal static string GetAlias(Type providerType)
{
foreach (object attribute in providerType.GetTypeInfo().GetCustomAttributes(inherit: false))
foreach (CustomAttributeData attributeData in CustomAttributeData.GetCustomAttributes(providerType))
{
if (attribute.GetType().FullName == AliasAttibuteTypeFullName)
if (attributeData.AttributeType.FullName == AliasAttibuteTypeFullName)
{
PropertyInfo valueProperty = attribute
.GetType()
.GetProperty(AliasAttibuteAliasProperty, BindingFlags.Public | BindingFlags.Instance);

if (valueProperty != null)
foreach (CustomAttributeTypedArgument arg in attributeData.ConstructorArguments)
{
return valueProperty.GetValue(attribute) as string;
Debug.Assert(arg.ArgumentType == typeof(string));

return arg.Value?.ToString();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net461</TargetFrameworks>
<RootNamespace>Microsoft.Extensions.Hosting</RootNamespace>
<EnableDefaultItems>true</EnableDefaultItems>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Abstractions\src\Microsoft.Extensions.Configuration.Abstractions.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.DependencyInjection.Abstractions\src\Microsoft.Extensions.DependencyInjection.Abstractions.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;

Expand All @@ -15,7 +16,7 @@ public static class ServiceCollectionHostedServiceExtensions
/// <typeparam name="THostedService">An <see cref="IHostedService"/> to register.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
/// <returns>The original <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddHostedService<THostedService>(this IServiceCollection services)
public static IServiceCollection AddHostedService<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THostedService>(this IServiceCollection services)
where THostedService : class, IHostedService
{
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, THostedService>());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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;
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;

class Program
{
static int Main(string[] args)
{
IServiceCollection descriptors = new ServiceCollection();
descriptors.AddHostedService<MyHost>();

ServiceProvider provider = descriptors.BuildServiceProvider();

foreach (IHostedService h in provider.GetServices<IHostedService>())
{
if (!(h is MyHost))
{
return -1;
}
}

return 100;
}

private class MyHost : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project DefaultTargets="Build">
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" />

<PropertyGroup>
<AdditionalProjectReferences>Microsoft.Extensions.Hosting.Abstractions;Microsoft.Extensions.DependencyInjection</AdditionalProjectReferences>
</PropertyGroup>

<ItemGroup>
<TestConsoleAppSourceFiles Include="AddHostedServiceTests.cs" />
</ItemGroup>

<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets))" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Http",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Http", "src\Microsoft.Extensions.Http.csproj", "{A50C941B-09D7-4AB4-85AF-D02E6F96885A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Http.Tests", "tests\Microsoft.Extensions.Http.Tests.csproj", "{C9FE0B1E-35F6-4AFE-A9A6-DA6C1B88C1E7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{92E18BB1-B630-4B03-9B05-47F31D1BF94A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Http.Tests", "tests\Microsoft.Extensions.Http.Tests\Microsoft.Extensions.Http.Tests.csproj", "{7DADD323-891C-4EA7-8906-6BC4009FA083}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -31,23 +31,23 @@ Global
{A50C941B-09D7-4AB4-85AF-D02E6F96885A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A50C941B-09D7-4AB4-85AF-D02E6F96885A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A50C941B-09D7-4AB4-85AF-D02E6F96885A}.Release|Any CPU.Build.0 = Release|Any CPU
{C9FE0B1E-35F6-4AFE-A9A6-DA6C1B88C1E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9FE0B1E-35F6-4AFE-A9A6-DA6C1B88C1E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9FE0B1E-35F6-4AFE-A9A6-DA6C1B88C1E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9FE0B1E-35F6-4AFE-A9A6-DA6C1B88C1E7}.Release|Any CPU.Build.0 = Release|Any CPU
{92E18BB1-B630-4B03-9B05-47F31D1BF94A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92E18BB1-B630-4B03-9B05-47F31D1BF94A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92E18BB1-B630-4B03-9B05-47F31D1BF94A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92E18BB1-B630-4B03-9B05-47F31D1BF94A}.Release|Any CPU.Build.0 = Release|Any CPU
{7DADD323-891C-4EA7-8906-6BC4009FA083}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7DADD323-891C-4EA7-8906-6BC4009FA083}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7DADD323-891C-4EA7-8906-6BC4009FA083}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7DADD323-891C-4EA7-8906-6BC4009FA083}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{183E40F1-33A4-461D-AADF-4CF59FB93CCA} = {B51DC955-13A6-41ED-A6C7-DDAB98911513}
{A50C941B-09D7-4AB4-85AF-D02E6F96885A} = {0517D0EB-1ED7-420A-A7D7-9A167C102ED2}
{C9FE0B1E-35F6-4AFE-A9A6-DA6C1B88C1E7} = {9DE482AA-076A-4EB1-821F-EE2A4A230B4A}
{92E18BB1-B630-4B03-9B05-47F31D1BF94A} = {9DE482AA-076A-4EB1-821F-EE2A4A230B4A}
{7DADD323-891C-4EA7-8906-6BC4009FA083} = {9DE482AA-076A-4EB1-821F-EE2A4A230B4A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F300086C-E02A-417C-8659-59AA5139E68A}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Extensions.Http
{
internal class DefaultTypedHttpClientFactory<TClient> : ITypedHttpClientFactory<TClient>
internal class DefaultTypedHttpClientFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient> :
ITypedHttpClientFactory<TClient>
{
private readonly Cache _cache;
private readonly IServiceProvider _services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Threading;
Expand Down Expand Up @@ -316,7 +317,8 @@ public static IHttpClientBuilder ConfigureHttpMessageHandlerBuilder(this IHttpCl
/// scope bound to the message handler, which is managed independently.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddTypedClient<TClient>(this IHttpClientBuilder builder)
public static IHttpClientBuilder AddTypedClient<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>(
this IHttpClientBuilder builder)
where TClient : class
{
if (builder == null)
Expand All @@ -327,7 +329,8 @@ public static IHttpClientBuilder AddTypedClient<TClient>(this IHttpClientBuilder
return AddTypedClientCore<TClient>(builder, validateSingleType: false);
}

internal static IHttpClientBuilder AddTypedClientCore<TClient>(this IHttpClientBuilder builder, bool validateSingleType)
internal static IHttpClientBuilder AddTypedClientCore<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>(
this IHttpClientBuilder builder, bool validateSingleType)
where TClient : class
{
ReserveClient(builder, typeof(TClient), builder.Name, validateSingleType);
Expand Down Expand Up @@ -375,7 +378,8 @@ internal static IHttpClientBuilder AddTypedClientCore<TClient>(this IHttpClientB
/// scope bound to the message handler, which is managed independently.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddTypedClient<TClient, TImplementation>(this IHttpClientBuilder builder)
public static IHttpClientBuilder AddTypedClient<TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(
this IHttpClientBuilder builder)
where TClient : class
where TImplementation : class, TClient
{
Expand All @@ -387,7 +391,8 @@ public static IHttpClientBuilder AddTypedClient<TClient, TImplementation>(this I
return AddTypedClientCore<TClient, TImplementation>(builder, validateSingleType: false);
}

internal static IHttpClientBuilder AddTypedClientCore<TClient, TImplementation>(this IHttpClientBuilder builder, bool validateSingleType)
internal static IHttpClientBuilder AddTypedClientCore<TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(
this IHttpClientBuilder builder, bool validateSingleType)
where TClient : class
where TImplementation : class, TClient
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
Expand Down Expand Up @@ -198,7 +199,8 @@ public static IHttpClientBuilder AddHttpClient(this IServiceCollection services,
/// <typeparamref name="TClient"/> as the service type.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection services)
public static IHttpClientBuilder AddHttpClient<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>(
this IServiceCollection services)
where TClient : class
{
if (services == null)
Expand Down Expand Up @@ -240,7 +242,8 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection
/// <typeparamref name="TClient"/> as the service type.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services)
public static IHttpClientBuilder AddHttpClient<TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(
this IServiceCollection services)
where TClient : class
where TImplementation : class, TClient
{
Expand Down Expand Up @@ -282,7 +285,8 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS
/// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection services, string name)
public static IHttpClientBuilder AddHttpClient<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>(
this IServiceCollection services, string name)
where TClient : class
{
if (services == null)
Expand Down Expand Up @@ -332,7 +336,8 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection
/// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services, string name)
public static IHttpClientBuilder AddHttpClient<TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(
this IServiceCollection services, string name)
where TClient : class
where TImplementation : class, TClient
{
Expand Down Expand Up @@ -376,7 +381,8 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS
/// <typeparamref name="TClient"/> as the service type.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection services, Action<HttpClient> configureClient)
public static IHttpClientBuilder AddHttpClient<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>(
this IServiceCollection services, Action<HttpClient> configureClient)
where TClient : class
{
if (services == null)
Expand Down Expand Up @@ -421,7 +427,8 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection
/// <typeparamref name="TClient"/> as the service type.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection services, Action<IServiceProvider, HttpClient> configureClient)
public static IHttpClientBuilder AddHttpClient<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>(
this IServiceCollection services, Action<IServiceProvider, HttpClient> configureClient)
where TClient : class
{
if (services == null)
Expand Down Expand Up @@ -470,7 +477,8 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection
/// <typeparamref name="TClient"/> as the service type.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services, Action<HttpClient> configureClient)
public static IHttpClientBuilder AddHttpClient<TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(
this IServiceCollection services, Action<HttpClient> configureClient)
where TClient : class
where TImplementation : class, TClient
{
Expand Down Expand Up @@ -520,7 +528,8 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS
/// <typeparamref name="TClient"/> as the service type.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services, Action<IServiceProvider, HttpClient> configureClient)
public static IHttpClientBuilder AddHttpClient<TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(
this IServiceCollection services, Action<IServiceProvider, HttpClient> configureClient)
where TClient : class
where TImplementation : class, TClient
{
Expand Down Expand Up @@ -569,7 +578,8 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS
/// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection services, string name, Action<HttpClient> configureClient)
public static IHttpClientBuilder AddHttpClient<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>(
this IServiceCollection services, string name, Action<HttpClient> configureClient)
where TClient : class
{
if (services == null)
Expand Down Expand Up @@ -621,7 +631,8 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection
/// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection services, string name, Action<IServiceProvider, HttpClient> configureClient)
public static IHttpClientBuilder AddHttpClient<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TClient>(
this IServiceCollection services, string name, Action<IServiceProvider, HttpClient> configureClient)
where TClient : class
{
if (services == null)
Expand Down Expand Up @@ -677,7 +688,8 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection
/// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services, string name, Action<HttpClient> configureClient)
public static IHttpClientBuilder AddHttpClient<TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(
this IServiceCollection services, string name, Action<HttpClient> configureClient)
where TClient : class
where TImplementation : class, TClient
{
Expand Down Expand Up @@ -734,7 +746,8 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS
/// Use <see cref="Options.Options.DefaultName"/> as the name to configure the default client.
/// </para>
/// </remarks>
public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IServiceCollection services, string name, Action<IServiceProvider, HttpClient> configureClient)
public static IHttpClientBuilder AddHttpClient<TClient, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(
this IServiceCollection services, string name, Action<IServiceProvider, HttpClient> configureClient)
where TClient : class
where TImplementation : class, TClient
{
Expand Down
Loading