ServiceStack.OrmLite:ServiceStack.Text.AssemblyUtils.ToTypeString(Type type)中的NullReferenceException

时间:2018-07-27 12:36:56

标签: servicestack ormlite-servicestack

当我继续测试OrmLite时,我遇到了另一个问题,这个问题似乎发生在ServiceStack.Text.AssemblyUtils中:

System.NullReferenceException: Object reference not set to an instance of an object.
   at ServiceStack.Text.AssemblyUtils.ToTypeString(Type type)
   at ServiceStack.Text.Common.JsWriter`1.WriteType(TextWriter writer, Object value)
   at ServiceStack.Text.Common.WriteListsOfElements`2.<>c__DisplayClass1_0.<.cctor>b__0(TextWriter writer, Object obj)
   at ServiceStack.Text.Common.WriteListsOfElements`2.WriteGenericIList(TextWriter writer, IList`1 list)
   at ServiceStack.Text.TypeSerializer.SerializeToString(Object value, Type type)
   at ServiceStack.Text.TypeSerializer.SerializeToString[T](T value)
   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.GetFieldValue(FieldDefinition fieldDef, Object value)
   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.GetValueOrDbNull[T](FieldDefinition fieldDef, Object obj)
   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.SetParameterValue[T](FieldDefinition fieldDef, IDataParameter p, Object obj)
   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.SetParameterValues[T](IDbCommand dbCmd, Object obj)
   at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.Insert[T](IDbCommand dbCmd, T obj, Action`1 commandFilter, Boolean selectIdentity)
   at ServiceStack.OrmLite.UntypedApi`1.<>c__DisplayClass16_0.<Insert>b__0(IDbCommand dbCmd)
   at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func`2 filter)
   at tWorks.Core.CoreServerCommons.Handlers.OrmLiteDbHandler.<>c__DisplayClass46_0.<AddCoreObjectToDatabase>b__0(IDbConnection db) in D:\[GIT]\Core\CoreServerCommons\Handlers\DbHandlers\OrmLite\OrmLiteDbHandler.cs:line 253

当我使用无类型的API插入对象时会发生这种情况,

long insertedId = dbCluster.GetDbAndRun((IDbConnection db) =>
{
    coreObject.Id = id;
    var typedApi = db.CreateTypedApi(coreObject.GetType());
    return typedApi.Insert(coreObject, selectIdentity: true);
});

我要插入的'coreObject':

public class ModuleController : Actor
{
    public ModuleController(string username, List<Type> contactTypes)
    {
        this.Username = username;
        this.ContactTypes = contactTypes;

        this._AllowedLoginChannels = new List<Type>();
        this._AllowedLoginChannels.Add(typeof(CoreClient));
    }
    public List<Type> ContactTypes { get; set; }
}

我设法弄清了NullRef的来源:

enter image description here

基本上,如果NULL列表中有一个ContactTypes条目,则失败如上所述。

现在,在这种情况下,列表不应该包含NULL值,但是如果我没有在ServiceStack中深入了解NullRef会感到好多,这会妨碍我持久化该对象。也许我要在那里NULL? 它只是在列表中,还是词典不能具有NULL值?持久化时NULL还有什么不能?

Shouldnt ServiceStack只能将其处理为null,因此以其JSON格式,它简单地是null

1 个答案:

答案 0 :(得分:1)

OrmLite默认情况下使用JSV格式序列化复杂类型的Blob,这是一个空格敏感的格式,使用CSV格式,该格式不包含null数据类型,因为该数据类型会被推断为“空”字符串,相反,任何空属性都将简单地从连线格式中排除,这与反序列化时保留默认属性值具有相同的作用。

如果您想在序列化数据中包含空值,则可以change the text serializer used to serialize complex types,例如:

PostgreSqlDialect.Provider.StringSerializer = new JsonStringSerializer();