Print Page | Close Window

Use Entity Manager on server. How?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3248
Printed Date: 19-Apr-2024 at 12:12pm


Topic: Use Entity Manager on server. How?
Posted By: katit
Subject: Use Entity Manager on server. How?
Date Posted: 01-Feb-2012 at 2:40pm
I have silverlight application that uses Model on a client.
 
I also have mobile clients that connect to the same server (WCF REST)
 
I'm using EntityManager on server as well but get errors like:
 
An EntityManager can only execute on a single thread. This EntityManager is authorized to execute on the thread with id=’13’; the requested operation came from the thread with Id=‘14’.
Consider calling the EntityManager’s asynchronous methods; they work safely on background threads managed by DevForce.
You may have to disable this cross-thread checking for specific reasons such as automated testing. Please review our documentation on multi-threading issues and the EntityManager.AuthorizedThreadId property.
 
What does it mean and how do I work with it? Prior to getting DevForce we used EF and there was no issues like this. We don't want to maintain 2 models with the same stuff and converted to DevForce.
 
Mobile service looks like this:
 
[Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MobileService
    {
        [Import]
        public IContextManager ContextManager { get; set; }
        [Import]
        public IMembershipService MembershipService { get; set; }
 

 
So, that means I'm having instance per call. However my ContextManager and MembershipService shared and I guess this is where the problem is. I understand I can make them non-shared. Or is there other way to solve this problem?
 
I realize that I probably should do single instance per-call which means I should use NonShared on all of my DI-objects.
 
I wonder how DevForce will perform in such scenario? I have very light DB calls (small inserts) but there is a lot of calls. Up to 1000 per minute which means I will have to create EntityManager instance every time. Is that a big load? Or it will be fine?
 



Replies:
Posted By: sbelini
Date Posted: 01-Feb-2012 at 2:58pm
Hi katit,
 
To disable cross-thread checking, set EntityManager.AuthorizedThreadId = null
 
Regards,
   Silvio.


Posted By: katit
Date Posted: 01-Feb-2012 at 3:03pm
Silvio,
 
I checked "Multithreading" article on drc, disabling is not really a good solution if it might present issues. I'd rather play safe. Do you have any experience using EntityManager is similar scenario?


Posted By: sbelini
Date Posted: 07-Feb-2012 at 2:04pm
Hi Katit,
 
If you want to play safe, your best bet should be having a single EM instance per-call.
 
Note that after the first request there won't be a really bad overhead while creating an EM each time, since DevForce will have done all its static initialization.
 
However, if you still think that the performance has drastically decreased, you could try one of the following:
 
1) use the EM copy constructor to pass credentials from an already logged in EM;
2) store the EM in a session variable if you expect to have sessions;
3) create a pool of EMs and grab one from the pool for each request, and then return to the pool when done.
 
I hope this helps.
 
Regards,
   Silvio.


Posted By: katit
Date Posted: 07-Feb-2012 at 3:59pm
Thank you!
 
I went single-instance per call and it seems like it's working OK
 
Server is stable, CPU low, all good. It is slowish on response but it is consistently slow which is fine for what I do. Seems like I'm paying tax on object initializations but it's not in system load. Just warm-up time.


Posted By: berardelia
Date Posted: 30-Mar-2012 at 6:10am
This solution works only for Silverlightapps? What about ASP.NET?



Print Page | Close Window