请参阅以下调用外部api的代码并返回结果。然后在剃须刀视图中使用它:
public async Task<HttpResponseMessage> GetEventsForCalendar(string category, int page = 1)
{
Func<Task<HttpResponseMessage>> execute = async ()
=>
{
using (var client = ServiceLocator.Current.GetInstance<AdministrateHttpClient>())
{
var requestUrl = _config.BaseCrmUrl +
string.Format(
"events/events?_page={0}&course.categories.id__in={1}&_display=id,title,prices,default_price_id,code,sold_out,delegates_max,places_reserved,tax_type,start,status,end,sessions,delegates_booked,location_object,location&_order=start&is_cancelled=false&start__gt={2}&type__ni=session&is_iht=false",
page, category, DateTime.Today.ToString("yyyy-MM-dd:THH:mm:ssZ"));
var result = await
client.GetLoggedAsync(requestUrl);
return result;
}
};
return await ExecFunc(execute);
}
目前正在接受约我从控制器调用时加载30-60秒。
有没有什么方法可以加快速度? 如果您需要更多代码来了解此功能的作用,请告诉我。
编辑 - 添加了ExecFunc
public TResult ExecFunc<TResult>(Func<TResult> func)
{
try
{
return func();
}
catch (Exception exception)
{
_logger.Exception(exception);
throw;
}
}
编辑2 - 添加了GetLoggedAsync
public async Task<HttpResponseMessage> GetLoggedAsync(string requestUri, [CallerMemberName] string caller = "")
{
var requestId = Guid.NewGuid();
_logger.Info(GetOutgoingLogText(requestUri, "GET", string.Empty, requestId, caller));
var result = await GetAsync(requestUri);
var content = await result.Content.ReadAsStringAsync();
_logger.Info(GetReturnLogText(result, "GET", content, requestId, caller));
return result;
}
编辑3 - 电话链: 初始调用(在控制器中) - finallist只是一个文本字符串:
try
{
model.Events = await _eventService.GetEventsForCalendar(finallist);
}
catch (Exception ex)
{
model.Events = new StaticPagedList<Event>(Enumerable.Empty<Event>(), 1, 1, 0);
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
任务:
Task<IPagedList<Event>> GetEventsForCalendar(string category, int page = 1);
任务:
public async Task<IPagedList<Event>> GetEventsForCalendar(string category, int page = 1)
{
var response = await _eventRepository.GetEventsForCalendar(category, page);
var result = await response.ReadPagedJsonData<Event>();
return result;
}
任务:
Task<HttpResponseMessage> GetEventsForCalendar(string category, int page = 1);
这导致上面的初始脚本