C# 如何从Azure函数中进行web服务调用(对认知服务)?

C# 如何从Azure函数中进行web服务调用(对认知服务)?,c#,azure,azure-functions,microsoft-cognitive,azure-blob-storage,C#,Azure,Azure Functions,Microsoft Cognitive,Azure Blob Storage,我一直在尝试从Azure函数中调用认知服务,但运气不太好。我有一个Blob触发函数,当一个对象被添加到Blob时开始。我想在同一个函数中向另一个blob容器输出一些东西 我通过以下方式实现了这一点 创建了具有以下绑定的函数应用(Blob触发): "bindings": [ { "name": "myBlob", "type": "blobTrigger", "direction": "in", "path": "originals/{name}", "connection":

我一直在尝试从Azure函数中调用认知服务,但运气不太好。我有一个Blob触发函数,当一个对象被添加到Blob时开始。我想在同一个函数中向另一个blob容器输出一些东西

我通过以下方式实现了这一点

  • 创建了具有以下绑定的函数应用(Blob触发):

    "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "originals/{name}",
      "connection": "AzureWebJobsStorage"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "extracted/processed-{name}.json",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    }
    ], "disabled": false
    
  • 使用了以下代码:

    #r "Newtonsoft.Json"
    
    using System;
    using System.Text;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using Newtonsoft.Json;
    
    
    public static async Task Run(Stream myBlob, Stream outputBlob, TraceWriter log)
    {
     string subscriptionKey = "YOUR KEY HERE";
     string uriBase = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/recognizeText";
    
     int ID = 0;
     String outputJSON = "json string to be written onto blob";
     HttpClient client = new HttpClient();
     client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
    
     string requestParameters = "handwriting=true";
     string uri = uriBase + "?" + requestParameters;
     HttpResponseMessage response = null;
     string operationLocation = null;
     HttpContent content = new StreamContent(myBlob);
     content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
     response = await client.PostAsync(uri, content);
    
     if (response.IsSuccessStatusCode)
        operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault();
     else
     {
        log.Info("\nError:\n");
        log.Info((await response.Content.ReadAsStringAsync()));
        return;
      }
    
      string contentString;
      ...
      ...
      System.Threading.Thread.Sleep(1000);
      response = await client.GetAsync(operationLocation);
      contentString = await response.Content.ReadAsStringAsync();
    
      RootObject jObj = JsonConvert.DeserializeObject<RootObject>(contentString);
    
      log.Info(outputJSON); // at this point outputjson has the info needed to write onto the new blob
    
      byte[] data = Encoding.ASCII.GetBytes(outputJSON);
      outputBlob.Write(data,0,data.Length);
    }
    
    #r“Newtonsoft.Json”
    使用制度;
    使用系统文本;
    使用System.Net.Http;
    使用System.Net.Http.Header;
    使用Newtonsoft.Json;
    公共静态异步任务运行(流myBlob、流outputBlob、TraceWriter日志)
    {
    string subscriptionKey=“此处为您的密钥”;
    字符串uriBase=”https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/recognizeText";
    int ID=0;
    String outputJSON=“要写入blob的json字符串”;
    HttpClient=新的HttpClient();
    client.DefaultRequestHeaders.Add(“Ocp Apim订阅密钥”,subscriptionKey);
    string requestParameters=“手写=true”;
    字符串uri=uriBase+“?”+requestParameters;
    HttpResponseMessage响应=null;
    字符串操作位置=null;
    HttpContent=新的StreamContent(myBlob);
    content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
    response=wait client.PostAsync(uri、内容);
    if(响应。IsSuccessStatusCode)
    operationLocation=response.Headers.GetValues(“操作位置”).FirstOrDefault();
    其他的
    {
    log.Info(“\n错误:\n”);
    log.Info((wait response.Content.ReadAsStringAsync());
    返回;
    }
    字符串contentString;
    ...
    ...
    系统线程线程睡眠(1000);
    response=wait client.GetAsync(operationLocation);
    contentString=wait response.Content.ReadAsStringAsync();
    RootObject jObj=JsonConvert.DeserializeObject(contentString);
    log.Info(outputJSON);//此时,outputJSON具有写入新blob所需的信息
    byte[]data=Encoding.ASCII.GetBytes(outputJSON);
    outputBlob.Write(数据,0,数据长度);
    }
    

  • 我希望这有帮助。干杯!:)

    太好了,你可以接受你的答案来结束这个问题。当然可以。根据SO规定,我只能等到明天。:)