XRM / Dynamics CRM 2011中OrganizationServiceProxy的连接/对象池

问题描述

|| 我正在编写一个MVC 3 WebApp,它使用Early Bound使用XRM 2011。这是与Dynamics IIS托管在不同计算机上的面向Internet的应用程序。 当然,这会使OrganisationServiceProxy调用非常频繁,并且每次单击时响应都会变慢。 建议重用OrganizationServiceProxy连接,而不是每次都创建新实例吗? 如是, 有什么要管理的连接,例如 连接池应用程序-MS或第三方/开源 或WCF之类的框架(尚未使用WCF) 如果必须编写自己的代码来管理连接,建议使用哪种设计模式? 抱歉,MS网站上的帖子重复。希望这个论坛更加活跃。     

解决方法

经过几个测试周期后,我发现使用CrmConection是最快的方法。与上述缓存实现相比,CrmConnection的运行速度至少快5倍。
CrmConnection connection = new CrmConnection(\"XrmConnectionString\");   // Todo: Replace magic connection string
using (XrmVRC.XrmVrcServiceContext context = new XrmVRC.XrmVrcServiceContext(connection)) {
    // Processing...
}
    ,这是一个很老的问题,但是对于仍在寻找其他任何人的人,请阅读此Microsoft Dynamics CRM 2011和Microsoft Dynamics CRM Online的SDK扩展。我相信扩展将为您处理资源的缓存/池化。 对于OP原始问题的解决方案,请在此处和此处查看。以下是上面链接的CRM SDK文档的引文:   Microsoft Dynamics CRM的开发人员扩展提供以下功能:         CrmConnection类(Microsoft.Xrm.Client)提供的与Microsoft Dynamics CRM服务器的简化连接   定制为代码生成工具(CrmSvcUtil.exe)提供的强类型的代码生成   App.config和Web.config可配置性,用于启用CrmConfigurationManager类(Microsoft.Xrm.Client)提供的自定义扩展   通过缓存CachedOrganizationService类(Microsoft.Xrm.Client)提供的服务结果来提高性能         《门户网站开发人员指南》使您可以构建与Microsoft Dynamics CRM紧密集成的Web门户。有关详细信息,请参阅《 Microsoft Dynamics CRM 2011门户开发人员指南》和《 Microsoft Dynamics CRM Online》。本指南证明了以下功能:         安全管理(Microsoft.Xrm.Portal)   缓存管理(Microsoft.Xrm.Portal)   内容管理(Microsoft.Xrm.Portal,Microsoft.Xrm.Portal.Files,WebSiteCopy.exe)        ,我也将此问题发布在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());
}
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...