MS CRM Callouts Tip - Avoid Dead Loop in Callouts铪
来源:互联网 发布:网络固定座机 编辑:程序博客网 时间:2024/05/21 19:28
Problem: If you call CRM web service to update an entity in this entity's PostUpdate event handler, you may get a dead loop in your Callouts. This problem is not easy to find out as the way you debug the Callouts. If you get a blank page in CRM Web UI after some Callouts is triggered, check Windows Event Logs and you may find an error saying something has timeouted.
Cause: The reason is in a PostUpdate event handler, calling of CRM web service to update the entity will trigger another PostUpdate event, then this PostUpdate event handler is executed again, then dead loop...
Solution: If you really need some update to the entity in its PostUpdate event handler, e.g. update some customized fields on this entity, here is my solution:
A helper class:
class CalloutHelper
{
private static System.Collections.Hashtable _FlagDictionary = Hashtable.Synchronized(new Hashtable());
public static void SetBypassCalloutFlag(string entityGUID, string userGUID)
{
string flagKey = GenerateFlagKey(entityGUID,userGUID);
if(!_FlagDictionary.ContainsKey(flagKey))
{
lock (_FlagDictionary.Keys.SyncRoot)
{
_FlagDictionary.Add(flagKey, null);
}
}
}
public static void ResetBypassCalloutFlag(string entityGUID, string userGUID)
{
string flagKey = GenerateFlagKey(entityGUID, userGUID);
if (_FlagDictionary.ContainsKey(flagKey))
{
lock (_FlagDictionary.Keys.SyncRoot)
{
_FlagDictionary.Remove(flagKey);
}
}
}
public static bool IsBypassCallout(string entityGUID, string userGUID)
{
string flagKey = GenerateFlagKey(entityGUID, userGUID);
return _FlagDictionary.ContainsKey(flagKey);
}
private static string GenerateFlagKey(string entityGUID, string userGUID)
{
return entityGUID + "&" + userGUID;
}
}
At the very beginning of PostUpdate event handler, use the helper class to check if this event is bypassed
public override void PostUpdate(CalloutUserContext userContext, CalloutEntityContext entityContext,
string preImageEntityXml, string postImageEntityXml)
{
if (CalloutHelper.IsBypassCallout(entityContext.InstanceId.ToString(), userContext.UserId.ToString()))
{
CalloutHelper.ResetBypassCalloutFlag(entityContext.InstanceId.ToString(), userContext.UserId.ToString());//reset flag
return;//skip this callouts as it is flaged to be bypassed
}
// your PostUpdate logic goes there
// code to call CRM web service to update this entity
CalloutHelper.SetBypassCalloutFlag(entityContext.InstanceId.ToString(), userContext.UserId.ToString());//set flag
}
You may have found that the key of the flag is based on combination of entity GUID and user GUID, this is assuming that one user cannot update the same entity twice simutaniously
[Update 2007-10-22]: In callouts code when you want to update a CRM entity through the CRM web service, please ensure you use impersonation to call the web service. This will make sure you get correct userContext.UserId to make above code work. - see how to do impersonation http://msdn2.microsoft.com/en-us/library/aa682071.aspx
Cause: The reason is in a PostUpdate event handler, calling of CRM web service to update the entity will trigger another PostUpdate event, then this PostUpdate event handler is executed again, then dead loop...
Solution: If you really need some update to the entity in its PostUpdate event handler, e.g. update some customized fields on this entity, here is my solution:
A helper class:
class CalloutHelper
{
private static System.Collections.Hashtable _FlagDictionary = Hashtable.Synchronized(new Hashtable());
public static void SetBypassCalloutFlag(string entityGUID, string userGUID)
{
string flagKey = GenerateFlagKey(entityGUID,userGUID);
if(!_FlagDictionary.ContainsKey(flagKey))
{
lock (_FlagDictionary.Keys.SyncRoot)
{
_FlagDictionary.Add(flagKey, null);
}
}
}
public static void ResetBypassCalloutFlag(string entityGUID, string userGUID)
{
string flagKey = GenerateFlagKey(entityGUID, userGUID);
if (_FlagDictionary.ContainsKey(flagKey))
{
lock (_FlagDictionary.Keys.SyncRoot)
{
_FlagDictionary.Remove(flagKey);
}
}
}
public static bool IsBypassCallout(string entityGUID, string userGUID)
{
string flagKey = GenerateFlagKey(entityGUID, userGUID);
return _FlagDictionary.ContainsKey(flagKey);
}
private static string GenerateFlagKey(string entityGUID, string userGUID)
{
return entityGUID + "&" + userGUID;
}
}
At the very beginning of PostUpdate event handler, use the helper class to check if this event is bypassed
public override void PostUpdate(CalloutUserContext userContext, CalloutEntityContext entityContext,
string preImageEntityXml, string postImageEntityXml)
{
if (CalloutHelper.IsBypassCallout(entityContext.InstanceId.ToString(), userContext.UserId.ToString()))
{
CalloutHelper.ResetBypassCalloutFlag(entityContext.InstanceId.ToString(), userContext.UserId.ToString());//reset flag
return;//skip this callouts as it is flaged to be bypassed
}
// your PostUpdate logic goes there
// code to call CRM web service to update this entity
CalloutHelper.SetBypassCalloutFlag(entityContext.InstanceId.ToString(), userContext.UserId.ToString());//set flag
}
You may have found that the key of the flag is based on combination of entity GUID and user GUID, this is assuming that one user cannot update the same entity twice simutaniously
[Update 2007-10-22]: In callouts code when you want to update a CRM entity through the CRM web service, please ensure you use impersonation to call the web service. This will make sure you get correct userContext.UserId to make above code work. - see how to do impersonation http://msdn2.microsoft.com/en-us/library/aa682071.aspx
- MS CRM Callouts Tip - Avoid Dead Loop in Callouts铪
- MS CRM Callouts Tip - Avoid Dead Loop in Callouts铪
- MS CRM Callouts Tip - Avoid Dead Loop in Callouts铪
- MS CRM Callouts Tip - Avoid Dead Loop in Callouts
- MS CRM Callouts Tip - Debug and Development铪铪铪铪
- MS CRM Callouts Tip - Debug and Development铪铪铪铪
- MS CRM Callouts Tip - Debug and Development铪铪铪铪
- MS CRM Callouts Tip - 调用WCF service in CRM 3 Callouts (.net 1.1)
- MS CRM Callouts Tip - 调用WCF service in CRM 3 Callouts (.net 1.1)
- MS CRM Callouts Tip - 调用WCF service in CRM 3 Callouts (.net 1.1)
- MS CRM Callouts Tip - 调用WCF service in CRM 3 Callouts (.net 1.1)
- MS CRM Callouts Tip - 在Callouts中调用log4net
- MS CRM Callouts Tip - 在Callouts中调用log4net
- MS CRM Callouts Tip - 在Callouts中调用log4net
- MS CRM Callouts Tip - 在Callouts中调用log4net
- MS CRM Callouts Tip - Debug and Development
- 自定义callouts part 1
- 自定义callouts part 2
- MOICE, 微软发布的OFFICE最新安全功能
- MS CRM Callouts Tip - 调用WCF service in CRM 3 Callouts (.net 1.1)
- EJB3.0 (序)
- Eclipse Action 6
- Printing out your W2 Form using C# and .NET
- MS CRM Callouts Tip - Avoid Dead Loop in Callouts铪
- 项目经理的角色(Roles of Project Manager)
- SWTSwing 和SwingUI 初探
- 对反病毒产品你应该知道的几个事实
- MS CRM Callouts Tip - 在Callouts中调用log4net
- Redirecting Standard Input/Output using the Process Class铪
- commons-beanutils 1.7 新增的 LazyDynaBean
- [概念] 激励理论-海兹伯格激励理论(Herzberg's theory of motivation)
- EoS:Swing之上的Eclipse