I'm trying to implement a per-request singleton pattern of my ObjectContext. To do that I use the events ContextCreating and ContextDisposing of the EntityDataSource as suggested in
http://msdn.microsoft.com/en-us/library/cc668193%28VS.100%29.aspx. This technique works perfectly fine for controls that are not connected with a DynamicDataManager. But when I try to apply the same technique using Dynamic Data, a new ObjectContext is ALWAYS
created, even though the ContextCreating event does get executed.
I've thrown in the ContextCreated event to see what's going on through debugging. The first event is first executed and the e.Context receives my Singleton. To be sure I do a GetHashCode on that object and record the number. Then the ContextCreated event
is launched and the e.Context contains a DIFFERENT object (GetHashCode returns a different number).
I repeat that this behavior is only seen when the EventDataSource is used in conjuction with a server control that is registered with a DynamicDataManager. If no DynamicDataManager is involved the ObjectContext objects in ContextCreating and ContextCreated
are the same!
I looked at this a little today.... here's what I saw:
DynamicDataManager subscribes to the EntityDataSource's ContextCreating event as well (just as you are!) and creates a new ObjectContext there.
So, here's where the fun begins -- since both (you and the DDM) subscribed to same event, the order in which event handlers are fired matters... In your case, because you subscribed declaratively (in HTML markup), your event handler fired first... it did
its thing, then DDM's event handler overwrote your ObjectContext with a new one.
One way to work around this is to wait as late as possible to wire up the event handler (to ensure it gets run last).... In your sample, it's fairly straightforward -- just remove OnContextCreating=.... from the HTML markup for the EntityDataSource, and
move that operation to Page_Load.... I added this to your example:
protected void Page_Load()
{
edsDynamic.ContextCreating += new EventHandler<EntityDataSourceContextCreatingEventArgs>(edsDynamic_ContextCreating);
}
and things started working the way you were expecting it.
The above is "what" happens... I am checking with my colleagues on "why" DDM forces creating a new ObjectContext...
Thanx for the info! Very accurate indeed, after putting the event wiring in the Page_Load all worked as expected. Now, even though I do see the construction of an extra ObjectContext I rest assured that it is not used by the EntityDataSource.
Although my problem is solved I do not mark it "answered" in expectance of the answer to the "why" question and an estimation of whether this behavior will change.
None
0 Points
13 Posts
Problem creating an ObjectContext in the ContextCreating event of an EntityDataSource in Dynamic...
Jan 26, 2010 01:39 PM|kkara|LINK
Hi there,
I'm trying to implement a per-request singleton pattern of my ObjectContext. To do that I use the events ContextCreating and ContextDisposing of the EntityDataSource as suggested in http://msdn.microsoft.com/en-us/library/cc668193%28VS.100%29.aspx. This technique works perfectly fine for controls that are not connected with a DynamicDataManager. But when I try to apply the same technique using Dynamic Data, a new ObjectContext is ALWAYS created, even though the ContextCreating event does get executed.
My code behind looks something like this :
protected void ds_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e) {
e.Context = OT.Entities.EntitiesContext.GetInstance();
}
protected void ds_ContextDisposing(object sender, EntityDataSourceContextDisposingEventArgs e) {
e.Cancel = true;
}
protected void ds_ContextCreated(object sender, EntityDataSourceContextCreatedEventArgs e) {
}
I've thrown in the ContextCreated event to see what's going on through debugging. The first event is first executed and the e.Context receives my Singleton. To be sure I do a GetHashCode on that object and record the number. Then the ContextCreated event is launched and the e.Context contains a DIFFERENT object (GetHashCode returns a different number).
I repeat that this behavior is only seen when the EventDataSource is used in conjuction with a server control that is registered with a DynamicDataManager. If no DynamicDataManager is involved the ObjectContext objects in ContextCreating and ContextCreated are the same!
Any ideas why this might be happening?
dynamic data EntiyDataSource
Member
520 Points
304 Posts
Re: Problem creating an ObjectContext in the ContextCreating event of an EntityDataSource in Dyna...
Jan 27, 2010 01:34 PM|Radomir|LINK
Sent you a PM
Member
520 Points
304 Posts
Re: Problem creating an ObjectContext in the ContextCreating event of an EntityDataSource in Dyna...
Jan 28, 2010 02:39 PM|Radomir|LINK
I looked at this a little today.... here's what I saw:
DynamicDataManager subscribes to the EntityDataSource's ContextCreating event as well (just as you are!) and creates a new ObjectContext there.
So, here's where the fun begins -- since both (you and the DDM) subscribed to same event, the order in which event handlers are fired matters... In your case, because you subscribed declaratively (in HTML markup), your event handler fired first... it did its thing, then DDM's event handler overwrote your ObjectContext with a new one.
One way to work around this is to wait as late as possible to wire up the event handler (to ensure it gets run last).... In your sample, it's fairly straightforward -- just remove OnContextCreating=.... from the HTML markup for the EntityDataSource, and move that operation to Page_Load.... I added this to your example:
and things started working the way you were expecting it.
The above is "what" happens... I am checking with my colleagues on "why" DDM forces creating a new ObjectContext...
None
0 Points
13 Posts
Re: Problem creating an ObjectContext in the ContextCreating event of an EntityDataSource in Dyna...
Jan 29, 2010 04:26 AM|kkara|LINK
Thanx for the info! Very accurate indeed, after putting the event wiring in the Page_Load all worked as expected. Now, even though I do see the construction of an extra ObjectContext I rest assured that it is not used by the EntityDataSource.
Although my problem is solved I do not mark it "answered" in expectance of the answer to the "why" question and an estimation of whether this behavior will change.
Thanx again for the quick response and solution.
Kostas
Contributor
2292 Points
908 Posts
Microsoft
Re: Problem creating an ObjectContext in the ContextCreating event of an EntityDataSource in Dyna...
Feb 12, 2010 03:41 PM|davidfowl|LINK
When you register your object context in global asax then you should use the overload to RegisterContext that takes a context factory:
Principal SDE, ASP.NET Team, Microsoft