【问题标题】:Serialization of MS CRM 2011 plugin execution context to JSON将 MS CRM 2011 插件执行上下文序列化为 JSON
【发布时间】:2014-02-10 09:48:01
【问题描述】:

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

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

Newtonsoft.Json.JsonSerializationException: Error getting value from 'ServiceProvider' on 'Plugin+LocalPluginContext

插件由 Visual Studio 插件生成。代码放在基类Plugin 中,如下所示:

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 无法序列化某些类型。但我无法确定这个限制。

您能帮我纠正一下方法吗?

【问题讨论】:

    标签: c# json plugins dynamics-crm-2011 json.net


    【解决方案1】:

    我想做一些类似的事情,但不是用 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();
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 是的,我是,但我想提出更有用的单元测试方法,比crm2011plugintest.codeplex.com 更好。如果我成功了,我将在 Kiev CRM UG 上分享我的结果。
      【解决方案3】:

      这对我来说效果更好 http://www.crmanswers.net/2015/02/json-and-crm-sandbox-plugins.html

      string parameter1 = "";
      using (MemoryStream memoryStream = new MemoryStream())
      {
          DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Course));
          serializer.WriteObject(memoryStream, course);
          parameter1 = Encoding.Default.GetString(memoryStream.ToArray());
      }
      

      【讨论】:

      • 我设法使这段代码工作,通过它要求我将[DataContract] 属性添加到类定义和[DataMember] 到它的所有公共字段。
      猜你喜欢
      • 2014-03-26
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多