Drizin / InterpolatedSql

Sql Builder using Interpolated Strings
MIT License
106 stars 7 forks source link

Missing method targeting netstandard2.0 #17

Closed chadbergeron closed 1 week ago

chadbergeron commented 3 weeks ago

When using this package in a class library that uses multi-targeting (in my case it's .net standard 2.0 and .net 8), if a project consuming the class library is compatible with targeting .net standard 2.0 only, trying to use interpolatedsql.dapper throws an error at runtime:

System.MissingMethodException: Method not found: 'InterpolatedSql.Dapper.SqlBuilders.SqlBuilder InterpolatedSql.Dapper.IDbConnectionExtensions.SqlBuilder(System.Data.IDbConnection, System.FormattableString)'.

If i update the consuming class to multi-target both .net 8 and netstandard2.0 the error goes away and the method is able to be found.

let me know if you can't reproduce this with the multi-targeting i've mentioned and i'll try to pick apart our work app a bit more to see if i can create a working example for upload but this should be the most basic example:

Drizin commented 3 weeks ago

When InterpolatedSql is built for net6.0 or greater it has some overloads that use InterpolatedSqlHandler and does NOT have some overloads that take FormattableString.

So if your intermediate library is using netstandard2.0 it's probably using netstandard2.0\InterpolatedSql*dll, which uses FormattableString (no InterpolatedSqlHandler).

Then there's the Web project using NET8 and referencing the intermediary library. It should probably work fine (should use netstandard2.0\InterpolatedSql*dll). But looks like you're getting a conflict (intermediary library is trying to find overloads that take FormattableString but can't find them because the library available is probably the net6+ version).

I assume the problem is happening because your Web project (net8.0) also has (or had) a direct reference to InterpolatedSql package, am I right? If that's the case then Web project would have references to both net6.0\InterpolatedSql*dll (direct reference) and (netstandard2.0\InterpolatedSql*dll (indirect reference), and those would conflict (looks like your intermediate library is trying to use the overload that uses FormattableString but it's conflicting with the net6.0\InterpolatedSql*dll which does not have that overload).

When we're working with multitargeted projects (and dependencies) sometimes even after modifying the references there are still some leftovers in project.assets.json - so I suggest that you should manually delete bin and obj of each project (it's very helpful when working with multitargeted projects, clean isn't enough) and rebuild.

Let me know how it goes.

I just tested the scenario you described (even with the direct+indirect references) and I didn't had any problem.

chadbergeron commented 1 week ago

Thank you for taking the time to dig into that. There for sure is some direct Interpolated Sql refs throughout our project in both the web and other class libraries. I'll digest your comment above and see if I can make the necessary changes on my side to get it to work. I'll close this for now and If i'm seeing something different after I shuffle project references around I'll re-open with a reproduction project. Thanks again!