开发者

Connection/Object pool for OrganizationServiceProxy in XRM/Dynamics CRM 2011

I am writing a MVC 3 WebApp which uses XRM 2011 using Early Bound. This is an internet-facing application hosted on separate machine than Dynamics IIS.

This of course makes OrganizationServiceProxy call very very frequently and response is kind of sluggish on every first hit.

Is it advisable to reuse OrganizationServiceProxy connection rather than create new instance every time?

If yes,

  1. Is there anything to manage the connections such as
    • connection poo开发者_如何学JAVAl app - MS or third party/open source
    • or some framework like WCF (Never used WCF, yet)
  2. Which design pattern is recommended if I have to write my own code to manage connection?

Sorry for the duplicate post from MS website. Hopefully this forum is more active.


After few test cycles, I have found that using CrmConection is the fastest method. Compared to above caching implementation, CrmConnection runs at least 5 times faster.

CrmConnection connection = new CrmConnection("XrmConnectionString");   // Todo: Replace magic connection string
using (XrmVRC.XrmVrcServiceContext context = new XrmVRC.XrmVrcServiceContext(connection)) {
    // Processing...
}


This is quite an old question but for anyone else still looking have a read of this SDK Extensions for Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online. I believe the extensions take care of the caching/pooling of resources for you.

For a solution to OP original question have a look here and here. Below is a quote from the CRM SDK documentation linked above:

Developer Extensions for Microsoft Dynamics CRM provide the following capabilities:

  • Simplified connection to Microsoft Dynamics CRM servers provided by the CrmConnection class (Microsoft.Xrm.Client)

  • Code Generation of strong types provided by customizations to the code generation tool (CrmSvcUtil.exe)

  • App.config and Web.config configurability for enabling custom extensions provided by the CrmConfigurationManager class (Microsoft.Xrm.Client)

  • Performance enhancements through caching of service results provided by the CachedOrganizationService class (Microsoft.Xrm.Client)

The Portal Developer’s Guide enables you to build a Web portal that is tightly integrated with Microsoft Dynamics CRM. For more information, see Portal Developer Guide for Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online. This guide proves the following capabilities:

  • Security Management (Microsoft.Xrm.Portal)

  • Cache Management (Microsoft.Xrm.Portal)

  • Content Management (Microsoft.Xrm.Portal, Microsoft.Xrm.Portal.Files, WebSiteCopy.exe)


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());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