2017-05-23 37 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

相关问题