我在执行某些操作的自定义实体的Create(同步,后期操作)上注册了一个CRM插件,我希望Create操作在插件中出现错误时成功。出于性能原因,我还希望在创建记录时立即触发插件,因此不希望使插件异步。我通过执行以下操作实现了这一点:
public class FooPlugin : IPlugin
{
public FooPlugin(string unsecureInfo, string secureInfo) { }
public void Execute(IServiceProvider serviceProvider)
{
try
{
// Boilerplate
var context = (IPluginExecutionContext) serviceProvider.GetService(typeof (IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof (IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Additional validation omitted
var targetEntity = (Entity) context.InputParameters["Target"];
UpdateFrobber(service, (EntityReference)targetEntity["new_frobberid"]);
CreateFollowUpFlibber(service, targetEntity);
CloseTheEntity(service, targetEntity);
}
catch (Exception ex)
{
// Send an email but do not re-throw the exception
// because we don't want a failure to roll-back the transaction.
try
{
SendEmailForException(ex, context);
}
catch { }
}
}
}
但是,当发生错误时(例如在UpdateFrobber(...)
中),服务客户端会收到此异常:
System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]:
There is no active transaction. This error is usually caused by custom plug-ins
that ignore errors from service calls and continue processing.
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ref ProxyRpc rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(ref MessageData msgData, Int32 type)
at Microsoft.Xrm.Sdk.IOrganizationService.Create(Entity entity)
at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.CreateCore(Entity entity)
at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Create(Entity entity)
at Microsoft.Xrm.Client.Services.OrganizationService.<>c__DisplayClassd.<Create>b__c(IOrganizationService s)
at Microsoft.Xrm.Client.Services.OrganizationService.InnerOrganizationService.UsingService(Func`2 action)
at Microsoft.Xrm.Client.Services.OrganizationService.Create(Entity entity)
at MyClientCode() in MyClientCode.cs: line 100
我的猜测是,这是因为UpdateFrobber(...)
使用从插件派生的IOrganizationService实例,因此任何CRM服务调用它都会参与与插件相同的事务,如果这些“子”操作失败,它会导致整个事务回滚。它是否正确?是否有一种“安全”方法可以忽略同步插件中“子”操作的错误?也许是一种实例化不重用插件上下文的IOrganizationService实例的方法?
如果相关,我们将在本地运行CRM 2013。
答案 0 :(得分:2)
当您的插件参与数据库事务时,您无法忽略来自子插件的未处理异常。
但是,当您的插件在部分受信任模式下运行On Premise时,您实际上可以创建自己的OrganizationServiceProxy
实例并使用它来访问CRM。请务必参考插件正在执行的服务器,以避免出现“双跳”问题。
答案 1 :(得分:0)
如果确实需要,我可以使用ContinueOnError = true创建ExecuteMultipleRequest,对于您的电子邮件,您只需检查ExecuteMultipleResponse ...
但是看起来有点矫kill过正。
答案 2 :(得分:0)
如果在异步模式下运行,您可以捕获异常。捕获异常时请务必验证您的模式。
示例代码:
try
{
ExecuteTransactionResponse response =
(ExecuteTransactionResponse)service.Execute(exMultReq);
}
catch (Exception ex)
{
errored = true;
if (context.Mode == 0) //0 sync, 1 Async.
throw new InvalidPluginExecutionException(
$"Execute Multiple Transaction
Failed.\n{ex.Message}\n{innermessage}", ex);
}
if(errored == true)
{
//Do more stuff to handle it, such as Log the failure.
}
同步插件不可能这样做。
可以在我的博客上找到更详细的总结,解释执行模式和用例:https://helpfulbit.com/handling-exceptions-in-plugins/
干杯。