奇怪的行为 CRM 2011 插件

2024-05-12

我已经为我们的报价产品注册了一个插件。该插件在我们的测试环境中运行良好。我已经测试过很多次了。然后在主服务器中注册插件。但是,会出现以下情况: 当我首先创建或更新报价产品时,报价产品表单会变灰:

单击报价单后,出现错误。没有可用的日志文件(如您所见)。我调试了该插件,但也没有错误。单击“确定”后,错误消失,并且在报价产品(针对税务字段)上发生所需的业务。意味着插件的代码没有错误并且运行良好。代码如下:

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(使用前将#替换为@)

奇怪的行为 CRM 2011 插件 的相关文章

  • 无法将匿名方法转换为类型“System.Windows.Threading.DispatcherPriority”,因为它不是委托类型

    谁能解释我需要做什么才能克服这个错误 无法将匿名方法转换为类型 System Windows Threading DispatcherPriority 因为它不是委托类型 private void Test object sender ba
  • 哪些 iomanip 操纵器具有“粘性”?

    我最近在创建一个stringstream由于我错误地假设std setw 会影响每次插入的字符串流 直到我明确更改它 然而 插入后它总是被取消设置 With timestruct with value of Oct 7 9 04 AM st
  • 如何在函数中将结构成员作为指针传递?

    问题是我有一个结构是另一个 主要 结构的成员 我编写了一个函数来清除第一个结构 它需要一个指向结构的指针 我想使用该函数来清除主要结构内的结构 但我不确切知道哪种方法是正确的 为了更好地解释它 这里有一些代码 我有一个结构 定义为 type
  • Rx Framework:在超时时执行操作,而不中断原始可观察序列

    给定一个可观察的源 通过轮询低级设备的 变化 状态生成 observable source metacode IObservable
  • 使用inotify监控文件

    我正在使用 inotify 来监视本地文件 例如使用 root temp inotify add watch fd root temp mask 删除该文件后 程序将被阻止read fd buf bufSize 功能 即使我创建一个新的 r
  • C++ 中的字符串到 LPCWSTR

    我正在尝试从字符串转换为 LPCWSTR 我使用多位 1 例如 LPCWSTR ToLPCWSTR string text LPCWSTR sw LPCWSTR text c str return sw 2 返回中文字符 LPCWSTR T
  • 获取光标相对于控件的位置 - C#

    我想获取鼠标相对于鼠标指针所在控件的位置 这意味着当我将光标置于控件的起点 左上角 时 它应该给出 0 0 我正在使用以下代码 private void panel1 MouseMove object sender MouseEventAr
  • C# 动态 Linq 变量Where 子句

    我正在按照 Scott Gu 的文章创建动态 LINQhttp weblogs asp net scottgu archive 2008 01 07 dynamic linq part 1 using the linq dynamic qu
  • 堆栈独立的C/C++蓝牙API?

    我想知道是否有适用于 Windows XP Vista 7 x86 和 x64 的堆栈独立 C C 蓝牙 api 我的目标是创建连接并通过蓝牙发送 接收一些时间关键的数据 我的研究给了我以下选择以及这项任务的缺点 用于蓝牙的 Windows
  • 模拟 EF core dbcontext 和 dbset

    我正在使用 ASP NET Core 2 2 EF Core 和 MOQ 当我运行测试时 我收到此错误 消息 System NotSupportedException 非虚拟 可在 VB 中重写 成员上的设置无效 x gt x Movies
  • 将 Uploadify 与 Sharepoint 和 .net 结合使用

    我在共享点页面上有一些由 JQuery 生成的 html 我想在这个 html 中使用 uploadify 将文件上传到服务器 亚历山大 https stackoverflow com users 25427 alexander gyosh
  • 如何在 asp .net mvc 2 中对不直接属于我的模型的对象使用 DisplayFor()?

    我确信我在这里遗漏了一些非常简单的东西 我创建了一个自定义日期时间显示模板 使用以下方法时效果很好 但是 我遇到了这样的情况 在部分控件内 我在 for 循环中迭代模型中的对象 我想要一个 DateTime 属性来使用显示模板 但我不知道如
  • 是否有更好(更简单)的方法来获取特定域 SID?

    我被指派修改 WinForms 应用程序 主要检查登录用户是否属于特定域 这是我到目前为止所想出的 byte domainSid var directoryContext new DirectoryContext DirectoryCont
  • 通过 Nuke.Common/NuGet.CommandLine 部署 NuGet 包时如何通过 Azure Auth

    我正在尝试通过 Azure DevOps 上的 Nuke 和 CI CD 自动执行 NuGet 包更新 一切都构建得很好 但在 PushNuGet 步骤中 该过程尝试通过弹出窗口向 Azure 进行身份验证 这显然从未在 in devops
  • 为什么 C# 编译的正则表达式比等效的字符串方法更快?

    每次我必须对字符串执行简单的包含或替换操作 其中我正在搜索的术语是固定值 时 我发现如果我获取示例输入并对其进行一些分析 则使用编译的正则表达式是几乎 总是比使用 String 类中的等效方法更快 我尝试过比较多种方法 hs是要搜索的 干草
  • C++ 静态工厂构造函数

    我正在进行模拟 它需要创建多个相当相似的模型 我的想法是有一个名为 Model 的类并使用静态工厂方法来构造模型 例如 模型 createTriangle or 模型 createFromFile 我从以前的 java 代码中汲取了这个想法
  • System.Drawing.Icon 构造函数抛出“操作成功完成”异常

    在 Windows XP 计算机上 以下代码抛出 System ComponentModel Win32Exception 并显示消息 操作成功完成 System Drawing Icon icon new System Drawing I
  • 如何创建浏览器插件?

    我必须创建一个插件 当用户将鼠标悬停在某些术语上时 该插件必须显示信息 谁能告诉我如何做的方向 我对创建插件没有太多想法 我知道我想要做的事情可以通过java脚本来完成 但是java脚本文件可以作为浏览器插件安装吗 任何对此的想法将不胜感激
  • 为 C++ 类播种 rand()

    我正在开发一个 C 类 它使用rand 在构造函数中 我真的希望这个班级在几乎所有方面都能照顾好自己 但我不知道在哪里播种rand 如果我播种rand 在构造函数中 每次构造我的对象类型的新实例时都会对其进行播种 因此 如果我按顺序创建 3
  • 访问 Visual Studio 扩展中的当前代码窗格

    我正在编写一个 Visual Studio 2010 扩展 在代码视图中带有右键单击菜单 我希望能够从菜单项事件处理程序检查当前代码 但无法在对象模型中找到执行此操作的位置 如何在 Visual Studio 扩展中访问当前窗口中的代码 E

随机推荐