I am trying to make RIA service calls from non UI thread.
I made calls with opening new thread and with background workers, but for both cases callback is running on UI thread.
Is it possible to execute callback on callers thread, not UI?
Thanks
tl;dr
Use WCF
Marshal your results to the UI thread yourself
WCF RIA is built to do work on the UI thread
WCF RIA was designed to work mostly on the UI thread. That obviously has many pros and cons; mostly cons in your case. I'm having trouble finding definitive documentation of this design, however, most questions on the topic are answered by affirming the UI threadedness.
Here is a post from a member of the WCF RIA QA team indicating UI-thread-only processing.
Andy French describes the callbacks marshalling to the UI thread:
The Domain Context Load and SubmitChanges execute asynchronously. They take a thread from the thread pool, make the necessary calls to the server, and when those calls complete the work is automatically marshalled back to the UI thread to modify the entity collections and subsequently update the UI (probably via INotificationChanged).
If you use WCF to get your own data, you can do it on any thread you like. When the calls complete, you will have to write (or borrow) the code to update the UI on the UI thread to avoid cross thread exceptions.
IMO, the main advantages of WCF RIA giving simple problems simple solutions:
Great tooling for re-using code between the server and client
Service and client code are always compatible
Transferring data to/from the client/server is relatively simple
WCF RIA is strongly opinionated resulting in easy-to-learn coding patterns
The cons make hard problems hard or impossible:
WCF RIA is strongly opinionated and not following that opinion is painful or impossible
All operations return on the UI thread, often causing performance problems
There is some voodoo to achieve the highest amount of client+server code re-use
Related
We are going to create a product from our existing application. Our existing application is using Entity framework 6, Web API 2, ninject. So instead of creating a new design and using a new technologies, we are using same technologies i.e,Entity framework 6, Web API 2, ninject. There will be heavy database operations. We are able to divide the complete operations to small tasks. So we decided to use multi threading. My question is.
1) How to configure ninject to create a new dbcontext on every thread call?
2) Once the thread done its operations, the database changes should effect immediately irrespective of remaining threads done their operations or not.
3) Once each thread operations are done, the thread should be disposed along with its dbcontext.
4) Will there be any side effects on using multi threading in entity framwork.
A coding examples will be greatly appreciated.
1) How to configure ninject to create a new dbcontext on every thread call?
Use Tasks. Inside each Task create a new DbContext in a using block. I guess in ninject that means a Transient scope.
2) Once the thread done its operations, the database changes should effect immediately irrespective of remaining threads done their operations or not.
Using a separate DbContext in each Task should do that.
4) Will there be any side effects on using multi threading in entity framework.
You may experience locking waits, resource exhaustion, or deadlocking in your database.
3) Once each thread operations are done, the thread should be disposed along with its dbcontext.
With Tasks you don't have explicitly manage the Threads.
A coding examples will be greatly appreciated.
It's not clear exactly why you want to use parallelism in the client. If you could explain that a bit, including details about your databsae, there might be relevant coding examples. Or it may become clear that multi threading at the client is just not helpful.
I have a app in which I need to query a sharepoint site via services. The app will be under heavy usage so performance and scalability will be two of my priorities.
I started to investigate which service approach is better and from a perf point of view it seems that client object model is the one officially suggested, but when I came to scalability I personally found that actually web services seem more potent than client object model. This is because it seems that web services have async support for IO bound operations rather than client object model that doesn't. I say doesn't because as Stephen Toub said the ability to invoke a synchronous method asynchronously does nothing for scalability and I think BeginInvoke does just that.
I have to mention that I am using in my app C# 5 async/await feature in order to return the thread to the thread pool when queries are executed on the server.
My question is, what should weight more in order to take a decision?
Edit: It is worth to mention that I am not using the Silverlight CSOM, I am using the more generic .NET one.
This is an answer with no answer. :)
You are correct in that BeginInvoke is fake-asynchronous (i.e., it just issues the blocking call on a thread pool thread). So it would actually be worse, scalability-wise, than just invoking the blocking methods.
First, consider your scalability as compared to your Sharepoint server. If you're running on roughly equivalent hardware, then you probably don't need to scale any more than the Sharepoint server will. You would probably be fine with either solution.
If you do need to scale better (e.g., the Sharepoint server is a cluster or cloud, or if your machine is much lesser than the Sharepoint machine), then it requires more thought and likely testing.
The better performance in the client model is purely from its batching capabilities. So if your application won't use batching, then the (asynchronous) web services model would be better.
However, if your application uses batching and needs to scale better, then there isn't an answer. In this case, the only way to know is to build a test case both ways and measure it.
Actually, the client object model not only provides, but sometimes requires asynchronous access. You can find more info in the Data Retrieval Overview but the short version is that:
You create and load one or more queries in a ClientRuntimeContext then
Execute all loaded queries either synchronously with ClientRuntimeContext.ExecuteQuery or asynchronously with ClientRuntimeContext.ExecuteQueryAsync with two callbacks for success or failure
You don't have to use BeginInvoke anywhere.
In fact, the Client object model in Silverlight provides only the Async method in which case you are forced to execute the call asynchronously.
The syntax is quirky, but the client object model targets .NET 3.5 and Silverlight, so it wasn't possible to provide a Task based interface. You can even argue that callbacks are somewhat simpler than the Begin/End pattern and definitelly better that raising completion events.
EDIT
As #alexb noticed, ExecuteQueryAsync is available only in Silverlight. There are other ways to work asynchronously though.
You can take advantage of Sharepoint's OData support to query lists using WCF Data Services. The query scenario is a bit similar, as you submit your query and wait for a callback when the results come in. In the meantime, you get access to a DataServiceQuery object that represents the asynchronous query.
This method uses REST/Json and therefore lighter on the wire than the web services. The LINQ and ORM-like API are also easier to work with compared to the web services.
Sharepoint's support is described in Query SharePoint Foundation with ADO.NET Data Services and asynchronous querying is described in How to: Execute Asynchronous Data Service Queries (WCF Data Services)
Could you give more explanations on MvvmCross multithreading?
ViewModel calls to Views are safe, so there must be no any conflicts.
However, IMvxMessanger has SubscribeOnThreadPoolThread and also SubscribeOnMainThread (except just Subscribe), which are not really clear for me when to use them.
Also, what's about multithreading inside of ViewModel (for instance, if two web-requests are activated simultaneously and on their results each of them tries to access my dataservice (for instance, writing data to database))?
(Or there are some other such special situations you know from your experiense).
Thank you!
For the single technical question about the differences between the subscribe methods on the messenger, these are explained in the XML comments on the interface (but are also largely self-explanatory anyway)
subscribe on main thread - messages will be received on main thread
subscribe on thread pool thread - messages will be received on a thread pool thread
subscribe - messages will be received, no assumptions should be made about which thread
Xml comments at - https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/Messenger/Cirrious.MvvmCross.Plugins.Messenger/IMvxMessenger.cs#L15
For the rest of the question: as an app developer, you are free to use threading and async in your code - and the normal c# and .net multithreading objects are there for you to use (or a portable subset of them) - synchronising access to a resource is just a normal development decision and technique.
The Problem
Our liferay system is the basis to synchronize data with other web-applications.
And we use Model Listeners for that purpose.
There are a lot of web-service calls and database updates through the listeners and consequently the particular action in liferay is too slow.
For example:
On adding of a User in liferay we need to fire a lot of web-service calls to add user details and update other systems with the userdata, and also some liferay custom tables. So the adding of User is taking a lot of time and in a few rare cases the request may time-out!
Since the code in the UserListener only depends on the User Details and even if there is any exception in UserListener still the User would be added in Liferay, we have thought of the following solution.
We also have a scheduler in liferay which fixes things if there was some exception while executing code in Listeners.
Proposed Solution
We thought of making the code in UserListener asynchronous by using Concurrency API.
So here are my questions:
Is it recommended to have concurrent code in Model Listeners?
If yes, then will it have any adverse effect if we also update Liferay custom tables through this code, like transactions or other stuff?
What can be other general Pros and Cons of this approach?
Is there any other better-way we can have real-time update to other systems without hampering User-experience?
Thank you for any help on this matter
It makes sense that you want to use Concurrency to solve this issue.
Doing intensive work like invoking web services etc in the thread that modifies the model is not really a good idea, apart from the impact it will have on user experience.
Firing off threads within the models' listeners may be somewhat complex and hard to maintain.
You could explore using Liferay's Message Bus paradigm where you can send a message to a disconnected message receiver which will then do all the intensive work outside of the model listener's calling thread.
Read more about the message bus here:
Message Bus Developer Guide
Message Bus Wiki
I've been using JMS Message Driven Bean for a while and it is working great for the asynchronous tasks. I know that there is many ways to handle the asynchronous processes, but I am just curious what are the benefits over using JMS Message Driven Bean and ScheduledThreadPoolExecutor?
For example I have a web service which handles some tasks asynchronously. So I see two main differences. If I would be using ScheduledThreadPoolExecutor I don't need application server, I could use a servlet container for e.g. Tomcat, because I am not using any EJB stuff, for MDB I need an application server, for e.g. Glassfish. But in terms of handling the actual asynchronous process, what are the advantages over each ScheduledThreadPoolExecutor and MDB?
ScheduledThreadPoolExecutor is used to schedule tasks, the abstraction best corresponding to MDB is ExecutorService. But back to your question.
MDB is more heavyweight, API is much more complex and in principle it was actually designed for transferring data, not logic. On the other hand ExecutorService is a thin layer on top of actual thread pool. So if you need performance, low latency and small overhead, go for ordinary thread pool.
The only reason for MDB and JMS is when you need durability and transaction support. That of course introduces even bigger overhead as each message needs to be persisted. But you won't loose any tasks that are queued or even in the middle of processing are not lost due to crash.