2016-10-22 26 views
1

我想創建一個將文件上載到Office365 Sharepoint文檔庫的Azure函數(C#API通用HTTP)方法。
因爲OneDrive API可以讓我上傳大文件(使用守護進程&證書身份驗證),我已經成功地實現了用C#控制檯應用程序的目標。這個想法現在將移動到Azure功能的代碼。但是,在保存/編譯期間收到錯誤消息。Azure消耗OneDrive API的函數返回編譯錯誤

代碼:編譯期間

#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上運行。

最好的問候, 延

回答

1

延,

這是引用PCLS當一些箱子引發的問題。

我們有一個更持久的解決方案計劃,但在此期間,你應該能夠通過添加明確你需要爲你與JSON.NET完成的程序集的引用來編譯功能。所以在你的情況下,以下內容:

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

希望這有助於!

+0

嗨法比奧, 這確實解決了編譯錯誤。然而, – Jens

+0

行 X509Certificate2 cert = new X509Certificate2(certfile,「」); 引發異常System.Security.Cryptography.CryptographicException:系統找不到指定的文件。 真是奇怪,因爲該文件指定的路徑上存在(我查使用File.Exists()) 附: 我注意到一個藍色的功能的當前目錄設置爲d:\ Windows \ System32下 – Jens

+0

你可以嘗試通過一個絕對路徑(而不僅僅是文件名)製作的,如下:System.IO.Path.Combine(環境.ExpandEnvironmentVariables(「%HOME%」),@「site \ wwwroot \ \ mykeyfile.pfx」); 我在GitHub上打開了一個問題,所以我們可以開發一個更方便的方法來獲取函數目錄。 –