1 year ago
#305470
user2948533
How can I consume a FileContentResult returned from an .Net Core api inside a .Net WebApi?
I am trying something similar posted [here][1]
[1]: .Net Core Receive FileContentResult from one API to Another This is what I am trying and I think I am very close,just that not sure how to receive the in the receiver Api a response which is being sent by the Producer Api as FileContentResult, any help will be appreciated. My effort is listed below: //Source API-working well
[HttpPost("download")]
public async Task<FileContentResult> ExportApplicationUsers2(ExportApplicationUsersQuery command)
{
try
{
var filePath = await Mediator.Send(command);
return File(System.IO.File.ReadAllBytes(filePath), "application/octet-stream");
}
catch (Exception ex)
{
throw ex.InnerException;
}
}
//Controller in Consumer Api
public async Task<Result<FileContentResult>> ExportApplicationUsers(ExportUsersRequest model)
{
try
{
var httpClient = _httpClientFactory.CreateClient();
var response = await httpClient.PostFileAsync("/users/download", JsonConvert.SerializeObject(model));
return response;
//return File(System.IO.File.ReadAllBytes(response), "application/octet-stream");
}
catch (Exception ex)
{
_logger.Error(ex.Message, ex);
return new Result<FileContentResult>(new List<string> { ex.Message });
}
}
The Actual PostFileAsync() which I need to fix first:
public async Task<FileContentResult> PostFileAsync(string uri, object data)
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
if (data != null)
{
request.Content = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
var response = default(FileContentResult);
var actionTask = _httpClient.SendAsync(request)
.ContinueWith(responseTask =>
{
FileStreamResult resposneMessage = responseTask.Result;
response = (FileContentResult)resposneMessage.ReadAsStreamAsync().Result;//This is where I need help as its not able to convert into FileContentResult!
});
actionTask.Wait();
return response;
}
c#
.net-core
filecontentresult
0 Answers
Your Answer