繁体   English   中英

SharePoint WebPart RestAPI异常无法加载文件或程序集

[英]SharePoint WebPart RestAPI exception Could not load file or assembly

我在Visual Studio 2017中制作了SharePoint 2013 WebPart,并通过本教程构建了RESTapi作为控制台应用程序: 从.NET客户端(C#)调用Web API

在我的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'或其依赖项之一。系统找不到指定的文件。”:“系统。 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<AuthResponse>()我尝试使用the await response.Content.ReadAsAsync<AuthResponse>()

JsonConvert.DeserializeObject<AuthResponse>(jsonString);

但是然后我得到几乎相同的异常:

{“无法加载文件或程序集'Newtonsoft.Json,版本= 1.0.0.0.0,文化=中性,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。 当我没有捕捉到异常时,我得到这个:

mscorlib.dll中发生类型'System.AggregateException'的异常,但未在用户代码中处理。其他信息:发生一个或多个错误。 发生了

我试图重新安装引用,但是没有用,所以如何在SharePoint Webpart中使引用起作用?

谢谢

在Webpart解决方案中,有一个程序包,在该程序包内,您需要将要用来将它们添加到完全信任状态的所有程序集(在“高级”选项卡中)放入。 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM