C# 将MS CRM 2011插件执行上下文序列化为JSON

C# 将MS CRM 2011插件执行上下文序列化为JSON,c#,json,plugins,dynamics-crm-2011,json.net,C#,Json,Plugins,Dynamics Crm 2011,Json.net,我正在尝试将MS CRM 2011插件执行上下文序列化为JSON,以便稍后对其进行分析(我需要找到错误的问题,我更希望有更易于人类阅读的上下文转储形式) 但我的所有尝试都会导致序列化错误,如下所示: Newtonsoft.Json.JsonSerializationException:从“Plugin+localpluginText”上的“ServiceProvider”获取值时出错 插件是由VisualStudio附加组件生成的。代码放在基类插件中,如下所示: internal void Tr

我正在尝试将MS CRM 2011插件执行上下文序列化为JSON,以便稍后对其进行分析(我需要找到错误的问题,我更希望有更易于人类阅读的上下文转储形式)

但我的所有尝试都会导致序列化错误,如下所示:

Newtonsoft.Json.JsonSerializationException:从“Plugin+localpluginText”上的“ServiceProvider”获取值时出错

插件是由VisualStudio附加组件生成的。代码放在基类插件中,如下所示:

internal void Trace(string message)
{
    if (string.IsNullOrWhiteSpace(message) || this.TracingService == null)
    {
        return;
    }

    if (this.PluginExecutionContext == null)
    {
        this.TracingService.Trace(message);
    }
    else
    {
        this.TracingService.Trace(
            "{0}, Correlation Id: {1}, Initiating User: {2}",
            message,
            this.PluginExecutionContext.CorrelationId,
            this.PluginExecutionContext.InitiatingUserId);

        var jss = new JsonSerializerSettings();
        var dcr = new DefaultContractResolver();

        dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
        jss.ContractResolver = dcr;

        this.TracingService.Trace("Local Context Dump: {0}", JsonConvert.SerializeObject(this, jss));
    }
}
我认为问题在于一般做法
LocalContext
是一个非常复杂的对象。JSON.NET无法序列化某些类型。但我无法确定这一限制


你能帮我纠正这个方法吗?

你知道插件调试器的方法吗?重新检查,它描述了如何调试甚至位于CRM Online中的插件。

我想做一些类似的事情,但不是使用JSON,而是使用自己的C#方法将其转换为字符串,我只是将其添加到日志中:这里是C#函数,如果它有帮助的话

protected String GetPluginExecutionInfo(IPluginExecutionContext context)
{
    var lines = new List<String>();
    var target = GetTarget<Entity>(context);

    lines.Add("MessageName: " + context.MessageName);
    lines.Add("PrimaryEntityName: " + context.PrimaryEntityName);
    lines.Add("PrimaryEntityId: " + context.PrimaryEntityId);
    lines.Add("BusinessUnitId: " + context.BusinessUnitId);
    lines.Add("CorrelationId: " + context.CorrelationId);
    lines.Add("Depth: " + context.Depth);
    lines.Add("Has Parent Context: " + (context.ParentContext != null));
    lines.Add("InitiatingUserId: " + context.InitiatingUserId);
    AddParameters(lines, context.InputParameters, "Input Parameters");
    lines.Add("IsInTransaction: " + context.IsInTransaction);
    lines.Add("IsolationMode: " + context.IsolationMode);
    lines.Add("Mode: " + context.Mode);
    lines.Add("OperationCreatedOn: " + context.OperationCreatedOn);
    lines.Add("OperationId: " + context.OperationId);
    lines.Add("Organization: " + context.OrganizationName + "(" + context.OrganizationId + ")");
    AddParameters(lines, context.OutputParameters, "Output Parameters");
    AddEntityReference(lines, context.OwningExtension, "OwningExtension");
    AddEntityImages(lines, context.PostEntityImages, "Post Entity Images");
    AddEntityImages(lines, context.PreEntityImages, "Pre Entity Images");
    lines.Add("SecondaryEntityName: " + context.SecondaryEntityName);
    AddParameters(lines, context.SharedVariables, "Shared Variables");
    lines.Add("Stage: " + context.Stage);
    lines.Add("UserId: " + context.UserId);

    if (target == null || target.Attributes.Count == 0)
    {
        lines.Add("Target: Empty ");
    }
    else
    {
        lines.Add("* Target " + target.ToEntityReference().GetNameId() + " *");
        lines.Add(target.ToStringAttributes("    Target[{0}]: {1}"));
    }

    lines.Add("* App Config Values *");
    foreach (var key in ConfigurationManager.AppSettings.AllKeys)
    {
        lines.Add("    [" + key + "]: " + ConfigurationManager.AppSettings[key]);
    }

    return String.Join(Environment.NewLine, lines);
}

private static void AddEntityReference(List<string> nameValuePairs, EntityReference entity, string name)
{
    if (entity != null)
    {
        nameValuePairs.Add(name + ": " + entity.GetNameId());
    }
}

