Skip to content

Commit 034c009

Browse files
fix(ReplicatorTestCase): Replace deprecated WebHostBuilder with HostBuilder (apache#1411) (apache#1416)
Signed-off-by: Shashwat Sinha <shashwat.sinha2003@gmail.com>
1 parent 69431e7 commit 034c009

1 file changed

Lines changed: 66 additions & 53 deletions

File tree

src/Lucene.Net.Tests.Replicator/ReplicatorTestCase.cs

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.AspNetCore.Routing;
1313
using Microsoft.AspNetCore.TestHost;
1414
using Microsoft.Extensions.DependencyInjection;
15+
using Microsoft.Extensions.Hosting;
1516
#else
1617
using Lucene.Net.Replicator.Net;
1718
#endif
@@ -85,78 +86,90 @@ public static TestServer NewHttpServer(IReplicationService service, MockErrorCon
8586
{
8687
if (useStartupClass)
8788
{
88-
var builder = new WebHostBuilder()
89-
.ConfigureServices(container =>
89+
var hostBuilder = new HostBuilder()
90+
.ConfigureWebHost(webBuilder =>
9091
{
91-
container.AddSingleton(service);
92-
container.AddSingleton(mockErrorConfig);
93-
});
92+
webBuilder.UseTestServer()
93+
.ConfigureServices(container =>
94+
{
95+
container.AddSingleton(service);
96+
container.AddSingleton(mockErrorConfig);
97+
});
9498

95-
if (useSynchronousIO)
96-
{
97-
builder.UseStartup<SynchronousReplicationServlet>();
98-
}
99-
else
100-
{
101-
builder.UseStartup<ReplicationServlet>();
102-
}
99+
if (useSynchronousIO)
100+
{
101+
webBuilder.UseStartup<SynchronousReplicationServlet>();
102+
}
103+
else
104+
{
105+
webBuilder.UseStartup<ReplicationServlet>();
106+
}
107+
});
103108

104-
var server = new TestServer(builder);
109+
var host = hostBuilder.Build();
110+
host.Start();
111+
var server = host.GetTestServer();
105112
server.BaseAddress = new Uri("http://localhost" + ReplicationService.REPLICATION_CONTEXT);
106113
return server;
107114
}
108115
else
109116
{
110-
var builder = new WebHostBuilder()
111-
.ConfigureServices(container =>
117+
var hostBuilder = new HostBuilder()
118+
.ConfigureWebHost(webBuilder =>
112119
{
113-
container.AddRouting();
114-
container.AddSingleton(service);
115-
container.AddSingleton(mockErrorConfig);
116-
if (useSynchronousIO)
120+
webBuilder.UseTestServer()
121+
.ConfigureServices(container =>
117122
{
118-
container.AddSingleton<SynchronousReplicationServiceMiddleware>();
119-
container.AddSingleton<EnableSynchronousIOMiddleware>();
120-
}
121-
else
123+
container.AddRouting();
124+
container.AddSingleton(service);
125+
container.AddSingleton(mockErrorConfig);
126+
if (useSynchronousIO)
127+
{
128+
container.AddSingleton<SynchronousReplicationServiceMiddleware>();
129+
container.AddSingleton<EnableSynchronousIOMiddleware>();
130+
}
131+
else
132+
{
133+
container.AddSingleton<ReplicationServiceMiddleware>();
134+
}
135+
container.AddSingleton<MockErrorMiddleware>();
136+
})
137+
.Configure(app =>
122138
{
123-
container.AddSingleton<ReplicationServiceMiddleware>();
124-
}
125-
container.AddSingleton<MockErrorMiddleware>();
126-
})
127-
.Configure(app =>
128-
{
129-
app.UseRouting();
130-
131-
// Middleware so we can mock a server exception and toggle the exception on and off.
132-
app.UseMiddleware<MockErrorMiddleware>();
139+
app.UseRouting();
133140

134-
if (useSynchronousIO)
135-
{
136-
app.UseMiddleware<EnableSynchronousIOMiddleware>();
137-
}
141+
// Middleware so we can mock a server exception and toggle the exception on and off.
142+
app.UseMiddleware<MockErrorMiddleware>();
138143

139-
app.UseEndpoints(endpoints =>
140-
{
141-
// This is to define the endpoint for Replicator.
142-
// All URLs with the pattern /replicate/{shard?}/{action?} terminate here and any middleware that
143-
// is expected to run for Replicator must be registered before this call.
144-
string pattern = ReplicationService.REPLICATION_CONTEXT + "/{shard?}/{action?}";
145144
if (useSynchronousIO)
146-
endpoints.MapReplicator<SynchronousReplicationServiceMiddleware>(pattern);
147-
else
148-
endpoints.MapReplicator<ReplicationServiceMiddleware>(pattern);
145+
{
146+
app.UseMiddleware<EnableSynchronousIOMiddleware>();
147+
}
149148

150-
endpoints.MapGet("/{controller?}/{action?}/{id?}", async context =>
149+
app.UseEndpoints(endpoints =>
151150
{
152-
// This is just to demonstrate allowing requests to other services/controllers in the same
153-
// application. This isn't required, but is allowed.
154-
await context.Response.WriteAsync("Hello World!");
151+
// This is to define the endpoint for Replicator.
152+
// All URLs with the pattern /replicate/{shard?}/{action?} terminate here and any middleware that
153+
// is expected to run for Replicator must be registered before this call.
154+
string pattern = ReplicationService.REPLICATION_CONTEXT + "/{shard?}/{action?}";
155+
if (useSynchronousIO)
156+
endpoints.MapReplicator<SynchronousReplicationServiceMiddleware>(pattern);
157+
else
158+
endpoints.MapReplicator<ReplicationServiceMiddleware>(pattern);
159+
160+
endpoints.MapGet("/{controller?}/{action?}/{id?}", async context =>
161+
{
162+
// This is just to demonstrate allowing requests to other services/controllers in the same
163+
// application. This isn't required, but is allowed.
164+
await context.Response.WriteAsync("Hello World!");
165+
});
155166
});
156167
});
157168
});
158-
var server = new TestServer(builder);
159-
return server;
169+
170+
var host = hostBuilder.Build();
171+
host.Start();
172+
return host.GetTestServer();
160173
}
161174
}
162175

0 commit comments

Comments
 (0)