Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PlatformNotSupportedException throws when using Dapper with WP

Tags:

c#

dapper

uwp

Executing a UWP application in DEBUG works perfectly.

Using exactly the same code compiled in RELEASE crashes with this error message

System.PlatformNotSupportedException: 
    'Dynamic code generation is not supported on this platform.'

when executing this code (it's using Dapper 1.5.1 and System.Data.SQLite 1.0.109.2)

using (var c = NewConnection())
{
    var sql = @"
        update settings
        set
            ""value"" = @SetDate
        where ""key"" = 'week_date'";
    c.Execute(sql, new { SetDate = date }); //<= throws PlatformNotSupportedException 
                                            // only on RELEASE not in DEBUG
}

The application is UWP configured as below. Furthermore, the faulting code is a .NET Standard 2.0 Class Library

enter image description here

Why is it crashing on RELEASE only and how to fix it?

like image 740
JiBéDoublevé Avatar asked Jan 14 '19 15:01

JiBéDoublevé


1 Answers

Dapper is very deeply based on runtime IL generation, in ways that it would be basically impossible to change. Runtime IL generation is fundamentally not compatible with UWP.

There is no simple way of making this work.

So: to do this, you'd need to use something dapper-like-but-not-dapper, with one of two alternative implementations:

  • reflection-based binding (relatively slow, depending on how much data access you're doing)
  • compile-time code-gen of the missing pieces, presumably using some kind of roslyn analysis and partial class generation

Perhaps right now, the more pragamatic approach would be: don't use dapper in this case.

like image 151
Marc Gravell Avatar answered Oct 16 '22 22:10

Marc Gravell