【问题标题】:Cant run HttpTrigger Azure Function from another azure function无法从另一个天蓝色函数运行 HttpTrigger Azure 函数
【发布时间】:2018-07-02 12:03:42
【问题描述】:

这里是function.json sn-p

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "query",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]

这是我的 run.csx 文件:

public class Query{
    public double AdvanceDays{get;set;}
}
public static async Task<HttpResponseMessage> Run(Query query,entity Entity,HttpRequestMessage req, TraceWriter log, IEnumerable<dynamic> inputDocument)
{

现在我需要从另一个函数调用这个 Http 触发函数。我在做:

var url = "https://azurefunction...";
var content2 = new StringContent(string.Format("{{\"AdvanceDays\":7}}"));
var response = await client.PostAsync(url,content2);

但我收到以下错误: No MediaTypeFormatter is available to read an object of type 'Query' from content with media type 'text/plain'.

我该如何解决这个问题?请帮忙!!!

【问题讨论】:

    标签: c# azure http-post azure-functions


    【解决方案1】:

    看起来问题在于您发送的请求未指定其内容类型,并且默认为text/plain。由于没有Content-type header,因此该函数不知道您发送的是哪种类型的内容。你应该添加一个Content-Type header,其值为application/json

    Accept 请求头字段可用于指定响应可接受的某些媒体类型。

    Content-Type entity-header 字段指示发送给接收者的实体主体的媒体类型,或者在 HEAD 方法的情况下,本应发送的媒体类型具有请求是 GET。

    来源:Accept header vs Content-Type Header

    【讨论】:

    • 虽然它现在正在工作,但我遇到了一个非常奇怪的错误。当我从 Func2 调用 Func1 时,Func1 会正确执行并返回成功。但在 Func1 中,我得到以下日志
    • 2018-07-02T13:40:00.240 [Info] Function started (Id=f68ea59d-5988-4fda-939b-4bc4195e4ad4) 2018-07-02T13:40:00.475 [Info] C# HTTP trigger function processed a request. 2018-07-02T13:41:40.524 [Error] Exception while executing function:Functions.HttpTriggerCSharp3. mscorlib: Exception has been thrown by the target of an invocation. mscorlib: One or more errors occurred. A task was canceled. 2018-07-02T13:41:40.571 [Error] Function completed (Failure, Id=f68ea59d-5988-4fda-939b-4bc4195e4ad4, Duration=100325ms)
    • 这可能需要一个好的调试会话。请检查该功能,尝试调试它,如果您无法修复它,请创建一个新问题。您是否使用了任何可能起作用的外部参考?
    • 所以调用函数和被调用函数都单独运行顺利。我只是从调用函数调用被调用函数。 Called Func 在日志中显示成功。由于调用函数除了调用这个函数什么都不做,我不明白为什么调用函数会显示这个错误?两者都是 Timer Triggered Azure Functions 顺便说一句。是不是因为调用函数是asyncpublic static async Task&lt;HttpResponseMessage&gt; Run(HttpRequestMessage req, TraceWriter log)
    • 检查传入的数据,以及是否符合预期格式。我可以想象一下:string.Format("{{\"AdvanceDays\":7}}") 给出的结果与您的预期不同。
    猜你喜欢
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多