簡體   English   中英

引用.NET標准類庫時的Azure函數(HttpTrigger)異常

[英]Azure function (HttpTrigger) exception when referencing a .NET standard class library

我正在使用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)

我可以在本地調試功能,並成功執行它。

然后,我添加了對新的 .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有關,或者缺少它。

您可能遇到與System.Net.Http和其他使用HttpRequest Extensions的庫之間發生沖突的類似問題。 看到這篇文章。

我嘗試將System.Net.Http上的版本降級到4.0.0,這是唯一允許我的應用正常運行的版本。 我仍然收到有關System.Net.Http版本要求沖突的警告,但似乎沒有任何影響(將討論保存為我鏈接的主題)。 一位發帖人提到,對他來說,4.3.1不是最新的4.3.2解決了這個問題。 其他人提到他們能夠完全刪除System.Net.Http,因為其他程序包已包含這些擴展。 就我而言,我無法刪除它,因為我正在使用SendGrid阻止這樣做。 可能想嘗試將其刪除,或者如果無法刪除,請降低版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM