Extension Methods for CRM 2011 Online Instances Causing TypeLoad Exceptions

dynamics-crm-2011, extension-methods, typeloadexception

Solution

I just tried this plugin with a CRM Online trial instance (5.0.9690.3358) and is working.

The plugin is registered on Create message, Task entity, Pre-operation, Synchronous.

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

    }
}

This to be sure that the problem is not the extension method.

My best guess (in order):

- One project in your solution is compiled with .NET Framework 4.5

- You are using old SDK version

- You are using old Plugin Registration Tool

Problem

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?

Original source