Skip to content

Commit b7b052a

Browse files
committed
Added new methods for handling file outputs
1 parent 0f45739 commit b7b052a

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

MZBase.Microservices/HttpServices/ServiceMediatorBase.cs

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.Extensions.Logging;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
64
using System.Net;
75
using System.Net.Http.Headers;
86
using System.Text;
97
using System.Text.Json;
10-
using System.Threading.Tasks;
118

129
namespace MZBase.Microservices.HttpServices
1310
{
@@ -542,7 +539,7 @@ protected async Task ProcessNotSuccessfulResponse(HttpResponseMessage? response,
542539
, methodName
543540
, _serviceUniqueName
544541
, _httpClientBaseAddress + apiUrl);
545-
throw new Exception("Service address not found: '"+ _httpClientBaseAddress + apiUrl+"', Reason Phrase:" + response.ReasonPhrase);
542+
throw new Exception("Service address not found: '" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response.ReasonPhrase);
546543
}
547544

548545
string? responsContent = null;
@@ -562,7 +559,7 @@ protected async Task ProcessNotSuccessfulResponse(HttpResponseMessage? response,
562559
, _serviceUniqueName
563560
, _httpClientBaseAddress + apiUrl);
564561

565-
throw new Exception("Failed to do post method:'" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response.ReasonPhrase);
562+
throw new Exception("Failed to do post method:'" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response.ReasonPhrase);
566563
}
567564
else
568565
{
@@ -576,7 +573,7 @@ protected async Task ProcessNotSuccessfulResponse(HttpResponseMessage? response,
576573
, _serviceUniqueName
577574
, _httpClientBaseAddress + apiUrl);
578575

579-
throw new Exception("Failed calling remote procedure:'" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" +response.ReasonPhrase + "," + responsContent);
576+
throw new Exception("Failed calling remote procedure:'" + _httpClientBaseAddress + apiUrl + "', Reason Phrase:" + response.ReasonPhrase + "," + responsContent);
580577
}
581578
}
582579

@@ -619,5 +616,70 @@ protected async Task ProcessNotSuccessfulResponse(HttpResponseMessage? response,
619616

620617

621618
}
619+
public async ValueTask<FileContentResult> GetFileContentResultAsync(string address)
620+
{
621+
_logger.LogInformation("Called method '{ServiceMethod}' from service '{Category}' for remote address '{RemoteAddress}'"
622+
, "GetAsync"
623+
, _serviceUniqueName
624+
, _httpClientBaseAddress + address);
625+
626+
627+
using var httpResponseMessage = await _httpClient.GetAsync(address);
628+
629+
630+
if (!httpResponseMessage.IsSuccessStatusCode)
631+
{
632+
await ProcessNotSuccessfulResponse(httpResponseMessage, address, "GetAsync");
633+
}
634+
635+
if (httpResponseMessage.StatusCode == HttpStatusCode.NoContent)
636+
{
637+
638+
return default;
639+
}
640+
641+
_logger.LogInformation("Successfully called remote procedure: Method '{ServiceMethod}' from service '{Category}' for remote address '{RemoteAddress}'"
642+
, "GetAsync"
643+
, _serviceUniqueName
644+
, _httpClientBaseAddress + address);
645+
646+
return new FileContentResult(await httpResponseMessage.Content.ReadAsByteArrayAsync(), httpResponseMessage.Content.Headers.ContentType.MediaType);
647+
648+
649+
}
650+
public async ValueTask<FileStreamResult> GetFileStreamAsync(string address)
651+
{
652+
_logger.LogInformation("Called method '{ServiceMethod}' from service '{Category}' for remote address '{RemoteAddress}'"
653+
, "GetAsync"
654+
, _serviceUniqueName
655+
, _httpClientBaseAddress + address);
656+
657+
658+
using var httpResponseMessage = await _httpClient.GetAsync(address);
659+
660+
661+
if (!httpResponseMessage.IsSuccessStatusCode)
662+
{
663+
await ProcessNotSuccessfulResponse(httpResponseMessage, address, "GetAsync");
664+
}
665+
666+
if (httpResponseMessage.StatusCode == HttpStatusCode.NoContent)
667+
{
668+
return default;
669+
}
670+
671+
_logger.LogInformation("Successfully called remote procedure: Method '{ServiceMethod}' from service '{Category}' for remote address '{RemoteAddress}'"
672+
, "GetAsync"
673+
, _serviceUniqueName
674+
, _httpClientBaseAddress + address);
675+
676+
// Fix CS1503: Use stream, not byte[]
677+
var stream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);
678+
679+
// Fix CS8602: Check for null before dereferencing
680+
var contentType = httpResponseMessage.Content.Headers.ContentType?.MediaType ?? "application/octet-stream";
681+
682+
return new FileStreamResult(stream, contentType);
683+
}
622684
}
623685
}

MZBase.Microservices/MZBase.Microservices.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<Version>2.0.1</Version>
7+
<Version>2.0.2</Version>
88
<Authors>Mahdi Zandakbari</Authors>
99
<Description>This package intends to provide some simple helpers to the developers of microserices in .Net 8</Description>
1010
<PackageProjectUrl>https://github.com/mzand111/MZBase</PackageProjectUrl>
@@ -13,6 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.3.0" />
16+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" />
1617
</ItemGroup>
1718

1819
</Project>

0 commit comments

Comments
 (0)