By Checking the result,i know , if the service calls are made asynchronously (in parallel), the total response time will be slightly more than 8000 millisecond,
NOW ,MY QUESTION IS the Class for "WeatherService" How to implement ,1 think ,I also use the EAP to solve the problem,but now TAP is the recommended approach to asynchronous programming in the .NET Framework,how I can achieve by the TAP
blow is my source code
1:Synchronous situation
ABOUT Controller
public class DefaultController : Controller
{
public ActionResult IndexSynchronous(string city)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
NewsService newsService = new NewsService();
string[] headlines = newsService.GetHeadlines();
SportsService sportsService = new SportsService();
string[] scores = sportsService.GetScores();
WeatherService weatherService = new WeatherService();
string[] forecast = weatherService.GetForecast();
watch.Stop();
TimeSpan timeSpan = watch.Elapsed;
return Content(timeSpan.ToString());
}
}
ABOUT CLASS FOR SERVICE
public class NewsService
{
public string[] GetHeadlines()
{
Thread.Sleep(8000);
string[] strs = { "关于赵薇的童年", "关于王宝强的童年" };
return strs;
}
}
public class SportsService
{
public string[] GetScores()
{
Thread.Sleep(8000);
string[] strs = { "第一名分数为11", "第二名分数为22" };
return strs;
}
}
public class WeatherService
{
public string[] GetForecast()
{
Thread.Sleep(8000);
string[] strs = { "关于赵薇的童年", "关于王宝强的童年" };
return strs;
}
}
URL:http://localhost:51424/Default/IndexSynchronous
RESULT:00:00:24.0008085
The following example shows an asynchronous version of the news portal Index action method.
2:Asynchronous situation
ABOUT AsyncController
public class DefaultController : AsyncController
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
public void IndexAsync(string city)
{
watch.Start();
AsyncManager.OutstandingOperations.Increment(3);
NewsService newsService = new NewsService();
newsService.GetHeadlinesCompleted += (sender, e) =>
{
AsyncManager.Parameters["headlines"] = e.strs;
AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetHeadlinesAsync();
SportsService sportsService = new SportsService();
sportsService.GetScoresCompleted += (sender, e) =>
{
AsyncManager.Parameters["scores"] = e.strs;
AsyncManager.OutstandingOperations.Decrement();
};
sportsService.GetScoresAsync();
WeatherService weatherService = new WeatherService();
weatherService.GetForecastCompleted += (sender, e) =>
{
AsyncManager.Parameters["forecast"] = e.strs;
AsyncManager.OutstandingOperations.Decrement();
};
weatherService.GetForecastAsync();
}
public ActionResult IndexCompleted(string[] headlines, string[] scores, string[] forecast)
{
string stsHeadlines = "";
for (int i = 0; i < headlines.Length; i++)
{
stsHeadlines += headlines[i];
}
string stsScores = "";
for (int i = 0; i < scores.Length; i++)
{
stsScores += scores[i];
}
string stsForecast = "";
for (int i = 0; i < forecast.Length; i++)
{
stsForecast += forecast[i];
}
watch.Stop();
TimeSpan timeSpan = watch.Elapsed;
return Content(timeSpan.ToString() + stsHeadlines + stsScores + stsForecast);
}
ABOUT CLASS FOR SERVICE
public class NewsService
{
public string[] strs;
public delegate void GetHeadlinesCompletedEventHandler(object Sender, GetHeadlinesCompletedEventArgs e);
public event GetHeadlinesCompletedEventHandler GetHeadlinesCompleted;
public class GetHeadlinesCompletedEventArgs :EventArgs
{
public readonly string[] strs;
public GetHeadlinesCompletedEventArgs(string[] strs)
{
this.strs = strs;
}
}
protected virtual void OnGetHeadlinesCompleted(GetHeadlinesCompletedEventArgs e)
{
if (GetHeadlinesCompleted != null)
{
GetHeadlinesCompleted(this, e);
}
}
public async void GetHeadlinesAsync()
{
string[] result = await GetValueAsync();
GetHeadlinesCompletedEventArgs e = new GetHeadlinesCompletedEventArgs(result);
OnGetHeadlinesCompleted(e);
}
public Task<string[]> GetValueAsync( )
{
return Task.Run(() =>
{
Thread.Sleep(8000);
string[] strss = { "关于赵薇的童年", "关于王宝强的童年" };
return strss;
});
}
}
public class SportsService
{
public string[] strs;
public delegate void GetScoresCompletedEventHandler(object Sender, GetScoresCompletedEventArgs e);
public event GetScoresCompletedEventHandler GetScoresCompleted;
public class GetScoresCompletedEventArgs : EventArgs
{
public readonly string[] strs;
public GetScoresCompletedEventArgs(string[] strs)
{
this.strs = strs;
}
}
protected virtual void OnGetScoresCompleted(GetScoresCompletedEventArgs e)
{
if (GetScoresCompleted != null)
{
GetScoresCompleted(this, e);
}
}
public async void GetScoresAsync()
{
string[] result = await GetValueAsync();
GetScoresCompletedEventArgs e = new GetScoresCompletedEventArgs(result);
OnGetScoresCompleted(e);
}
public Task<string[]> GetValueAsync()
{
return Task.Run(() =>
{
Thread.Sleep(8000);
string[] strs = { "第一名分数为11", "第二名分数为22" };
return strs;
});
}
}
public class WeatherService
{
public string[] strs;
public delegate void GetForecastCompletedEventHandler(object Sender, GetForecastCompletedEventArgs e);
public event GetForecastCompletedEventHandler GetForecastCompleted;
public class GetForecastCompletedEventArgs : EventArgs
{
public readonly string[] strs;
public GetForecastCompletedEventArgs(string[] strs)
{
this.strs = strs;
}
}
protected virtual void OnGetForecastCompleted(GetForecastCompletedEventArgs e)
{
if (GetForecastCompleted != null)
{
GetForecastCompleted(this, e);
}
}
public async void GetForecastAsync()
{
string[] result = await GetValueAsync();
GetForecastCompletedEventArgs e = new GetForecastCompletedEventArgs(result);
OnGetForecastCompleted(e);
}
public Task<string[]> GetValueAsync()
{
return Task.Run(() =>
{
Thread.Sleep(8000);
string[] strs = { "关于赵薇的童年", "关于王宝强的童年" };
return strs;
});
}
}
URL:http://localhost:51424/Default/Index
RESULT:
00:00:08.0519411
What I have tried:
var newsService = new NewsService();
var sportsService = new SportsService();
return View("Common",
new PortalViewModel {
NewsHeadlines = await newsService.GetHeadlinesAsync(),
SportsScores = await sportsService.GetScoresAsync()
});