glo*_*rob 5 extension-methods typeloadexception dynamics-crm-2011
我正在为CRM Online试用租户上的客户端编写一个插件(假设它有最新的补丁等)并且遇到了我以前从未见过的错误.一般来说,我总是使用下面的扩展方法,只是为了清晰的代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我觉得那里没有什么大的争议吗?无论如何,如果我将类文件作为此客户端插件的一部分包含在内,我会抛出以下错误:
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]
Run Code Online (Sandbox Code Playgroud)
没有包含跟踪,这表明插件甚至没有执行(即使第一行代码抛出异常!).
我做了一些挖掘,似乎至少对于这个客户端/实例: - 如果我public static class Foo用任何方法包含一个静态类文件(),我得到这个错误,无论该类是否实际被代码使用 - 当生成错误,插件本身不执行(异常发生在任何代码之前)
之前有人见过这样的事情或对System.TypeLoadException异常有任何见解吗?
我刚试过这个插件与CRM Online试用版(5.0.9690.3358)并且正在运行.
该插件在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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这可以肯定问题不是扩展方法.
我最好的猜测(按顺序):