Last post Oct 03, 2015 05:03 AM by Andrei Vinca
Oct 02, 2015 04:45 PM|Andrei Vinca|LINK
I have the following code:
public void ConfigureAuth(IAppBuilder app)
witch works as expected in Api Controllers, but I want to use the same thing in a SignalR Hub.
I have the following Hub (It is a simplifyed version of what I really have):
public class LiveHub : Hub<IConnectionHub>
SqlDatabaseContext = HttpContext.Current.GetOwinContext().Get<SqlDatabaseContext>();
public override Task OnConnected()
public void Test(string test)
If I use the Db context ( SqlDatabaseContext ) on OnConnected() method workes fine. When the method "test" is called from cliend I get: "The operation cannot be completed because the DbContext has been disposed".
Is there any way how I can set how the Db connext is disposed? OR Is there any way I can force the owin to recreate the context when the method is called?
Oct 02, 2015 10:08 PM|Rion Williams|LINK
Are you sure that this is the specific method that is triggering the error (or that there isn't something else that could be triggering it)? Generally, when you receive an error regarding the "... has been disposed" it's because the application is trying
to access something that isn't stored in memory and is making a request to your data source (which is closed at this point). You can usually resolve this by forcing whatever you are accessing out of your database into memory (generally by an AsEnumerable()
or ToList() call).
I suppose that you could instantiate an instance of your context (and wrap it in a using statement to ensure it was properly disposed of after use) similar to the following :
using(var context = HttpContext.Current.GetOwinContext().Get<SqlDatabaseContext>())
// Your code here (ensure any data being returned uses a ToList() or similar)
Oct 03, 2015 05:03 AM|Andrei Vinca|LINK
Thanks for the answer, the problem is, owin returns a disposed context.
What I understand until now: In a regular controller, when an action is called, the owin context is recreated for every request, and disposed when the life cycle ends.
In signalR, the context is not recreated, and will be forever disposed when called from signalR.