[英]Error generating Entity Wrapper in Visual Studio 2013 with CRM developer tools
[英]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.