Azure Function 不反序列化对象

Azure Function not deserializing object

在最新版本的 Azure Functions 中,我无法获得反序列化对象的函数。

版本:

Azure 函数核心工具:1.0.0-beta.100

函数运行时版本:1.0.11015.0

Visual Studio: 15.3.0 预览版 4.0

示例(不工作):

[FunctionName("Login")]
public static async Task<HttpResponseMessage> Login([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req, ILogger log, AuthenticatingUser user)
{
    return await Task.Run(() =>
    {
        return req.CreateResponse(HttpStatusCode.OK, user.Username);
    });
}

当尝试使用 Postman 来 post 这个函数时,我总是得到以下错误:

Exception while executing function: Functions.Login. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'user'. Microsoft.Azure.WebJobs.Host: No value was provided for parameter 'user'.

我已经尝试 post 使用表单数据、x-www-form-urlencoded 和原始数据(选择 JSON - 我期望的工作)。所有人都以同样的错误结束。

当我从函数中取出用户时,我可以访问通过以下方式传入的 JSON:

var content = req.Content;
string jsonContent = content.ReadAsStringAsync().Result;

在以前版本的 Azure Functions 中(值得注意的是 不是 使用 Visual Studio 扩展构建,涉及 JSON 文件等)这按预期工作。

我正在 post 阅读以下内容的各种版本:

{
    "Username": "myUserName",
    "Password": "MyPassword123"
}

用户模型:

public class AuthenticatingUser
{
    public string Username { get; set; }
    public string Password { get; set; }
}

您应该使用 HttpTrigger 属性标记 user 参数:

public static async Task<HttpResponseMessage> Login(
    HttpRequestMessage req, 
    [HttpTrigger(AuthorizationLevel.Function, "post")] AuthenticatingUser user)

req 仍将在没有任何属性的情况下填充。

确保您的请求 Content-Type header 设置为 application/json。鉴于此,该函数将在请求 body.

中与您的 JSON 一起使用