I have web app which invokes OData Webservice (DataServiceContext
) via a proxy. The issue is the code even though makes a call to OData webservice every time, it always returns old data after changing content in the Content Management system (SDL Tridion).
string getPageContentForUrl(string url)
{
var page = cdService
.Pages
.Expand("PageContent")
.Where(x => x.Url == url)
.FirstOrDefault();
if (page == null || page.PageContent == null)
{
return string.Empty;
}
else
{
return page.PageContent.Content;
}
}
We had to an reset the apppool to see the latest data changes.
So while debugging more, I noticed that
var context = (System.Data.Services.Client.DataServiceContext)cdService;
context.Entities[0].State = Unchanged
so I tried fixing it by calling .Detach()
explicitly before returning the value from getPageContentForUrl
, so something like,
cdService.Detach(page);
cdService.Detach(page.PageContent);
My question is, can I do the above at more "global" level, maybe have webservice always assume the State as "Changed", since I don't want to manually write code to Detach()
?
I think the answer lies indeed - as you suspected - in the proxy you're using, or rather in the DataServiceContext. This is what Microsoft has to say:
By default, the client only materializes an entry in the response feed into an object for entities that are not already being tracked by the DataServiceContext. This means that changes to objects already in the cache are not overwritten. This behavior is controlled by specifying a MergeOption value for queries and load operations.
To me, this sounds exactly like the behaviour that you're describing. Fortunately, the caching can be turned off by setting the MergeOption property on the DataServiceContext.