繁体   English   中英

CRM 2013错误时如何创建实体

[英]CRM 2013 how to create entity when error

我在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;
            }
        }

我找到了解决此问题的另一种方法。 我们可以创建新服务,以在失败的事务之外创建新事务。 如果您想执行以下操作,请查看以下代码段:

     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;
            }

本质上没有。 您不能阻止异常回滚事务。 StackOverflow上看到类似的问题。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM