使用OneDrive API的Azure函数返回编译错误

本文关键字:返回 编译 错误 函数 Azure OneDrive API 使用 | 更新日期: 2023-09-27 18:14:14

我想创建一个Azure函数(c# API通用HTTP)方法,用于将文件上传到Office365 Sharepoint文档库。
因为OneDrive API允许我上传大文件(使用守护进程&),我已经用一个c#控制台应用程序成功地实现了这个目标。现在的想法是将代码移动到Azure函数中。但是,我在保存/编译过程中收到一个错误。

代码:

#r "Newtonsoft.Json"
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
public class DriveResult
{
    [JsonProperty("@odata.id")]
    public string id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
}
const string appId = "<myAppId>";
const string tenantName = "<tenantName>";
const string tenantId = "<myTenantId>";

private static string GetResourceUrl()
{
    return "https://" + tenantName + ".sharepoint.com";
}
private static string GetAuthority()
{
    return "https://login.windows.net/" + tenantId + "/oauth2/authorize";
}
public static async Task<bool> Run(HttpRequestMessage req, TraceWriter log)
{
    var token = await GetAccessTokenAsync();
    return true; //temporary code
}
private static async Task<string> GetAccessTokenAsync()
{
    AuthenticationContext authenticationContext = new AuthenticationContext(GetAuthority(), false);
    string certfile = @"mykeyfile.pfx";
    X509Certificate2 cert = new X509Certificate2(certfile, "<myinsanepwd>");
    ClientAssertionCertificate cac = new ClientAssertionCertificate(appId, cert);
    var authenticationResult = await authenticationContext.AcquireTokenAsync("GetResourceUrl()", cac);
    return authenticationResult.AccessToken;
}
Project.json
{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.IdentityModel.Clients.ActiveDirectory": "3.13.5",
          "System.Security.Cryptography.X509Certificates": "4.1.0"
      }
    }
   }
}
Function.json
{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

编译错误:

2016-10-22T13:05:24.625 run.csx(50,60): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
2016-10-22T13:05:24.625 run.csx(50,38): error CS0012: The type 'Task<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

为什么它在抱怨?这些函数运行在。net 4.6。

最诚挚的问候,Jens

使用OneDrive API的Azure函数返回编译错误

作为替代方案,如果您想从OneDrive或实际上任何其他基于文件的SaaS提供商(如DropBox, GoogleDrive, Box,…您可能想尝试SaaS文件触发器,在Azure函数中输入或输出。

下面是一个c#示例: https://github.com/Azure/azure-webjobs-sdk-templates/tree/dev/Templates/SaaSFileTrigger-CSharp

Jens,

这是由一些案例在引用pcl时触发的问题。

我们有一个更永久的解决方案的计划,但与此同时,你应该能够通过向你需要的程序集添加显式引用来编译你的函数,就像你在JSON.NET中所做的那样。所以在你的例子中,下面是:

#r "System.Runtime"
#r "System.Threading.Tasks"

希望这对你有帮助!