I'm building simple ASP.NET MVC
web page that show all active accounts from MS Dynamics CRM
.
Basically page works well if I compile it and run (with F5). Problem appears when I go to Microsoft Dynamics CRM web page, login and then change one account status from active to inactive. Now when I refresh page that I'm building, I get same old results.
I have tried refreshing (F5), hard refreshing (Ctrl + F5), disposing xrm
object before returning view, but nothing worked. So I think I misunderstood something.
Controllers/HomeControler.cs
public ActionResult Index()
{
using (var xrm = new XrmServiceContext("Xrm"))
{
var accounts = from a in xrm.AccountSet
where a.StateCode == 0
select a;
List<AccountModel> accountModels = new List<AccountModel>();
foreach (var account in accounts)
{
Debug.WriteLine(c+"\t"+account.Id+"\t"+account.Name);
Debug.WriteLine(account.Address1_Composite);
accountModels.Add(new AccountModel(
account.Id.ToString(),
account.Name,
account.Address1_Composite));
}
ViewBag.Title = "Page Title";
ViewBag.AccountModels = accountModels;
}
return View();
}
Views/Home/Index.cshtml
@{
Layout = @"~/Views/Shared/_Layout.cshtml";
}
<div class="table-responsive">
<table class="table table-bordered table-condensed table-hover">
<caption><h2>Active Accounts</h2></caption>
<thead>
<tr>
<th>Account ID</th>
<th>Account Name</th>
<th>Account Address</th>
</tr>
</thead>
<tbody>
@{
foreach (var accountModel in ViewBag.AccountModels)
{
<tr>
<td>@accountModel.Id</td>
<td>@accountModel.Name</td>
<td>@accountModel.Address</td>
</tr>
}
}
</tbody>
</table>
</div>
EDIT: Now I noticed that only first refresh after "Rebuild and Run" actually refresh site. Is this IIS Express (I'm doing this locally) fault?
It is important what you have configured in web.config for the XRM context. By default, the service is instantiated using the CachedOrganizationService
, which, as its name implies, caches all data.
To disable caching, use the following configuration (replacing the Xrm.XrmServiceContext, Xrm
with your own ServiceContext):
<microsoft.xrm.client>
<contexts>
<!-- Replace with your actual ServiceContext -->
<add name="Xrm" type="Xrm.XrmServiceContext, Xrm" serviceName="Xrm" instanceMode="PerRequest"/>
</contexts>
<services>
<!-- Disable cache -->
<add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client"/>
</services>
</microsoft.xrm.client>
For more information about the default configuration, check Developer extensions context object model on MSDN.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With