InvalidOperationEception with serialization with EF Power Tools

1.2k Views Asked by At

When executing the below code: The block of code being executed is with the stars

public static class DbContextExtensions { public static async Task<List> SqlQueryAsync(this DbContext db, string sql, object[] parameters = null, CancellationToken cancellationToken = default) where T : class { if (parameters is null) { parameters = new object[] { }; }

    **if (typeof(T).GetProperties().Any())
    {
        return await db.Set<T>().FromSqlRaw(sql, parameters).ToListAsync(cancellationToken);
    }**
    else
    {
        await db.Database.ExecuteSqlRawAsync(sql, parameters, cancellationToken);
        return default;
    }
}

}

I'm getting the following error: System.InvalidOperationException: The type 'System.Threading.ExecutionContext&' of property 'Context' on type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.Collections.Generic.List`1[SpiritWMSApiCore2.Models.Unitech_ASNInfo_BOLResult],SpiritWMSApiCore2.Models.SIRISContextProcedures+<Unitech_ASNInfo_BOLAsync>d__56]' is invalid for serialization or deserialization because it is a pointer type, is a ref struct, or contains generic parameters that have not been replaced by specific types.

I looked up with bing chat gpt and gives the following, but i still don't know how to resolve it... System.InvalidOperationException: The type 'System.Threading.ExecutionContext&' of property 'Context' on type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.Collections.Generic.List`1[SpiritWMSApiCore2.Models.Unitech_ASNInfo_BOLResult],SpiritWMSApiCore2.Models.SIRISContextProcedures+<Unitech_ASNInfo_BOLAsync>d__56]' is invalid for serialization or deserialization because it is a pointer type, is a ref struct, or contains generic parameters that have not been replaced by specific types.

1

There are 1 best solutions below

1
DaveDean1 On

For me, this was a missing await i.e. I was trying to serialise a Task<something> rather than a something. Serialising an uncompleted Task is obviously impossible

Related Questions in SYSTEM.THREADING.EXECUTIONCONTEXT