我有一个Web API项目,它连接到CRM 2011实例以收集一些数据。一旦创建了来自运行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
非常感谢。
发布于 2014-11-27 11:51:30
由于您已经准备好连接到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示例中的一个略微修改的版本,如果您需要更多帮助或信息,请告诉我。
https://stackoverflow.com/questions/27154282
复制相似问题