How to make a synchronous RIA request

I have an existing RIA service in which I would like to enable a very simple call to find the maximum fields allowed for a specific user object. The value will change infrequently, if ever, and I would like to name it only once, when necessary, and then save it to the client. However, when I need to know the value, I need to know it synchronously, since I will use it immediately.

I tried the following, but .Value always just 0, since the service does not actually make a request when this block of code is started, but rather after a while.

 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; }); 

Is there a way to make a synchronous on-demand load service call using RIA services? Or I need to either: 1) include the constant in the client code, and push the update when / ever it changes; or 2) host a completely separate service that I can invoke synchronously?

+1
source share
2 answers

You cannot force WCF RIA to work synchronously, unfortunately. You can put the value in the InitParams tag of the <object> in the HTML that hosts Silverlight. More details: http://msdn.microsoft.com/en-us/library/cc189004(v=vs.100).aspx

+3
source

I understand that the previous answer here could be true a couple of years ago, but now this is not quite the way I just discovered. Look at the wait statement http://msdn.microsoft.com/en-us/library/hh156528.aspx

I think this is exactly what you are looking for. You can call it from the asynchronous method (you must use the async modifier at the beginning of the method, for example: private async void dostuff ()). Although the parent method is still asynchronous, it will wait for the task to be called.

Say you are doing this from a domain data service. Here is an example: Note. DDS should return an IEnumerable type. Before you call data from your DDS, define a private task method that retrieves the data in question, for example:

 private Task<IEnumerable<fieldCounts>> GetCountssAsync() { fieldCountsEnumerable_DS _context = new fieldCountsEnumerable_DS (); return _context.LoadAsync(_context.GetCountsQuery()); } 

You can then call this task from an existing async ria service method or any client method that really uses:

 IEnumerable<fieldCounts> fieldcnts = await GetCountssAsync(); enter code here 

Just know, no matter what method you call it, this method should be asynchronous, as in the documentation. It must return control to the caller.

+1
source

All Articles