5

I am writing a plugin for a client on a CRM Online trial tenant (so assume it has latest patches etc.) and have come across an error I’ve not seen before. Generally speaking I always use an extension method along the lines of the following, just for clarity of code really:

public static void AddOrUpdate(this Entity e, string propertyName, object value)
{
    if (e.Attributes.Contains(propertyName))
    {
        e.Attributes[propertyName] = value;
    }
    else
    {
        e.Attributes.Add(propertyName, value);
    }
}

Nothing hugely controversial there I think? Anyway for whatever reason if I include the class file as part of a plugin for this client, I get the following error thrown:

Unhandled Exception: System.ServiceModel.FaultException`1
System.TypeLoadException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #9A0442A7

[foo.bar.Plugins: foo.bar.Plugins.TrackActivity]
[6ed535ec-c7a8-e211-858f-3c4a92dbdc37: foo.bar.Plugins.TrackActivity: Create of task]

There is no trace included, which shows the plugin isn’t even executed (even if the first line of code is throwing an exception!).

I did a bit of digging and it seems that for this client/instance at least: - If I include a static class file (public static class Foo) with any method, I get this error, whether the class is actually used by code or not - When the error is generated, the plugin itself is not executed (the exception occurs before any code)

Anyone seen anything like this before or have any insight into System.TypeLoadException exceptions?

4

2 回答 2

5

我刚刚使用 CRM Online 试用实例 (5.0.9690.3358) 尝试了这个插件,并且正在运行。

该插件注册在创建消息、任务实体、预操作、同步。

using System;
using Microsoft.Xrm.Sdk;

namespace TestPlugin
{
    public class MyPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName != "task")
                    return;

                try
                {
                    entity.AddOrUpdate("description", "updated by plugin");  
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }
    }

    public static class ExtensionMethods
    {
        public static void AddOrUpdate(this Entity e, string propertyName, object value)
        {
            if (e.Attributes.Contains(propertyName))
            {
                e.Attributes[propertyName] = value;
            }
            else
            {
                e.Attributes.Add(propertyName, value);
            }
        }

    }
}

这是为了确保问题不在于扩展方法。

我最好的猜测(按顺序):

  • 解决方案中的一个项目是使用 .NET Framework 4.5 编译的
  • 您正在使用旧的 SDK 版本
  • 您正在使用旧的插件注册工具
于 2013-05-04T09:01:52.540 回答
0

我们在客户服务器 (2008 R2) 上使用 CRM 2011 On-premises 时遇到了同样的问题。幸运的是,我们不必回到 .NET 4.0 - 在服务器上手动安装 4.5 解决了这个问题。希望在线 CRM 能够尽快更新。

于 2013-05-15T11:23:48.767 回答