我有一个现有的 RIA 服务,我想在其中包含一个非常简单的调用来查找某个自定义对象允许的最大字段。该值不会经常更改,如果有的话,我想在需要时只调用一次,然后将其保留在客户端上。但是,当我需要知道值时,我需要以同步的方式知道它,因为我将立即使用它。
我尝试了以下方法,但.Value
始终只是 0,因为在运行此代码块时服务实际上并没有发出请求,而是稍后的某个时间。
private static readonly Lazy<int> _fieldCount =
new Lazy<int>(() =>
{
const int TotalWaitMilliseconds = 2000;
const int PollIntervalMilliseconds = 500;
// Create the context for the RIA service and get the field count from the server.
var svc = new TemplateContext();
var qry = svc.GetFieldCount();
// Wait for the query to complete. Note: With RIA, it won't.
int fieldCount = qry.Value;
if (!qry.IsComplete)
{
for (int i = 0; i < TotalWaitMilliseconds / PollIntervalMilliseconds; i++)
{
System.Threading.Thread.Sleep(PollIntervalMilliseconds);
if (qry.IsComplete) break;
}
}
// Unfortunately this assignment is absolutely worthless as there is no way I've discovered to really invoke the RIA service within this method.
// It will only send the service request after the value has been returned, and thus *after* we actually need it.
fieldCount = qry.Value;
return fieldCount;
});
有没有办法使用 RIA 服务进行同步的按需加载服务调用?或者我是否必须:1)在客户端代码中包含常量,并在/如果它发生变化时推出更新;或 2) 托管一个完全独立的服务,我可以以同步方式调用它?