我还在MS 论坛上发布了这个问题,在那里我得到了 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
基于该建议,我最终使用了 API 示例代码中的以下类。如果有人对此有反馈或正确/错误/更好的建议,请告诉我。
就 AppPool 的管理而言,我仍在寻找信息。
1. ManagedTokenOrganizationServiceProxy
2. AutoRefreshSecurityToken
3. DeviceIdManager
我将以下连接字符串添加到 web.config
<add name="XrmConnectionString" connectionString="ServiceUri=http://<MACHINE>:<PORTt>/<ORG>/XRMServices/2011/Organization.svc; Domain=<DOMAIN>; Username=<USERNAME>; Password=<PASSWORD>" />
然后我创建了以下类来创建连接。
/// <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
}
以下 MVC 代码使用连接:
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());
}