2017-05-23 39 views
0

我的VisualStudio 2017年提出一個SharePoint 2013的WebPart和我建立一個RESTAPI與本教程一個控制檯應用程序: Calling a Web API From a .NET Client (C#)的SharePoint的WebPart RESTAPI例外無法加載文件或程序集

在我的WebPart我從創建一個對象Restapi類使用它。

RestAPI restApi = new RestAPI(); 

從RESTAPI類的構造函數我稱之爲

RunAsync().Wait(); 

在這種方法我打電話的另一種方法。

,現在我的問題: 我需要得到驗證票證所以我用這個方法:

HttpContent content = new StringContent("username=" + lgname + ";password=" + pswd, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"); 
HttpResponseMessage response = await client.PostAsync($"/OTCS/cs.exe/api/v1/auth", content); 
response.EnsureSuccessStatusCode(); 
var authResponse = await response.Content.ReadAsAsync<AuthResponse>(); 
return authResponse.Ticket; 

作爲一個控制檯應用程序能正常工作。

這是AuthResponse類:

[JsonObject] 

public class AuthResponse 
{ 
    [JsonProperty("Ticket")] 
    public string Ticket { get; set; } 
} 

我得到了類的JSON與http://json2csharp.com/

格式,但是當我使用這個在SharePoint的WebPart我得到以下異常:

{「無法加載文件或程序集'System.Net.Http.Formatting, Version = 5.2.3.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或 它的一個依賴關係。該系統找不到指定的文件 「:」 System.Net.Http.Formatting,版本= 5.2.3.0, 文化=中性公鑰= 31bf3856ad364e35" } System.Exception的{} System.IO.FileNotFoundException

在.csproj的文件

以下條目,可以發現:

<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> 
     <HintPath>packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath> 
    </Reference> 

的formatting.dll可以在路徑中找到 問題是System.Net.Http.Formatting找不到。 問題出在the await response.Content.ReadAsAsync<AuthResponse>() 我試過了o使用:

JsonConvert.DeserializeObject<AuthResponse>(jsonString); 

但後來我得到幾乎相同的異常:

{「無法加載文件或程序集Newtonsoft.Json,版本= 10.0.0.0, 文化=中性公鑰= 30ad4fe6b2a6aeed'或其 依賴項之一。該系統找不到指定的文件 「:」 Newtonsoft.Json,版本= 10.0.0.0,文化=中立, 公鑰= 30ad4fe6b2a6aeed「} System.Exception的 {} System.IO.FileNotFoundException

。 csproj條目:

<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> 
     <HintPath>packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> 
    </Reference> 

Newtonsoft.Json。DLL位於HintPath

在app.config看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
</configuration> 

所有引用是最新的,當我測試它作爲控制檯應用程序工作。

我在程序和SharePoint Server中使用.NET v4.5。 當我不捕獲異常,然後我得到這個:

型「System.AggregateException」的異常出現在mscorlib.dll的 但在用戶代碼的其他信息沒有處理:發生 一個或多個錯誤。發生

我試圖重新安裝引用,但沒有工作 所以我如何獲得在SharePoint Webpart中工作的引用?

感謝

回答

1

在您的網頁組件的解決方案有一個包,你需要把所有的組件(在高級選項卡)的包裝內,你要使用將它們添加到完全信任。 enter image description here

+0

太好了!那很簡單。謝謝。 – Hissatsu

相關問題