You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using NSwag to generate a C# client from a dotnet 7 Web API that is consumed by a Blazor WASM application. I am having trouble getting a Post with an IFormFile to upload an attachment with the client.
Here is my API Controller method. Note that it takes an IFormFile parameter.
[MapToApiVersion(Constants.ApiVersions.V1)]
[HttpPost]
[ProducesResponseType(typeof(BlobContentInfo), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(IEnumerable<ValidationError>), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<BlobContentInfo>> Post(
[FromRoute] int projectId,
[FromRoute] int equipmentId,
[FromForm] string? description,
[FromForm] IFormFile file,
[FromForm] AttachmentType? attachmentType = null)
{
return await TryGetAsync<ActionResult<BlobContentInfo>>(async _ =>
{
var user = await GetUser();
var equipment = await Mediator.Send(new GetEquipment { EquipmentId = equipmentId, ProjectId = projectId, User = user });
if (equipment == null)
return NotFound();
var attachment = new AttachmentUpload
{
EquipmentId = equipmentId,
UploadedOn = DateTimeOffset.UtcNow,
AttachmentType = attachmentType,
Description = description,
FormFile = file
};
var result = await _validator.ValidateAsync(attachment);
if (!result.IsValid)
return BadRequest(result.ToValidationErrors());
var uploaded = await Mediator.Send(new UploadAttachment
{
ProjectId = projectId,
AttachmentUpload = attachment,
User = user
});
return CreatedAtAction(nameof(Get), new { projectId, equipmentId }, uploaded);
}, (ex, _) => Error<BlobContentInfo>(ex));
}
Here is my Service Method which calls the generated API client method.
Note that is splits the IFormFile into parameters which make up its constituent parts. Is there a way to avoid this?
Note that the IHeaderDictionary expected by the generated client is of type Api.Client.IHeaderDictionary instead of Microsoft.AspNetCore.Http.IHeaderDictionary. Is there a way to make the generated client use Microsoft.AspNetCore.Http.IHeaderDictionary instead? I've tried an extension method to convert, but this doesn't work as there's no concrete Api.Client.IHeaderDictionary. I've also looked at the nswag.json, but I don't see anything in there that could accomplish this.
public async Task<ServiceResult<BlobContentInfo>> UploadEquipmentAttachment(
int projectId,
int equipmentId,
AttachmentUpload attachment,
CancellationToken cancellationToken = default)
{
return await TryGetAsync(async _ =>
{
var response =
await ApiClient.V1ProjectsEquipmentAttachmentsPostAsync(
projectId,
equipmentId,
attachment.Description,
attachment.FormFile.ContentType,
attachment.FormFile.ContentDisposition,
attachment.FormFile.Headers, // This fails as the NSwag client is expecting Api.Client.IHeaderDictionary instead of Microsoft.AspNetCore.Http.IHeaderDictionary
attachment.FormFile.Length,
attachment.FormFile.Name,
attachment.FormFile.FileName,
attachment.AttachmentType,
cancellationToken);
return new ServiceResult<BlobContentInfo>
{
Status = OperationResult.Success,
HttpResponseCode = (HttpStatusCode)response.StatusCode,
Data = response.Result
};
}, (ex, _) => Task.FromResult(HandleClientApiException<BlobContentInfo>(ex)));
}
Not an answer, but the only way I could get this to work was by changing the API to accept a byte[] instead of an IFormFile and then converting the byte[] to an IFormFile in the API before sending to Azure Blob storage.
I am using NSwag to generate a C# client from a dotnet 7 Web API that is consumed by a Blazor WASM application. I am having trouble getting a Post with an
IFormFile
to upload an attachment with the client.Here is my API Controller method. Note that it takes an
IFormFile
parameter.Here is my Service Method which calls the generated API client method.
IFormFile
into parameters which make up its constituent parts. Is there a way to avoid this?IHeaderDictionary
expected by the generated client is of typeApi.Client.IHeaderDictionary
instead ofMicrosoft.AspNetCore.Http.IHeaderDictionary
. Is there a way to make the generated client useMicrosoft.AspNetCore.Http.IHeaderDictionary
instead? I've tried an extension method to convert, but this doesn't work as there's no concreteApi.Client.IHeaderDictionary
. I've also looked at thenswag.json
, but I don't see anything in there that could accomplish this.Here is my
nswag.json
configuration:Here is where I wire-up the client from
Program.cs
The text was updated successfully, but these errors were encountered: