ASP.NET 核心 FromBody 模型绑定:使用 Interafece 字段绑定类



我一直在为此苦苦挣扎,我发现了一些问题,但没有一个可以回答我的需求。我会尝试发布一个更好的问题和我尝试过的一些事情。

情况如下: 我有一个APIGateway和一个WebApp。WebApp将POST请求发送到APIGateway,到目前为止一切顺利。我使用 FromBody 属性发送更大的对象,这也很好,直到我引入了接口 :)(

下面是一些代码:

网络应用:

public interface ICommand
{
Guid CorrelationId { get; set; }
Guid SocketId { get; set; }
}
public class Command : ICommand
{       
public Command(Guid CorrelationId, Guid SocketId)
{
this.CorrelationId = CorrelationId;
this.SocketId = SocketId;
}
public Guid CorrelationId { get; set; } = new Guid();
public Guid SocketId { get; set; } = new Guid();
}    
public interface IDocument
{
Guid Id { get; set; }
ulong Number { get; set; }
}
public class Document : IDocument
{
public Guid Id { get; set; } = new Guid();
public ulong Number { get; set; } = 0;
}
public interface ICreateDocumentCommand : ICommand
{
IDocument Document { get; set; }
}
public class  CreateDocumentCommand : Command, ICreateDocumentCommand
{
public CreateDocumentCommand(IDocument Document, ICommand Command) : base(Command.CorrelationId, Command.SocketId)
{
this.Document = Document;
}
public IDocument Document { get; set; }
}

APIGateway:

[HttpPost]
public async Task<IActionResult> Create([FromBody]CreateDocumentCommand documentCommand)
{
if (documentCommand == null)
{
return StatusCode(403);
}
return Json(documentCommand.Document.Id);
}

用例:

public class InventoryList : Document
{
public Guid WarehouseId { get; set; } = new Guid();
}
// Example document class
////////////////////////////////////////
// Example POST Request
ICommand command = new Command(messageId, socketId);
switch (item.GetType().Name)
{
case "InventoryList":
command = new CreateDocumentCommand((InventoryList)item, command);
break;
}
string result = await PostAsync($"{apiGatewayAddress}{item.GetType().BaseType.Name}/Create", command, accessToken);

我的开机自检发送功能:

public async Task<string> PostAsync<T>(string uri, T item, string authorizationToken = null, string authorizationMethod = "Bearer")
{
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(item, typeof(T), jsonSerializerSettings), System.Text.Encoding.UTF8, "application/json");
return await _client.SendAsync(requestMessage).Result.Content.ReadAsStringAsync();
}

如您所见,我已经在 JSON 序列化设置中包含TypeNameHandling.All,发送请求并调用 APIGateway 中的创建。但是,参数文档命令为 NULL。

我已经读过这个:Asp.Net 核心帖子从身体总是空

: ASP.NET 核心 MVC - 模型绑定:使用属性 [FromBody] 绑定接口模型 (BodyModelBinder(

这:用于 JSON.NET 反序列化的强制转换接口

尝试了各种魔术,创建新的构造函数,用[JSONConstructor]标记它们,仍然没有成功。我也尝试将APIGateway Cerate方法参数类型更改为ICreateDocumentCommand,再次得到一个空值。我一直在网上搜索一些模型绑定技巧,但是我找不到任何与 FromBody 绑定的东西。 我也找到了一些解决方案,包括 DI,但我正在寻找一个简单的解决方案。我希望我们能够找到一个:)

事实证明,将接口或内部带有接口的类作为 JSON 传递并不容易。我添加了一个自定义的 JSONConverter,它现在可以工作了!

最新更新