在web api get action调用中,readasasync()显示system.missingmethodexception:'找不到方法:'system.threading.tasks.task`1<! ! 0 GT; exeption [英] In web api get action call, readasasync() showing system.missingmethodexception: 'method not found: 'system.threading.tasks.task`1<! ! 0> exeption
问题描述
在我的项目中,我正在调用webapi服务 - 从另一个项目的控制器获取操作。我的代码是:
使用System.Net.Http;
使用System.Configuration;
使用System.Net.Http.Headers;
public ActionResult Index()
{
ViewBag.Place = new SelectList(GetPlaces(),ID,PlaceName);
FlightDetails flightDetails = new FlightDetails();
flightDetails.DepartureDate = System.DateTime.Now;
flightDetails.ReturnDate = System.DateTime.Now;
返回查看(flightDetails);
}
私人清单< tblPlace> GetPlaces()
{
List< tblPlace> lstPlaces = new List< tblPlace>(); //要取消注释
// lstPlaces = TwentyFour7Sewa.Utils.APIEngine< tblPlace> .GetAll(api / fromto);
using(var client = new HttpClient())
{
string apiHost = ConfigurationManager.AppSettings [ApiHost];
string apiDirectory =api / fromto;
List< tblPlace> result = new List< tblPlace>();
client.BaseAddress = new Uri(apiHost);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(application / json));
HttpResponseMessage response = client.GetAsync(apiDirectory).Result;
if(response.IsSuccessStatusCode)
{
result = response.Content.ReadAsAsync< List< tblPlace>>()。
返回结果;
}
其他
{
返回null;
}
}
// lstPlaces = lstPlaces.OrderBy(o => o.PlaceName).ToList< tblPlace>();
//返回lstPlaces;
}
然而,在行
ViewBag.Place = new SelectList(GetPlaces (),ID,PlaceName);
在Index()中的actionresult,我收到错误,
System.MissingMethodException:'找不到方法:'System.Threading。 Tasks.Task`1< !! 0 GT; System.Net.Http.HttpContentExtensions.ReadAsAsync(System.Net.Http.HttpContent)'。'
Web api返回所需的值。问题在于行
ReadAsAsync
。我在这部分代码上什么都没做。它一直工作到昨天。突然之间发生了什么,我不知道。
我尝试过的事情:
找到我需要使用Microsoft.Net.Http而不是System.Net.Http的地方,因为这已经过时了。当我在这个项目中从nuget包安装
Microsoft.Net.Http
时,这个dll没有显示在项目引用中。
该方法在System.Net.Http.Formatting
程序集中定义,该程序集通过NuGet分发。令人困惑的是,NuGet包名称是Microsoft.AspNet.WebApi.Client
:
NuGet Gallery | Microsoft.AspNet.WebApi.Client 5.2.7 [ ^ ]
注意:async
转换您的方法不应该调用.Result
:
public static async 任务< List< tblusertype>> GetAll()
{
string apiHost = ConfigurationManager.AppSettings [ ApiHost跨度>];
使用( var client = new HttpClient())
{
client.BaseAddress = new Uri(apiHost);
client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue( 应用/ JSON跨度>));
使用( var response = await client.GetAsync( api / UserType))
{
if (!response.IsSuccessStatusCode) return 空跨度>;
return await response.Content.ReadAsAsync< List< tblusertype>>();
}
}
}
另外:你正在使用HttpClient错误,这会破坏你的软件稳定性ASP.NET Monsters [ ^ 一>
In my project, I am calling webapi services - Get action from controller of another project. My code is:
using System.Net.Http; using System.Configuration; using System.Net.Http.Headers;
public ActionResult Index() { ViewBag.Place = new SelectList(GetPlaces(), "ID", "PlaceName"); FlightDetails flightDetails = new FlightDetails(); flightDetails.DepartureDate = System.DateTime.Now; flightDetails.ReturnDate = System.DateTime.Now; return View(flightDetails); }
private List<tblPlace> GetPlaces() { List<tblPlace> lstPlaces = new List<tblPlace>(); //to be uncommented //lstPlaces = TwentyFour7Sewa.Utils.APIEngine<tblPlace>.GetAll("api/fromto"); using (var client = new HttpClient()) { string apiHost = ConfigurationManager.AppSettings["ApiHost"]; string apiDirectory = "api/fromto"; List<tblPlace> result = new List<tblPlace>(); client.BaseAddress = new Uri(apiHost); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync(apiDirectory).Result; if (response.IsSuccessStatusCode) { result = response.Content.ReadAsAsync<List<tblPlace>>().Result; return result; } else { return null; } } //lstPlaces = lstPlaces.OrderBy(o => o.PlaceName).ToList<tblPlace>(); //return lstPlaces; }
However, in line
ViewBag.Place = new SelectList(GetPlaces(), "ID", "PlaceName");
in Index() actionresult, I am getting error,
System.MissingMethodException: 'Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Net.Http.HttpContentExtensions.ReadAsAsync(System.Net.Http.HttpContent)'.'
Web api is returning required value. The issue is with line
ReadAsAsync
. I did nothing on this part of code. It was working fine till yesterday. All of a sudden what happened, I have no clue.
What I have tried:
Found somewhere that I need to use Microsoft.Net.Http instead of System.Net.Http since this is obsolete. When I installed
Microsoft.Net.Http
from nuget package in this project, this dll was not shown in project reference.
That method is defined in theSystem.Net.Http.Formatting
assembly, which is distributed via NuGet. Confusingly, the NuGet package name isMicrosoft.AspNet.WebApi.Client
:
NuGet Gallery | Microsoft.AspNet.WebApi.Client 5.2.7[^]
NB: Theasync
conversion of your method should not call.Result
at all:
public static async Task<List<tblusertype>> GetAll() { string apiHost = ConfigurationManager.AppSettings["ApiHost"]; using (var client = new HttpClient()) { client.BaseAddress = new Uri(apiHost); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); using (var response = await client.GetAsync("api/UserType")) { if (!response.IsSuccessStatusCode) return null; return await response.Content.ReadAsAsync<List<tblusertype>>(); } } }
Also: You're using HttpClient wrong and it is destabilizing your software | ASP.NET Monsters[^]
这篇关于在web api get action调用中,readasasync()显示system.missingmethodexception:'找不到方法:'system.threading.tasks.task`1<! ! 0 GT; exeption的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!