private static IServiceProvider GetServices <TStartup>(WebApplicationFactory <TStartup> factory) where TStartup : class { var server = factory.Server; if (server == null) { var message = $"When running on 2.x, the server is not initialized until it is explicitly started or the first client is created. " + $"Consider using '{nameof(TryGetTestSink)}()' instead."; throw new InvalidOperationException(message); } IWebHost host; try { host = server.Host; } catch (InvalidOperationException) { // We are probably running on 3.0 with generic host // but we are referencing a lower version of the package here // so try to retrieve the Services with reflection if (factory.GetType().GetProperty("Services")?.GetValue(factory, null) is IServiceProvider services) { return(services); } // It looks like, after all, we are not running on 3.0 throw; } return(host.Services); }
private static bool TryGetServices <TStartup>(WebApplicationFactory <TStartup> factory, out IServiceProvider?services) where TStartup : class { IWebHost?host = null; try { host = factory.Server?.Host; } catch (InvalidOperationException) { // We are probably running on 3.0 with generic host // but we are referencing a lower version of the package here // so try to retrieve the Services with reflection if (factory.GetType().GetProperty("Services")?.GetValue(factory, null) is IServiceProvider serviceProvider) { services = serviceProvider; return(true); } } services = host?.Services; return(services != null ? true : false); }