简体   繁体   中英

Is there an OrganizationService connection keep alive setting

I have a Web API project that connects to a CRM 2011 instance to gather some data. Once the initial connection from the server that runs the Web API project is created, the application tends to run reliably and returns that data in an acceptable amount of time.

The problem is that if this connection needs to be recreated because it was closed by IIS (the connection can be closed by IIS within only 10-20 seconds sometimes) the user that's browsing the UI (which calls the Web API project) can sit there for a painful 10-15 seconds before getting a grid to be refreshed.

The connection takes quite some time to be established I would think because there's an ISA server between the web server (on which the Web API runs) and the CRM 2011 server.

I've tested that it is the connection that takes a long time to be reestablished by firing up IE on the web server and simply browing the CRM instance. Once the connection is made, very fast. If I let IE sit for a minute and the refresh a grid, 10-15 seconds wait time.

I know it's possible to configure some caching within the web.config , but in my experience, this lead to issues where the users browsing the UI (that is fed by the Web API application) won't exactly see the actual data held in our CRM instance for quite sometime before the cache is refreshed.

So I'm simply looking if there's a way to extend the lifetime of the connection between the web server running the Web API project and the CRM instance.

Is this possible from within the web.config file ? Here's the relevant part of my config file at the moment:

<connectionStrings>
    <add name="Xrm" connectionString="Server=https://crm.ourdomain.ca/org" />
</connectionStrings>
<microsoft.xrm.client>
    <contexts>
        <add name="Xrm" type="XrmPortal.Service.XrmServiceContext, CustomerPortal.WebAPI" />
    </contexts>
    <services>
        <add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client" />
    </services>
</microsoft.xrm.client>

Here's how I connect to CRM in my Web API project:

using (XrmServiceContext xrm = new XrmServiceContext())
{
    var contact = xrm.ContactSet.SingleOrDefault(x=>x.Id == theGuid);
}

XrmServiceContext is inheriting Microsoft.Xrm.Client.CrmOrganizationServiceContext

Thanks very much.

Since you are allready connecting to the CRM 2011 Web API, one can use the example from the SDK when connecting to the service :

private readonly AutoRefreshSecurityToken<OrganizationServiceProxy, IOrganizationService> _proxyManager;

then use the _proxyManger to renew the token everytime a call is made or if required.

    protected override void AuthenticateCore()
    {
        _proxyManager.PrepareCredentials();...

and

    protected override void ValidateAuthentication()
    {
        _proxyManager.RenewTokenIfRequired();...

this is what the renewtoken method looks like:

    /// <summary>
    ///   Renews the token (if it is near expiration or has expired)
    /// </summary>
    public void RenewTokenIfRequired()
    {
        if (null == _proxy.SecurityTokenResponse
            || !(DateTime.UtcNow.AddMinutes(15) >= _proxy.SecurityTokenResponse.Response.Lifetime.Expires)) return;
        try
        {
            _proxy.Authenticate();
        }
        catch (CommunicationException)
        {
            if (null == _proxy.SecurityTokenResponse ||
                DateTime.UtcNow >= _proxy.SecurityTokenResponse.Response.Lifetime.Expires)
                throw;

            // Ignore the exception 
        }
    }

This is a slightly modified version from the SDK samples, Let me know if you need more help or info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM