动态 CRM 在线对象缓存未正确缓存



我有一个要求,我们需要一个插件来从外部系统检索会话 ID 并将其缓存一段时间。我使用实体上的字段来测试会话是否实际被缓存。当我刷新CRM表单几次时,从输出中可以看出,同一密钥有四个版本(随时一致)。我尝试清除缓存并再次测试,但结果仍然相同。

任何帮助表示赞赏,提前感谢。

每次刷新页面时的输出:

20170511_125342:1:55a4f7e6-a1d7-e611-8100-c4346bc582c0

20170511_125358:1:55a4f7e6-a1d7-e611-8100-c4346bc582c0

20170511_125410:1:55a4f7e6-a1d7-e611-8100-c4346bc582c0

20170511_125342:1:55a4f7e6-a1d7-e611-8100-c4346bc582c0

20170511_125437:1:55a4f7e6-a1d7-e611-8100-c4346bc582c0

20170511_125358:1:55a4f7e6-a1d7-e611-8100-c4346bc582c0

20170511_125358:1:55a4f7e6-a1d7-e611-8100-c4346bc582c0

20170511_125437:1:55a4f7e6-a1d7-e611-8100-c4346bc582c0

为此,我实现了以下代码:

public class SessionPlugin : IPlugin
{
public static readonly ObjectCache Cache = MemoryCache.Default;
private static readonly string _sessionField = "new_sessionid";
#endregion
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
try
{
if (context.MessageName.ToLower() != "retrieve" && context.Stage != 40)
return;
var userId = context.InitiatingUserId.ToString();
// Use the userid as key for the cache
var sessionId = CacheSessionId(userId, GetSessionId(userId));
sessionId = $"{sessionId}:{Cache.Select(kvp => kvp.Key == userId).ToList().Count}:{userId}";
// Assign session id to entity
var entity = (Entity)context.OutputParameters["BusinessEntity"];
if (entity.Contains(_sessionField))
entity[_sessionField] = sessionId;
else
entity.Attributes.Add(new KeyValuePair<string, object>(_sessionField, sessionId));
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
private string CacheSessionId(string key, string sessionId)
{
// If value is in cache, return it
if (Cache.Contains(key))
return Cache.Get(key).ToString();
var cacheItemPolicy = new CacheItemPolicy()
{
AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration,
Priority = CacheItemPriority.Default
};
Cache.Add(key, sessionId, cacheItemPolicy);
return sessionId;
}
private string GetSessionId(string user)
{
// this will be replaced with the actual call to the external service for the session id
return DateTime.Now.ToString("yyyyMMdd_hhmmss");
}
}

Daryl在这里已经很好地解释了这一点: https://stackoverflow.com/a/35643860/7708157

基本上,您不是每个整个CRM系统都有一个MemoryCache实例,您的代码只是证明每个插件有多个应用程序域,因此即使存储在此类插件中的静态变量也可以具有多个值,这是您不能依赖的。MSDN 上没有文档可以解释 sanboxing 的工作原理(尤其是在这种情况下的应用程序域),但使用静态变量肯定不是一个好主意。当然,如果您在线处理,则无法确定是只有一个前端服务器还是其中的许多前端服务器(这也会导致此类行为)

类级别变量应限制为配置信息。不支持像现在这样使用类级别变量。在CRM Online中,由于有多个Web前端,一个特定的请求可能会由与另一个请求不同的插件类实例在不同的服务器上执行。总的来说,假设CRM是无状态的,除非持久化和检索,否则不应假定插件执行之间的任何内容都是连续的。

根据开发工具包:

插件的 Execute 方法应编写为无状态,因为 每次调用插件时都不会调用构造函数。 此外,多个系统线程可以同时执行插件 时间。所有每个调用状态信息都存储在上下文中, 因此,您不应使用全局变量或尝试将任何数据存储在 在下一次插件调用期间使用的成员变量,除非 该数据是从提供给的配置参数中获得的 构造函数。

参考: https://msdn.microsoft.com/en-us/library/gg328263.aspx

最新更新