-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathTestWebHostBuilderFactory.cs
More file actions
54 lines (44 loc) · 1.59 KB
/
TestWebHostBuilderFactory.cs
File metadata and controls
54 lines (44 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Steeltoe.Common.TestResources;
public static class TestWebHostBuilderFactory
{
private static readonly Action<IApplicationBuilder> EmptyAction = _ =>
{
};
private static readonly Action<ServiceProviderOptions> ConfigureServiceProvider = options =>
{
options.ValidateScopes = true;
options.ValidateOnBuild = true;
};
public static WebHostBuilder Create()
{
return Create(true);
}
public static WebHostBuilder Create(bool useTestServer)
{
var builder = new WebHostBuilder();
ConfigureBuilder(builder, useTestServer, true);
return builder;
}
private static void ConfigureBuilder(WebHostBuilder builder, bool useTestServer, bool deactivateDiagnostics)
{
builder.UseDefaultServiceProvider(ConfigureServiceProvider);
builder.Configure(EmptyAction);
if (useTestServer)
{
builder.UseTestServer();
}
if (deactivateDiagnostics)
{
builder.ConfigureServices(services => services.Replace(ServiceDescriptor.Singleton<DiagnosticListener, InactiveDiagnosticListener>()));
}
}
}