private static void AddEntityImages(List<string> nameValuePairs, EntityImageCollection images, string name)
{
    if (images != null && images.Count > 0)
    {
        nameValuePairs.Add("** " + name + " **");
        foreach (var image in images)
        {
            if (image.Value == null || image.Value.Attributes.Count == 0)
            {
                nameValuePairs.Add("    Image[" + image.Key + "] " + image.Value + ": Empty");
            }
            else
            {
                nameValuePairs.Add("*   Image[" + image.Key + "] " + image.Value.ToEntityReference().GetNameId() + "   *
                nameValuePairs.Add(image.Value.ToStringAttributes("        Entity[{0}]: {1}"));
            }
        }
    }
    else
    {
        nameValuePairs.Add(name + ": Empty");
    }
}

private static void AddParameters(List<string> nameValuePairs, ParameterCollection parameters, string name)
{
    if (parameters != null && parameters.Count > 0)
    {
        nameValuePairs.Add("* " + name + " *");
        foreach (var param in parameters)
        {
            nameValuePairs.Add("    Param[" + param.Key + "]: " + param.Value);
        }
    }
    else
    {
        nameValuePairs.Add(name + ": Empty");
    }
}

public static String ToStringAttributes(this Entity entity, string attributeFormat = "[{0}]: {1}")
{
    return String.Join(Environment.NewLine, entity.Attributes.Select(att => 
        String.Format(attributeFormat, att.Key, GetAttributeValue(att.Value))));
}

private static string GetAttributeValue(object value)
{
    if (value == null)
    {
        return "Null";
    }

    var osv = value as OptionSetValue;
    if (osv != null)
    {
        return osv.Value.ToString();
    }

    var entity = value as EntityReference;
    if (entity != null)
    {
        return entity.GetNameId();
    }

    return value.ToString();
}
受保护的字符串GetPluginExecutionInfo(IPluginExecutionContext上下文)
{
变量行=新列表();
var target=GetTarget(上下文);
添加(“MessageName:+context.MessageName”);
添加(“PrimaryEntityName:+context.PrimaryEntityName”);
添加(“PrimaryEntityId:+context.PrimaryEntityId”);
添加(“BusinessUnitId:+context.BusinessUnitId”);
添加(“CorrelationId:+context.CorrelationId”);
添加(“深度:+context.Depth”);
Add(“具有父上下文:”+(Context.ParentContext!=null));
添加(“InitiatingUserId:+context.InitiatingUserId”);
AddParameters(行,context.InputParameters,“输入参数”);
添加(“IsInTransaction:+context.IsInTransaction”);
添加(“IsolationMode:+context.IsolationMode”);
添加(“模式:+context.Mode”);
添加(“OperationCreatedOn:+context.OperationCreatedOn”);
添加(“OperationId:+context.OperationId”);
添加(“组织:“+context.OrganizationName+”(“+context.OrganizationId+”));
AddParameters(行,context.OutputParameters,“输出参数”);
AddEntityReference(行,context.OwningExtension,“OwningExtension”);
AddEntityImages(行、context.PostEntityImages、“Post实体图像”);
AddEntityImages(行、context.PreEntityImages、“实体前图像”);
添加(“SecondaryEntityName:+context.SecondaryEntityName”);
添加参数(行、context.SharedVariables、“共享变量”);
添加(“Stage:+context.Stage”);
添加(“UserId:+context.UserId”);
if(target==null | | target.Attributes.Count==0)
{
行。添加(“目标:空”);
}
其他的
{
添加(“*Target”+Target.ToEntityReference().GetNameId()+“*”);
Add(target.toString属性(“target[{0}]:{1}”);
}
行。添加(“*应用程序配置值*”);
foreach(ConfigurationManager.AppSettings.AllKeys中的变量键)
{
行。添加(“[”+key+”]:“+ConfigurationManager.AppSettings[key]);
}
返回String.Join(Environment.NewLine,lines);
}
私有静态void AddEntityReference(列表名称值对、EntityReference实体、字符串名称)
{
如果(实体!=null)
{
nameValuePairs.Add(name+“:“+entity.GetNameId());
}
}
私有静态void AddEntityImages(列表名称值对、EntityImageCollection图像、字符串名称)
{
if(images!=null&&images.Count>0)
{
nameValuePairs。添加(“**”+名称+“**”);
foreach(图像中的var图像)
{
if(image.Value==null | | image.Value.Attributes.Count==0)
{
nameValuePairs.Add(“Image[”+Image.Key+“]”“+”Image.Value+”:Empty”);
}
其他的
{
nameValuePairs.Add(“*Image[”+Image.Key+”]“+Image.Value.ToEntityReference().GetNameId()+”*
Add(image.Value.toString属性(“实体[{0}]:{1}”);
}
}
}
其他的
{
nameValuePairs.Add(name+“:Empty”);
}
}
私有静态void AddParameters(列表名称值对、参数集合参数、字符串名称)
{
if(parameters!=null&¶meters.Count>0)
{
nameValuePairs。添加(“*”+名称+“*”);
foreach(参数中的var参数)
{
nameValuePairs.Add(“参数[“+Param.Key+”]:“+Param.Value”);
}
}
其他的
{
nameValuePairs.Add(name+“:Empty”);
}
}
公共静态字符串toString属性(此实体,字符串attributeFormat=“[{0}]:{1}”)
{
返回String.Join(Environment.NewLine,entity.Attributes.Select(att=>
格式(attributeFormat、att.Key、GetAttributeValue(att.Value));
}
私有静态字符串GetAttributeValue(对象值)
{
如果(值==null)
{
返回“Null”;
}
var osv=作为选项SetValue的值;
如果(osv!=null)
{
返回osv.Value.ToString();
}
var entity=作为EntityReference的值;
如果(实体!=null)
{
返回实体.GetNameId();
}
返回值.ToString();
}
这对我来说效果更好


是的,我是,但我想提出一种比一种更有用的单元测试方法。如果我成功了,我将在基辅CRM UG上分享我的结果。我设法使这段代码工作,通过它,我需要在类定义中添加
[DataContract]
属性,并在其所有公共字段中添加
[DataMember]
string parameter1 = "";
using (MemoryStream memoryStream = new MemoryStream())
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Course));
    serializer.WriteObject(memoryStream, course);
    parameter1 = Encoding.Default.GetString(memoryStream.ToArray());
}