【问题标题】:Azure function (HttpTrigger) exception when referencing a .NET standard class library引用 .NET 标准类库时出现 Azure 函数 (HttpTrigger) 异常
【发布时间】:2017-09-06 00:47:55
【问题描述】:

我正在使用 WebJob SDK 在 Visual Studio 2017 中创建 Azure 函数(特别是传出的 Slack WebHook)。

public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,
        TraceWriter log)

我可以在本地调试函数,并成功执行。

然后我添加对new .NET 标准类库的引用,编译和调试,并在触发该函数时出现以下错误。

{
  "id": "2badc751-bd37-4482-90db-6dde1247110c",
  "requestId": "60acb725-546e-4e62-9868-a5bd4c217bfc",
  "statusCode": 500,
  "errorCode": 0,
  "message": "Exception while executing function: SlackWebHooks -> Exception binding parameter 'req' -> No MediaTypeFormatter is available to read an object of type 'HttpRequestMessage' from content with media type 'application/x-www-form-urlencoded'.",
  "errorDetails": "Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: SlackWebHooks ---> System.InvalidOperationException : Exception binding parameter 'req' ---> System.Net.Http.UnsupportedMediaTypeException : No MediaTypeFormatter is available to read an object of type 'HttpRequestMessage' from content with media type 'application/x-www-form-urlencoded'.\r\n   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content,Type type,IEnumerable`1 formatters,IFormatterLogger formatterLogger,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Extensions.Http.HttpTriggerAttributeBindingProvider.HttpTriggerBinding.CreateUserTypeValueProvider(HttpRequestMessage request,String invokeString)\r\n   at async Microsoft.Azure.WebJobs.Extensions.Http.HttpTriggerAttributeBindingProvider.HttpTriggerBinding.BindAsync(Object value,ValueBindingContext context)\r\n   at async Microsoft.Azure.WebJobs.Host.Triggers.TriggeredFunctionBinding`1.BindCoreAsync[TTriggerValue](ValueBindingContext context,Object value,IDictionary`2 parameters) \r\n   End of inner exception\r\n   at Microsoft.Azure.WebJobs.Host.Executors.DelayedException.Throw()\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,ParameterHelper parameterHelper,TraceWriter traceWriter,ILogger logger,CancellationTokenSource functionCancellationTokenSource)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??) \r\n   End of inner exception\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance functionInstance,CancellationToken cancellationToken)\r\n   at Microsoft.Azure.WebJobs.Host.Executors.ExceptionDispatchInfoDelayedException.Throw()\r\n   at async Microsoft.Azure.WebJobs.JobHost.CallAsync(??)\r\n   at async Microsoft.Azure.WebJobs.Script.ScriptHost.CallAsync(String method,Dictionary`2 arguments,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostManager.HandleRequestAsync(FunctionDescriptor function,HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ProcessRequestAsync(HttpRequestMessage request,FunctionDescriptor function,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.<>c__DisplayClass3_0.<ExecuteAsync>b__0(??)\r\n   at async Microsoft.Azure.WebJobs.Extensions.Http.HttpRequestManager.ProcessRequestAsync(HttpRequestMessage request,Func`3 processRequestHandler,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ExecuteAsync(HttpControllerContext controllerContext,CancellationToken cancellationToken)\r\n   at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.SystemTraceHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.WebScriptHostHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async System.Web.Http.HttpServer.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)"
}

我不确定为什么引用 .NET 标准库会改变 SDK 的行为?但是很明显和MediaTypeFormatter有关,或者说没有。

【问题讨论】:

  • 当我使用await req.Content.ReadAsAsync&lt;object&gt;(); 在我的HttpTrigger 函数应用程序中获取请求正文时遇到了类似的问题,而请求标头的Content-Typeapplication/x-www-form-urlencoded

标签: c# azure azure-functions


【解决方案1】:

您可能会遇到与我在 System.Net.Http 和使用 HttpRequest 扩展的其他库之间发生冲突时遇到的类似问题。看到这个post.

我尝试将 System.Net.Http 上的版本一直降级到 4.0.0,这是唯一允许我的应用程序运行的版本。我仍然收到有关 System.Net.Http 版本要求冲突的警告,但它似乎没有影响任何事情(为我链接的主题保存该讨论)。一位发帖者提到,对他来说,4.3.1 不是最新的 4.3.2 解决了这个问题。其他人提到他们能够完全删除 System.Net.Http,因为其他包包含扩展。就我而言,我无法删除它,因为我正在使用 SendGrid 阻止我这样做。可能想尝试删除它,或者如果不能,请降低版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多