Search code examples
c#wcfcompilation

Visual Studio states that there is an ambiguous call, when there's only a single method with the given name


I am trying to figure out why this is happening, but I can't. I have a WCF service and inside it I created a class, called ExtensionUtil which will consist of extension methods only.

For now the only method I have in the ExntensionUtil class is called AddWithNullableValue. It looks like this:

public static void AddWithNullableValue(this SqlParameterCollection paramCollection, string paramName, object value)
{
    SqlParameter param = new SqlParameter();

    param.ParameterName = paramName;
    param.Value = value == null ? DBNull.Value : value;

    paramCollection.Add(param);
}

I am using this method in the DAL like this:

sqlCommand.Parameters.AddWithNullableValue("@categoryId", publication.CategoryId);

Everything compiles fine. However, when I try to start the service a compilation error occurs, stating:

The call is ambiguous between the following methods or properties:
 'App_Code.Util.ExtensionUtil.AddWithNullableValue(System.Data.SqlClient.SqlParameterCollection, string, object)' and
 'App_Code.Util.ExtensionUtil.AddWithNullableValue(System.Data.SqlClient.SqlParameterCollection, string, object)'

Why is this happening? I have only one method with this name and the compiler says that the call is ambiguous.

Edit:

When I remove the 'this' keyword in front of the SqlParameterCollection paramater, making the method an ordinary static method, instead of an extension one, everything works okay. Moreover, I looked in the entire solution (using Ctrl+Shift+F) for all occurraces of the 'AddWithNullableValues' keyword and the search resulted in the following: the signature of the method and the two places in my code where I call it. I then even created a completely new static class, added the same method with a completely different name and the same body and I still got the same error when I called it, so the problem must be something else.

Finally, I needed another extension method and when I called it in my code I got the same error for it.

public static Nullable<T> GetNullableValue<T>(this SqlDataReader sqlDataReader, string columnName) where T : struct
{
    int columnIndex = sqlDataReader.GetOrdinal(columnName);

    Nullable<T> value = null;

    if (!sqlDataReader.IsDBNull(columnIndex))
    {
        value = (Nullable<T>)sqlDataReader.GetValue(columnIndex);
    }

    return value;
}

It seems to me that the solution has some problem with the extension methods and I don't know why. I built it successfully a couple of times, tried to clean it - everything works okay, until I try to use an extension method as an extension method, not as a static method.


Solution

  • I found the solution to this problem and it worked for me. It's quite disturbing but you can read the complete answer here.

    App_Code is not supported in a WAP. The App_Code folder is compiled at run-time; all code in a WAP is compiled at compile / development time. So, when you add an App_Code folder to a WAP. you end up with duplicate code; for example, a class defined in App_Code will also show up in the wap DLL. The fix is easy: just name the folder something else like Classes or CodeFiles.

    By the way, thanks to all of you who tried to help me :)