如何将SqlException类型添加到自定义跟踪参与者?

时间:2011-03-04 14:25:52

标签: .net workflow workflow-foundation-4

我在工作流程跟踪中遇到以下(间歇性)错误:

  

“发生错误   呼叫跟踪参与者导致   要中止的实例。见   更多细节的内部异常。   InnerException消息:类型   'System.Data.SqlClient.SqlException'   与数据合同名称   'SQLEXCEPTION:HTTP://schemas.datacontract.org/2004/07/System.Data.SqlClient'   不是预期的。考虑使用   DataContractResolver或添加任何类型   静态地不知道列表   已知类型 - 例如,通过使用   KnownTypeAttribute属性或   将它们添加到已知类型列表中   传递给   DataContractSerializer的“。

我的自定义跟踪参与者看起来像这样:

            protected override void Track (TrackingRecord record, TimeSpan timeout)
            {

              StringWriter sw = null;
              if (record == null) throw new ArgumentNullException("record");
              if (timeout == null) throw new ArgumentNullException("timeout");
              StringBuilder sb = new StringBuilder();
              sw = new StringWriter(sb);

              using (XmlTextWriter writer = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
              {
                  DataContractSerializer serializer = new DataContractSerializer(record.GetType());
                  serializer.WriteObject(writer, record);
                  writer.Flush();
                  writer.Close();
              }


              _logger.Log(sb.ToString());
            }

我该如何解决这个问题?

此外,还有其他类型的工作流程可能会向我提出我还没有看到但需要在跟踪参与者中处理的内容吗?

1 个答案:

答案 0 :(得分:2)

在此处找到解决方案:http://blogs.msdn.com/b/youssefm/archive/2009/06/05/introducing-a-new-datacontractserializer-feature-the-datacontractresolver.aspx

创建自定义DCR:

public class SharedTypeResolver : DataContractResolver
{
    public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
    {    
        if (!knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace))
        {
            XmlDictionary dictionary = new XmlDictionary();
            typeName = dictionary.Add(dataContractType.FullName);
            typeNamespace = dictionary.Add(dataContractType.Assembly.FullName);
        }
        return true;
    }


    public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
    {
        return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null) ?? Type.GetType(typeName + ", " + typeNamespace);
    }
}

在我的代码中更改DCS行:

DataContractSerializer serializer = new DataContractSerializer(record.GetType(), null, Int32.MaxValue, false, false, null, new SharedTypeResolver()

完成工作。