public async Task <ReportOutput> ExecuteReportOutputAsync(ReportDefinition reportDefinition, IDictionary <string, string> values) { ReportCommandContext context = new ReportCommandContext(reportDefinition, reportDefinition.Output.Command); if (!string.IsNullOrWhiteSpace(reportDefinition.ContextProvider)) { Type contextProviderType = Type.GetType(reportDefinition.ContextProvider, true, true); context.ContextProvider = ActivatorUtilities.CreateInstance(this.ServiceProvider, contextProviderType); } var reportCommand = await this.CommandBuilder.BuildCommandAsync(context, values); using (IServiceScope serviceScope = this.ServiceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) { if (!reportDefinition.TryFindDataSourceDefinition(reportDefinition.Output, out DataSourceDefinition dataSourceDefinition)) { throw new InvalidOperationException($"Could not find data source definition '{reportDefinition.Output.DataSource}' in the report '{reportDefinition.Name}'."); } var dataSourceRegistrationOptionsSnapshot = serviceScope.ServiceProvider.GetRequiredService <IOptionsSnapshot <DataSourceRegistrationOptions> >(); var dataSourceRegistrationOptions = dataSourceRegistrationOptionsSnapshot.Get(dataSourceDefinition.Type); if (null == dataSourceRegistrationOptions.TypeHandle || IntPtr.Zero == dataSourceRegistrationOptions.TypeHandle.Value) { throw new InvalidOperationException($"No data source service is registered for data source type '{dataSourceDefinition.Type}'."); } Type dataSourceType = Type.GetTypeFromHandle(dataSourceRegistrationOptions.TypeHandle); IDataSource dataSource = (IDataSource)serviceScope.ServiceProvider.GetRequiredService(dataSourceType); await dataSource.OpenAsync(dataSourceDefinition); try { var dataset = await dataSource.GetDataAsync(reportCommand, reportDefinition.EnumerateFieldDefinitions().ToArray()); return(new ReportOutput(reportCommand, dataset)); } finally { await dataSource.CloseAsync(); } } }