I’m trying to setup a session in Active Record for each WCF request. Here is the code:
[WebGet(UriTemplate = "ReadMessages?authUserId={authUserId}&authGuid={authGuid}&userId={userId}&lastMessageId={lastMessageId}&startDate={startDate}&endDate={endDate}")]
public IQueryable<Message> ReadMessages(int authUserId, string authGuid, uint userId, uint lastMessageId,
DateTime startDate, DateTime endDate)
{
UserUtility.Authenticate(authUserId, authGuid);
using (new SessionScope())
{
//get messages.
return from m in MessageData.FindAll(userId, lastMessageId, startDate, endDate)
select ConvertToView(m);
}
}
Even though I have the SessionScope using block, it still gives me a lazy load error because it’s returning an IQueryable and so it’s converting to a view, which triggers lazy loading, after it’s out of the using block I’m guessing. Here is the error:
Initializing[xxx.Business.Schemas.CommonSchemas.Models.Messaging.Message#6575]-failed to lazily initialize a collection of role: xxx.Business.Schemas.CommonSchemas.Models.Messaging.Message.MessageStatusHistories, no session or session was closed
In my configuration, I have IsRunningWebApp as true.
var source = new InPlaceConfigurationSource();
source.IsRunningInWebApp = true;
If you’re wondering why I’m returning IQueryable from a WCF web method, it’s because I’m using the WCF Web API (http://wcf.codeplex.com/wikipage?title=WCF%20HTTP), which allows you to query your objects in the url querystring using ODATA.
What am I doing wrong? How do I create a session that lives long enough to lazy load the models as I convert them to views on return from the web methods?
I ended up getting it working using this in my Global.asax:
Note: I also had to remove the using(new SessionScope()) from the web method, that will interfere with the solution above.