是否有组织服务连接保持活动状态设置



我有一个连接到CRM 2011实例以收集一些数据的Web API项目。创建来自运行 Web API 项目的服务器的初始连接后,应用程序往往会可靠地运行,并在可接受的时间内返回该数据。

问题是,如果由于此连接被 IIS 关闭而需要重新创建(IIS 有时只需 10-20 秒即可关闭连接),则正在浏览 UI(调用 Web API 项目)的用户可能会在那里痛苦地坐 10-15 秒,然后刷新网格。

我认为建立连接需要相当长的时间,因为在 Web 服务器(运行 Web API 的服务器)和 CRM 2011 服务器之间有一个 ISA 服务器。

我已经测试过,通过在 Web 服务器上启动 IE 并简单地浏览 CRM 实例,需要很长时间才能重新建立连接。一旦建立连接,非常快。如果我让IE静置一分钟并刷新网格,则需要等待10-15秒的时间。

我知道可以在web.config中配置一些缓存,但根据我的经验,这会导致以下问题:浏览 UI(由 Web API 应用程序提供)的用户在缓存刷新之前很长一段时间内无法完全看到我们的 CRM 实例中保存的实际数据。

因此,我只是在寻找是否有一种方法可以延长运行 Web API 项目的 Web 服务器和 CRM 实例之间连接的生命周期。

是否可以从web.config文件中执行此操作?这是我目前配置文件的相关部分:

<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>

以下是我在 Web API 项目中连接到 CRM 的方法:

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

XrmServiceContext继承Microsoft.Xrm.Client.CrmOrganizationServiceContext

非常感谢。

由于您已准备好连接到CRM 2011 Web API,因此可以在连接到服务时使用SDK中的示例:

private readonly AutoRefreshSecurityToken<OrganizationServiceProxy, IOrganizationService> _proxyManager;

然后,每次调用时或在需要时使用 _proxyManger 续订令牌。

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

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

这是更新令牌方法的样子:

    /// <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 
        }
    }

这是 SDK 示例中略有修改的版本,如果您需要更多帮助或信息,请告诉我。

最新更新