Azure Function consuming OneDrive API returns 编译错误
Azure Function consuming OneDrive API returns compile errors
我想创建一个将文件上传到 Office365 Sharepoint 文档库的 Azure 函数(C# API 通用 HTTP)方法。
因为 OneDrive API 允许我上传大文件(使用守护进程和证书验证),我已经成功地使用 C# 控制台应用程序实现了目标。现在的想法是将代码移动到 Azure 函数中。但是,我在 save/compilation.
期间收到错误消息
代码:
#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 上运行。
此致,
詹斯
延斯,
这是引用 PCL 时 一些 案例触发的问题。
我们有一个更永久的解决方案的计划,但与此同时,您应该能够通过显式添加对所需程序集的引用来编译您的函数,就像您对 JSON.NET 所做的那样。因此,在您的情况下,以下内容:
#r "System.Runtime"
#r "System.Threading.Tasks"
希望对您有所帮助!
作为替代方案,如果您想要 read/write from/to OneDrive 或实际上任何其他基于文件的 SaaS 提供商,例如 DropBox、GoogleDrive、Box,...您可能想尝试Azure 函数中的 SaaS 文件触发器、输入或输出。
这是一个 C# 示例:
https://github.com/Azure/azure-webjobs-sdk-templates/tree/dev/Templates/SaaSFileTrigger-CSharp
我想创建一个将文件上传到 Office365 Sharepoint 文档库的 Azure 函数(C# API 通用 HTTP)方法。
因为 OneDrive API 允许我上传大文件(使用守护进程和证书验证),我已经成功地使用 C# 控制台应用程序实现了目标。现在的想法是将代码移动到 Azure 函数中。但是,我在 save/compilation.
代码:
#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 上运行。
此致, 詹斯
延斯,
这是引用 PCL 时 一些 案例触发的问题。
我们有一个更永久的解决方案的计划,但与此同时,您应该能够通过显式添加对所需程序集的引用来编译您的函数,就像您对 JSON.NET 所做的那样。因此,在您的情况下,以下内容:
#r "System.Runtime"
#r "System.Threading.Tasks"
希望对您有所帮助!
作为替代方案,如果您想要 read/write from/to OneDrive 或实际上任何其他基于文件的 SaaS 提供商,例如 DropBox、GoogleDrive、Box,...您可能想尝试Azure 函数中的 SaaS 文件触发器、输入或输出。
这是一个 C# 示例:
https://github.com/Azure/azure-webjobs-sdk-templates/tree/dev/Templates/SaaSFileTrigger-CSharp