【问题标题】:SharePoint WebPart RestAPI exception Could not load file or assemblySharePoint WebPart RestAPI 异常 无法加载文件或程序集
【发布时间】:2017-05-23 08:21:28
【问题描述】:

我在 VisualStudio 2017 中创建了一个 SharePoint 2013 WebPart,并使用本教程构建了一个 RESTapi 作为控制台应用程序: Calling a Web API From a .NET Client (C#)

在我的 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; }
}

我得到了用http://json2csharp.com/格式化JSON的类

但是当我在 SharePoint Webpart 中使用它时,会出现以下异常:

{"无法加载文件或程序集'System.Net.Http.Formatting, 版本=5.2.3.0,文化=中性,PublicKeyToken=31bf3856ad364e35' 或 它的依赖项之一。系统找不到文件 指定。":"System.Net.Http.Formatting,版本=5.2.3.0, 文化=中立,PublicKeyToken=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&lt;AuthResponse&gt;() 我尝试使用:

JsonConvert.DeserializeObject<AuthResponse>(jsonString);

但我得到了几乎相同的异常:

{"无法加载文件或程序集'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' 或其之一 依赖关系。系统找不到文件 指定。":"Newtonsoft.Json,版本=10.0.0.0,文化=中性, PublicKeyToken=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 中工作的引用?

谢谢

【问题讨论】:

    标签: c# json asynchronous sharepoint


    【解决方案1】:

    在您的 webpart 解决方案中有一个包,您需要在包内放置您要使用的所有程序集(在“高级”选项卡中)以将它们添加到完全信任。

    【讨论】:

    • 哇太棒了!那很简单。谢谢。
    猜你喜欢
    • 2010-11-17
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多