I also posted this question on MS Forum where I got following reply by Pat.
While it is somewhat limited, there is some direction regarding the caching of service connectivity in the SDK:
Performance Best Practises - Caching
The two primary suggestions being to:
1. Cache the IServiceConfiguration class
2. Monitor your WCF security token and refresh it before it expires
Based on that advise, I endedup using following classes from the API sample code. Please let me know if anyone has feedback or right/wrong/better advise on this.
As far management of AppPool is concerned, I still looking for info.
1. ManagedTokenOrganizationServiceProxy
2. AutoRefreshSecurityToken
3. DeviceIdManager
I added following connection string to web.config
<add name="XrmConnectionString" connectionString="ServiceUri=http://<MACHINE>:<PORTt>/<ORG>/XRMServices/2011/Organization.svc; Domain=<DOMAIN>; Username=<USERNAME>; Password=<PASSWORD>" />
Then I created following class to create connection.
/// <summary>Provides server connection information.</summary>
public class ServerConnection
{
private static readonly ILog log = LogManager.GetLogger(typeof(ServerConnection));
#region Public methods
/// <summary>
/// Obtains the OrganizationServiceProxy connection for the target organization's
/// Uri and user login credentials from theconfiguration.
/// </summary>
public static OrganizationServiceProxy getServiceProxy() {
ManagedTokenOrganizationServiceProxy serviceProxy = null;
log.Debug("in getServiceProxy");
try {
CrmConnection crmConnection = new CrmConnection("XrmConnectionString");
serviceProxy = new ManagedTokenOrganizationServiceProxy(crmConnection.ServiceUri, crmConnection.ClientCredentials);
log.Debug("ManagedTokenOrganizationServiceProxy created = " + serviceProxy);
serviceProxy.EnableProxyTypes();
} catch (Exception e) {
log.Fatal(e, e);
throw;
}
log.Debug("Returning serviceProxy");
return serviceProxy;
}
#endregion
}
Following MVC code consumes the connection:
public ActionResult Index() {
XrmVrcServiceContext context = null;
try {
context = new XrmVrcServiceContext(ServerConnection.getServiceProxy());
} catch (Exception e) {
log.Error(e, e);
throw;
}
return View(context.new_XYZEntitySet.ToList());
}