我已经为我们的报价产品注册了一个插件。该插件在我们的测试环境中运行良好。我已经测试过很多次了。然后在主服务器中注册插件。但是,会出现以下情况:
当我首先创建或更新报价产品时,报价产品表单会变灰:
单击报价单后,出现错误。没有可用的日志文件(如您所见)。我调试了该插件,但也没有错误。单击“确定”后,错误消失,并且在报价产品(针对税务字段)上发生所需的业务。意味着插件的代码没有错误并且运行良好。代码如下:
using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Xrm;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk.Deployment;
public class Plugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
Entity entity;
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "quotedetail") { return; }
}
else
{
return;
}
try
{
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(
typeof(IOrganizationServiceFactory));
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
if (context.MessageName == "Create")
{
Entity QuoteProduct = (Entity)context.InputParameters["Target"];
Guid QPID = QuoteProduct.Id;
TaxCreator(service, QPID);
}
if (context.MessageName == "Update" && context.Depth < 3)
{
Entity QuoteProduct = (Entity)context.PostEntityImages["Target"];
Guid QPID = QuoteProduct.Id;
TaxUpdater(service, QPID);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(
"An error occurred in the plug-in.", ex);
}
}
private static void TaxCreator(IOrganizationService service, Guid QPID)
{
using (var crm = new XrmServiceContext(service))
{
var QuoteProduct = crm.QuoteDetailSet.Where(c => c.QuoteDetailId == QPID).First();
var SaleSetting = crm.new_salessettingSet.Where(c => c.statecode == 0).First();
double TaxPercent = (Convert.ToDouble(SaleSetting.new_TaxPercent) / 100);
if (QuoteProduct.IsPriceOverridden == false)
{
decimal Tax = (decimal)Convert.ToDecimal(Convert.ToDouble(QuoteProduct.BaseAmount - QuoteProduct.ManualDiscountAmount.GetValueOrDefault()) * TaxPercent);
decimal PricePerUnit = (decimal)(QuoteProduct.PricePerUnit.GetValueOrDefault() - QuoteProduct.VolumeDiscountAmount.GetValueOrDefault());
crm.UpdateObject(QuoteProduct);
crm.SaveChanges();
QuoteProduct.Attributes["tax"] = new Money(Tax);
QuoteProduct.Attributes["new_result"] = new Money(PricePerUnit);
crm.UpdateObject(QuoteProduct);
crm.SaveChanges();
}
}
}
private static void TaxUpdater(IOrganizationService service, Guid QPID)
{
using (var crm = new XrmServiceContext(service))
{
var QuoteProduct = crm.QuoteDetailSet.Where(c => c.QuoteDetailId == QPID).First();
var SaleSetting = crm.new_salessettingSet.Where(c => c.statecode == 0).First();
double TaxPercent = (Convert.ToDouble(SaleSetting.new_TaxPercent) / 100);
if (QuoteProduct.IsPriceOverridden == false)
{
decimal Tax = (decimal)Convert.ToDecimal(Convert.ToDouble(QuoteProduct.BaseAmount - QuoteProduct.ManualDiscountAmount.GetValueOrDefault()) * TaxPercent);
decimal PricePerUnit = (decimal)(QuoteProduct.PricePerUnit.GetValueOrDefault() - QuoteProduct.VolumeDiscountAmount.GetValueOrDefault());
crm.UpdateObject(QuoteProduct);
crm.SaveChanges();
QuoteProduct.Attributes["tax"] = new Money(Tax);
QuoteProduct.Attributes["new_result"] = new Money(PricePerUnit);
crm.UpdateObject(QuoteProduct);
crm.SaveChanges();
}
}
}
}
我还检查了服务器上的事件查看器是否有错误,但没有结果!
我已注册了用于创建和更新报价产品的插件。
任何帮助是极大的赞赏。
我会对其进行霰弹枪调试。实施以下跟踪。通常,我将跟踪器的声明放置为类成员(因此它对类中的所有帮助方法都是可见的),并在每次操作(或多或少)之后写入它(当绝望和沮丧时)。然后,当程序自行崩溃时(或者当我故意崩溃以查看跟踪日志时),我通常可以查明问题区域。
public class MyPlugin _ IPlugin
{
private ITracingService _trace;
public void Execute(IServiceProvider service)
{
_trace = (ITracingService)service.GetService(typeof(ITracingService));
_trace.Trace("Commencing.");
...
_trace.Trace("Right before an operation. " + someValue);
PerformAnOperation();
_trace.Trace("Right before an other operation. " + someOtherValue);
PerformAnOtherOperation();
...
throw new Exception("Intentional!");
}
}
然后,您可以继续查看到底问题出在哪里。根据我的经验,一旦人们知道where很痛苦,人们可以很容易地建议如何解决这个问题。
EDIT:
由于OP要求更多细节,我正在获取他的代码并为他将跟踪算法放入其中。有点多余,但显然,CRM 可能非常令人困惑。我自己也去过那里。
public class Plugin : IPlugin
{
// Here we declare our tracer.
private ITracingService _trace;
public void Execute(IServiceProvider serviceProvider)
{
// Here we create it.
_trace = (ITracingService)serviceProvider
.GetService(typeof(ITracingService));
_trace.Trace("Commencing.");
// Here we crash our plugin just to make sure that we can see the logs
// with trace information. This statement should be gradually moved down
// the code to discover where exactly the plugin brakes.
throw new Exception("Intentional!");
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider
.GetService(typeof(IPluginExecutionContext));
Entity entity;
...
try { ... }
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(
"An error occurred in the plug-in.", ex);
}
// Here we catch all other kinds of bad things that can happen.
catch(Exception exception) { throw exception; }
}
private static void TaxCreator(IOrganizationService service, Guid QPID)
{
// Here we add a line to the trace hoping that the execution gets here.
_trace.Trace("Entered TaxCreator.");
using (var crm = new XrmServiceContext(service))
{
var QuoteProduct = crm.QuoteDetailSet.Where(...).First();
var SaleSetting = crm.new_salessettingSet.Where(...).First();
double TaxPercent = (Convert.ToDouble(...) / 100);
// Here we add a line to the trace to see if we get past this point.
_trace.Trace("Approaching if statement.");
if (QuoteProduct.IsPriceOverridden == false) { ... }
}
}
private static void TaxUpdater(IOrganizationService service, Guid QPID)
{
// Here we add a line to the trace hoping that the execution gets here.
_trace.Trace("Entered TaxUpdater.");
...
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)