简体   繁体   English

CRM 2013错误时如何创建实体

[英]CRM 2013 how to create entity when error

I create custom error logger in CRM 2013 have functionality to save error information into CRM entity. 我在CRM 2013中创建自定义错误记录器,该功能具有将错误信息保存到CRM实体中的功能。 I debug my code and find that my code works well. 我调试了代码,发现我的代码运行良好。 But the problem is when CRM rollback the transaction, the log entity also disappear. 但是问题是当CRM回滚事务时,日志实体也消失了。 I want to know is it possible to create entity on catch block and still throw that error? 我想知道是否可以在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;
            }
        }

I found the other way to solve this issue. 我找到了解决此问题的另一种方法。 We can create new service to create new transaction outside the transaction being failed. 我们可以创建新服务,以在失败的事务之外创建新事务。 Here some snippet if you want to do the same: 如果您想执行以下操作,请查看以下代码段:

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

Essentially, no. 本质上没有。 You cannot prevent that an exception rolls back the transaction. 您不能阻止异常回滚事务。 See a similar question on StackOverflow . StackOverflow上看到类似的问题。

A common approach is to create a separate logging service that can store logs outside of the database transaction. 一种常见的方法是创建一个单独的日志记录服务,该服务可以将日志存储在数据库事务之外。

Btw Dynamics CRM 2015 spring release introduces the capability to store logs regardless if your plugin is participating in a database transaction. Btw Dynamics CRM 2015春季版本引入了存储日志的功能,无论您的插件是否参与数据库事务。

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

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