【问题标题】:Azure Function consuming OneDrive API returns compile errors使用 OneDrive API 的 Azure 函数返回编译错误
【发布时间】:2016-10-22 13:19:29
【问题描述】:

我想创建一个将文件上传到 Office365 Sharepoint 文档库的 Azure 函数(C# API 通用 HTTP)方法。
因为 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 上运行。

最好的问候, 延斯

【问题讨论】:

    标签: c# onedrive azure-functions


    【解决方案1】:

    作为替代方案,如果您想从 OneDrive 或任何其他基于文件的 SaaS 提供商(例如 DropBox、GoogleDrive、Box 等)读取/写入数据,您可能想尝试 SaaS 文件触发器、输入或在 Azure 函数中输出。

    这是一个 C# 示例:

    https://github.com/Azure/azure-webjobs-sdk-templates/tree/dev/Templates/SaaSFileTrigger-CSharp

    【讨论】:

      【解决方案2】:

      詹斯,

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

      我们有一个更持久的解决方案的计划,但与此同时,您应该能够通过显式添加对所需程序集的引用来编译您的函数,就像您使用 JSON.NET 所做的那样。因此,在您的情况下,以下内容:

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

      希望这会有所帮助!

      【讨论】:

      • 嗨法比奥,这确实解决了编译错误。然而,
      • 行 X509Certificate2 cert = new X509Certificate2(certfile, "");抛出异常 System.Security.Cryptography.CryptographicException:系统找不到指定的文件。真的很奇怪,因为该文件存在于指定路径上(我使用 File.Exists() 检查过)我注意到 Azure Function 的当前目录设置为 D:\Windows\system32
      • 你能尝试传递一个绝对路径(而不仅仅是文件名),如下所示:System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%HOME%"), @"站点\wwwroot\\mykeyfile.pfx");我在 GitHub 上打开了一个问题,以便我们可以公开一种更方便的方法来获取函数目录。
      • 另外,作为直接使用 OneDrive 的替代方法,您可能还需要考虑 Hamid 的回答。
      • 嗨法比奥,我尝试了你的解决方案,但它给了我同样的错误。 :( 我想要一个自定义端点而不是 Hamid 的答案。(当然这也很棒。)但是文件被正确检索(File.Exists() 证明了这一点)它可能与@987654321 有关@ ?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多