Skip to content

Commit f35d5b0

Browse files
committed
Merge in 'release/6.0' changes
2 parents 32a62c9 + c91170a commit f35d5b0

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

src/libraries/Microsoft.Extensions.HostFactoryResolver/src/HostFactoryResolver.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,25 @@ internal sealed class HostFactoryResolver
2020
public const string BuildWebHost = nameof(BuildWebHost);
2121
public const string CreateWebHostBuilder = nameof(CreateWebHostBuilder);
2222
public const string CreateHostBuilder = nameof(CreateHostBuilder);
23+
private const string TimeoutEnvironmentKey = "DOTNET_HOST_FACTORY_RESOLVER_DEFAULT_TIMEOUT_IN_SECONDS";
2324

2425
// The amount of time we wait for the diagnostic source events to fire
25-
private static readonly TimeSpan s_defaultWaitTimeout = Debugger.IsAttached ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(5);
26+
private static readonly TimeSpan s_defaultWaitTimeout = SetupDefaultTimout();
27+
28+
private static TimeSpan SetupDefaultTimout()
29+
{
30+
if (Debugger.IsAttached)
31+
{
32+
return Timeout.InfiniteTimeSpan;
33+
}
34+
35+
if (uint.TryParse(Environment.GetEnvironmentVariable(TimeoutEnvironmentKey), out uint timeoutInSeconds))
36+
{
37+
return TimeSpan.FromSeconds((int)timeoutInSeconds);
38+
}
39+
40+
return TimeSpan.FromMinutes(5);
41+
}
2642

2743
public static Func<string[], TWebHost>? ResolveWebHostFactory<TWebHost>(Assembly assembly)
2844
{
@@ -229,7 +245,7 @@ public object CreateHost()
229245

230246
// Try to set an exception if the entry point returns gracefully, this will force
231247
// build to throw
232-
_hostTcs.TrySetException(new InvalidOperationException("Unable to build IHost"));
248+
_hostTcs.TrySetException(new InvalidOperationException("The entry point exited without ever building an IHost."));
233249
}
234250
catch (TargetInvocationException tie) when (tie.InnerException is StopTheHostException)
235251
{
@@ -268,7 +284,7 @@ public object CreateHost()
268284
// Wait before throwing an exception
269285
if (!_hostTcs.Task.Wait(_waitTimeout))
270286
{
271-
throw new InvalidOperationException("Unable to build IHost");
287+
throw new InvalidOperationException($"Timed out waiting for the entry point to build the IHost after {s_defaultWaitTimeout}. This timeout can be modified using the '{TimeoutEnvironmentKey}' environment variable.");
272288
}
273289
}
274290
catch (AggregateException) when (_hostTcs.Task.IsCompleted)

src/libraries/Microsoft.Extensions.HostFactoryResolver/src/Microsoft.Extensions.HostFactoryResolver.Sources.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<IsSourcePackage>true</IsSourcePackage>
1010
<!-- This is non-shipping package. -->
1111
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
12+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
13+
<ServicingVersion>1</ServicingVersion>
1214
<PackageDescription>Internal package for sharing Microsoft.Extensions.Hosting.HostFactoryResolver type.</PackageDescription>
1315
</PropertyGroup>
1416
</Project>

src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/HostFactoryResolverTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ public void TopLevelStatements()
253253
Assert.IsAssignableFrom<IServiceProvider>(factory(Array.Empty<string>()));
254254
}
255255

256+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
257+
public void TopLevelStatementsTestsTimeout()
258+
{
259+
var assembly = Assembly.Load("TopLevelStatementsTestsTimeout");
260+
var factory = HostFactoryResolver.ResolveServiceProviderFactory(assembly, s_WaitTimeout);
261+
262+
Assert.NotNull(factory);
263+
Assert.Throws<InvalidOperationException>(() => factory(Array.Empty<string>()));
264+
}
265+
256266
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
257267
public void ApplicationNameSetFromAgrument()
258268
{

src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/Microsoft.Extensions.HostFactoryResolver.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<ProjectReference Include="NoSpecialEntryPointPatternHangs\NoSpecialEntryPointPatternHangs.csproj" />
2929
<ProjectReference Include="NoSpecialEntryPointPatternMainNoArgs\NoSpecialEntryPointPatternMainNoArgs.csproj" />
3030
<ProjectReference Include="TopLevelStatements\TopLevelStatements.csproj" />
31+
<ProjectReference Include="TopLevelStatementsTestsTimeout\TopLevelStatementsTestsTimeout.csproj" />
3132
</ItemGroup>
3233

3334
<ItemGroup>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Threading;
6+
using Microsoft.Extensions.Hosting;
7+
8+
var hostBuilder = new HostBuilder();
9+
Thread.Sleep(TimeSpan.FromSeconds(30));
10+
hostBuilder.Build();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(NetCoreAppCurrent);net461</TargetFrameworks>
5+
<EnableDefaultItems>true</EnableDefaultItems>
6+
<OutputType>Exe</OutputType>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\MockHostTypes\MockHostTypes.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)