Wes*_*ley 1 c# asynchronous async-await
我有一个云应用程序,它使用优秀的WURFL库进行设备检测.
根据访问网站的设备类型,我们加载不同的资源.
直到最近,我一直在我的云应用程序中嵌入该服务.问题在于它为每个调试例程增加了3-5分钟,因为该服务支持检测数千个设备..NET团队推荐的模式是将服务加载到缓存中:
var wurflDataFile = HttpContext.Current.Server.MapPath(WurflDataFilePath);
var configurer = new InMemoryConfigurer().MainFile(wurflDataFile);
var manager = WURFLManagerBuilder.Build(configurer);
HttpContext.Current.Cache["WurflManagerCacheKey"] = manager;
Run Code Online (Sandbox Code Playgroud)
这个3-5分钟的延迟让我们在5个开发人员的团队中花费了大约1-3个小时的生产时间(每次他们部署时,无论是本地还是PPE,人们都会起床吃零食等)
为了加快速度,我们最近创建了一个独立的Cloud WEB API服务,它接收正确的信息并返回一个包含我们所需信息的对象.我们从活动服务获得的响应时间非常短(少于30毫秒).
我现在面临的问题是,我不确定我们如何最好地改变我们的加载程序.
我可以制作HomeController async,等待房产,但这似乎很危险.处理这样的事情最好的方法是什么?
public static async Task<Device> GetDevice(RequestInfo requestInfo)
{
var client = new HttpClient();
client.BaseAddress = new Uri("http://mysiteurl.com");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsJsonAsync("/api/devicedetection", requestInfo);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsAsync<Device>();
}
return new Device(Device.Default);
}
//Proposed Solution from team as it stands
public async Task<ActionResult> Index(string id)
{
_device = await Device.GetDevice(Request.UserAgent);
try
{
if (!string.IsNullOrEmpty(_userName))
{
Session.Add("CurrentDevice", _device);
ViewBag.DeviceType = _device.Type;
ViewBag.DeviceOs = _device.Os;
}
}
catch (Exception ex)
{
Response.Redirect("/Error");
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
async-await在网站上使用没有任何问题.如果您正在执行的操作是自然异步的(例如I/O),则更为可取.它可以降低应用程序使用的资源并提高可伸缩性.*
我建议保持async命名标准,并通过在块内 移动GetDevice来重命名GetDeviceAsync并处理异常_device = await Device.GetDevice(Request.UserAgent);try-catch
*它可以略微增加每个操作的持续时间,但它允许同时进行更多操作