New Posts New Posts RSS Feed: Use Entity Manager on server. How?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Use Entity Manager on server. How?

 Post Reply Post Reply
Author
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 144
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post Topic: Use Entity Manager on server. How?
    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?
 


Edited by katit - 01-Feb-2012 at 2:51pm
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 783
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 01-Feb-2012 at 2:58pm
Hi katit,
 
To disable cross-thread checking, set EntityManager.AuthorizedThreadId = null
 
Regards,
   Silvio.
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 144
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 783
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
katit View Drop Down
Senior Member
Senior Member


Joined: 09-Sep-2011
Posts: 144
Post Options Post Options   Quote katit Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
berardelia View Drop Down
Newbie
Newbie


Joined: 30-Mar-2012
Posts: 1
Post Options Post Options   Quote berardelia Quote  Post ReplyReply Direct Link To This Post Posted: 30-Mar-2012 at 6:10am
This solution works only for Silverlightapps? What about ASP.NET?
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down