【问题标题】:CRM 2013 how to create entity when errorCRM 2013 出错时如何创建实体
【发布时间】:2015-05-19 10:53:06
【问题描述】:

我在 CRM 2013 中创建自定义错误记录器具有将错误信息保存到 CRM 实体中的功能。我调试了我的代码,发现我的代码运行良好。但问题是当CRM回滚事务时,日志实体也消失了。我想知道是否可以在 catch 块上创建实体并仍然抛出该错误?

public void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                ...
            }
            catch (Exception ex)
            {
                     IPluginExecutionContext context =
                  (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.
                    GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(Guid.Empty);

                var log = new Log
                {
                    Message = ex.Message
                };

                service.Create(log);
                throw;
            }
        }

【问题讨论】:

    标签: c# dynamics-crm


    【解决方案1】:

    我找到了解决此问题的另一种方法。我们可以创建新服务以在失败的事务之外创建新事务。如果你想做同样的事情,这里有一些 sn-p:

         try
                {
                    ...
                }
                catch (Exception ex)
                {
    
                 var HttpCurrentContext = HttpContext.Current;
               var  UrlBase = HttpCurrentContext.Request.Url.Host;
               string httpUrl = @"http://";
                if (HttpCurrentContext.Request.IsLocal)
                {
                    UrlBase += ":" + HttpCurrentContext.Request.Url.Port;
                }
                      if (!UrlBase.Contains(httpUrl))
                {
                    UrlBase = httpUrl + UrlBase;
                }
                var UriBase = UriBuilder(UrlBase.ToLowerInvariant().Trim() + "/xrmservices/2011/organization.svc").Uri;
    IServiceConfiguration<IOrganizationService> orgConfigInfo =
                   ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(UriBase);
                var creds = new ClientCredentials();
                using (_serviceProxy = new OrganizationServiceProxy(orgConfigInfo, creds))
                {
    
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                    _service = (IOrganizationService)_serviceProxy;
    
                     var log = new Log
                    {
                        Message = ex.Message
                    };
    
                    _service.Create(NewLog);
             }
                    throw;
                }
    

    【讨论】:

      【解决方案2】:

      基本上,不。您无法阻止异常回滚事务。在 StackOverflow 上查看类似问题。

      一种常见的方法是创建一个单独的日志服务,可以将日志存储在数据库事务之外。

      顺便说一句。 Dynamics CRM 2015 春季版引入了存储日志的功能,无论您的插件是否参与数据库事务。

      【讨论】:

      • 谢谢你明确地给我解决这个问题的想法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多