public HangfireServiceProviderTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { // Register Correlate, Hangfire and tell Hangfire to use Correlate. IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection .AddCorrelate() .AddHangfire((s, config) => { config .UseCorrelate(s) .UseMemoryStorage(); }); // Below, dependencies for test only. #if NETCOREAPP2_2 // Register a typed client which is used by the job to call an endpoint. // We use it to assert the request header contains the correlation id. serviceCollection .AddHttpClient <TestService>(client => { client.BaseAddress = new Uri("http://0.0.0.0"); }) .ConfigurePrimaryHttpMessageHandler(() => MockHttp) .CorrelateRequests(); #else // HttpClientFactory is not supported below .NET Core 2.1. // This service registration is somewhat similar to what the HttpClientFactory extension does. serviceCollection.AddTransient(s => { var correlatingHttpMessageHandler = new CorrelatingHttpMessageHandler( s.GetRequiredService <ICorrelationContextAccessor>(), Options.Create(new CorrelateClientOptions()) ) { InnerHandler = MockHttp }; return(new TestService( new HttpClient(correlatingHttpMessageHandler, true) { BaseAddress = new Uri("http://0.0.0.0") } )); }); #endif serviceCollection .AddSingleton(ExecutedJobs) .AddSingleton(testOutputHelper) // In ASP.NET Core, you'd use UseHangfireServer() which internally creates BackgroundJobServer. // Since we're testing all target frameworks, just register manually. .AddTransient <IBackgroundProcessingServer, BackgroundJobServer>(); _services = serviceCollection.BuildServiceProvider(); }
private TestService CreateTestService() { var correlatingHttpMessageHandler = new CorrelatingHttpMessageHandler( new CorrelationContextAccessor(), Options.Create(new CorrelateClientOptions()) ) { InnerHandler = MockHttp }; return(new TestService( new HttpClient(correlatingHttpMessageHandler, true) { BaseAddress = new Uri("http://0.0.0.0") } )); }