Код: Выделить всё
public class SsrsReportService
{
private readonly IHttpClientFactory _httpClientFactory;
public SsrsReportService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
///
/// Get the report PDF bytes from SSRS.
///
///
The report name.
///
///
public async Task GetReportPdfAsync(string reportPath)
{
var _http = _httpClientFactory.CreateClient("SsrsReportServiceClient");
try
{
var rptSvrAddr = mmAppBase.AppSettingsMgr.GetSetting("ReportServer");
var serverReportPath = $"{rptSvrAddr}{reportPath}&rs:Format=PDF";
var request = new HttpRequestMessage(HttpMethod.Get, serverReportPath);
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/pdf"));
request.Headers.IfModifiedSince = new DateTimeOffset(DateTime.Now);
var response = await _http.SendAsync(request);
if (!response.IsSuccessStatusCode)
throw new Exception($"Failed to get report: {response.StatusCode}");
return await response.Content.ReadAsByteArrayAsync();
}
catch (Exception)
{
throw;
}
finally
{
_http.Dispose(); // Ensure the HttpClient is disposed
}
}
}
< /code>
Вот как я добавляю его, мой сервисный уровень: < /p>
// Add Reporting Services Base Address.
services.AddHttpClient("SsrsReportServiceClient")
.ConfigureHttpClient(client =>
{
var baseRptSvr = mmAppBase.AppSettingsMgr.GetSetting("BaseRptSrv");
client.BaseAddress = new Uri(baseRptSvr);
}).ConfigurePrimaryHttpMessageHandler(() =>
{
// Use default credentials for SSRS
return new HttpClientHandler
{
UseDefaultCredentials = true,
AllowAutoRedirect = false // Prevent automatic redirects
};
});
services.AddTransient();
Любые предложения о том, как я должен это делать>
Подробнее здесь: https://stackoverflow.com/questions/796 ... rvices-whe