I made a SharePoint 2013 WebPart in VisualStudio 2017 and I build a RESTapi as a console application with this tutorial: Calling a Web API From a .NET Client (C#)
In my WebPart I create an object from the Restapi class to use it.
RestAPI restApi = new RestAPI();
In the constructor from the RestAPI class i call the
RunAsync().Wait();
In this method i'm calling an other method.
and now my Problem: I need to get the authentication ticket so i'm using this method:
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;
as an console application this works fine.
this is the AuthResponse class:
[JsonObject]
public class AuthResponse
{
[JsonProperty("Ticket")]
public string Ticket { get; set; }
}
I got the classes to format the JSON with http://json2csharp.com/
but when I use this in a SharePoint Webpart I get the following exception:
{"Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.":"System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"} System.Exception {System.IO.FileNotFoundException}
in the .csproj file the following entry can be found:
<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>
the formatting.dll can be found in the path. the problem is that System.Net.Http.Formatting could not be found. the issue is in the await response.Content.ReadAsAsync<AuthResponse>()
i tried to use:
JsonConvert.DeserializeObject<AuthResponse>(jsonString);
but then i get nearly the same exception:
{"Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.":"Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"} System.Exception {System.IO.FileNotFoundException}
.csproj entry:
<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 is located in the HintPath
the app.config looks like this:
<?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>
all the references are up to date and working when i tested it as a console application.
I'm using .NET v4.5 in the programm and in the SharePoint Server. when i not catch the exception then i get this:
An exception of type 'System.AggregateException' occurred in mscorlib.dll but was not handled in user code Additional information: One or more errors occurred. occurred
i tried to reinstall the references but that did not work so how can i get the references to work in the SharePoint Webpart?
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.