|
12 | 12 | using Microsoft.AspNetCore.Routing; |
13 | 13 | using Microsoft.AspNetCore.TestHost; |
14 | 14 | using Microsoft.Extensions.DependencyInjection; |
| 15 | +using Microsoft.Extensions.Hosting; |
15 | 16 | #else |
16 | 17 | using Lucene.Net.Replicator.Net; |
17 | 18 | #endif |
@@ -85,78 +86,90 @@ public static TestServer NewHttpServer(IReplicationService service, MockErrorCon |
85 | 86 | { |
86 | 87 | if (useStartupClass) |
87 | 88 | { |
88 | | - var builder = new WebHostBuilder() |
89 | | - .ConfigureServices(container => |
| 89 | + var hostBuilder = new HostBuilder() |
| 90 | + .ConfigureWebHost(webBuilder => |
90 | 91 | { |
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 | + }); |
94 | 98 |
|
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 | + }); |
103 | 108 |
|
104 | | - var server = new TestServer(builder); |
| 109 | + var host = hostBuilder.Build(); |
| 110 | + host.Start(); |
| 111 | + var server = host.GetTestServer(); |
105 | 112 | server.BaseAddress = new Uri("http://localhost" + ReplicationService.REPLICATION_CONTEXT); |
106 | 113 | return server; |
107 | 114 | } |
108 | 115 | else |
109 | 116 | { |
110 | | - var builder = new WebHostBuilder() |
111 | | - .ConfigureServices(container => |
| 117 | + var hostBuilder = new HostBuilder() |
| 118 | + .ConfigureWebHost(webBuilder => |
112 | 119 | { |
113 | | - container.AddRouting(); |
114 | | - container.AddSingleton(service); |
115 | | - container.AddSingleton(mockErrorConfig); |
116 | | - if (useSynchronousIO) |
| 120 | + webBuilder.UseTestServer() |
| 121 | + .ConfigureServices(container => |
117 | 122 | { |
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 => |
122 | 138 | { |
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(); |
133 | 140 |
|
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>(); |
138 | 143 |
|
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?}"; |
145 | 144 | if (useSynchronousIO) |
146 | | - endpoints.MapReplicator<SynchronousReplicationServiceMiddleware>(pattern); |
147 | | - else |
148 | | - endpoints.MapReplicator<ReplicationServiceMiddleware>(pattern); |
| 145 | + { |
| 146 | + app.UseMiddleware<EnableSynchronousIOMiddleware>(); |
| 147 | + } |
149 | 148 |
|
150 | | - endpoints.MapGet("/{controller?}/{action?}/{id?}", async context => |
| 149 | + app.UseEndpoints(endpoints => |
151 | 150 | { |
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 | + }); |
155 | 166 | }); |
156 | 167 | }); |
157 | 168 | }); |
158 | | - var server = new TestServer(builder); |
159 | | - return server; |
| 169 | + |
| 170 | + var host = hostBuilder.Build(); |
| 171 | + host.Start(); |
| 172 | + return host.GetTestServer(); |
160 | 173 | } |
161 | 174 | } |
162 | 175 |
|
|
0 commit comments