Dapper called in application, and called in DLL is throwing a MissingMethodException

c# dapper dll missingmethodexception

Question

I'm wondering why my application that is referencing a dll I created that also uses Dapper is failing. I get a Method not found: 'System.Collections.Generic.IEnumerable'1<!!0> Dapper.SqlMapper.Query(System.Data.IDbConnection, System.String, System.Object)'. error message. When I track this down to the offending code it appears to be in DotPdfInvoideLayout.dll @ InvoiceManager.LoadData() Here below is the code for the method that is failing, because I'm calling this as a dll, the Stack Trace points to the last curly brace of the method. Line 1988. I am assuming that my real problem is the line that makes the call to Query()

public void loadData(IEnumerable<IPdfRenderable> textBoxes)
{
    var conn = new SqlConnection("Server=something;DataBase=TRIMS;UID=user;Password=password;");
    var output = conn.Query<TRIMNameAddressMaster>("Select top 1 * from Trims.dbo.TRIMNAMEADDRESSMASTER where id =  '" + _transaction.strap + "'", null).FirstOrDefault();

    var type = output.GetType();

    var properties = type.GetProperties();


    var r = new System.Text.RegularExpressions.Regex(@"((?<=\{)[^}]*(?=\}))+");



    foreach (var textbox in textBoxes)
    {

        var matches = r.Matches(((PdfTextBox)textbox).Text);

        foreach (Match match in matches)
        {
            try
            {
                var p = properties.Single(pi => pi.Name.ToLower() == match.Value.ToLower());
                ((PdfTextBox)textbox).Text = ((PdfTextBox)textbox).Text.Replace(
                    "{" + match.Value + "}", (p.GetValue(output, null) ?? string.Empty).ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("No Mapping occurred" + match.Value);
            } 
        }

    }
}

As a console application DotPdfInvoiceLayout runs perfectly fine. I removed the Main() and changed the project properties to run this as a Class Library then copied the generated dll into the bin of my web application and referenced the dll in the web project.

I've tried to make sure both are using the same version of Dapper.

Accepted Answer

This sounds like one of the projects is referencing the down-level 3.5 library, and one is referencing the up-level 4.0/4.5 library. We intentioanally have the .NET 3.5 project configured to only use C# 3.0 syntax - although in some ways this is actually a legacy thing from "back in the day" when dapper was deployed as a code file rather than an assembly. As a consequence of using C# 3.0, it doesn't have the same level of support for optional parameters, so it uses overloads instead. These overloads have simply never existed in the 4.0/4.5 library. Due to this, the 3.5 project is not directly interchangeable with the 4.0 project.

Change the projects so that they are both targeting the same .net level in terms of "dapper", and it should work.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why