29

I have an extension method like

public static class Extension
{
    public static string GetTLD(this string str)
    {
        var host = new System.Uri(str).Host;
        int index = host.LastIndexOf('.'), last = 3;
        while (index >= last - 3)
        {
            last = index;
            index = host.LastIndexOf('.', last - 1);
        }
        var domain = host.Substring(index + 1);
        return domain;
    }
}

And I am calling this like

string domain = "." + _url.GetTLD();

I am getting no error at building and clean build.
But I am getting compilation error at run time error saying

The call is ambiguous between the following methods or properties: 'myIGNOU.Extension.GetTLD(string)' and 'myIGNOU.Extension.GetTLD(string)'

I swear that I don't have this extension method placed any where else too in the project. Why I am getting this error only at run time..?

But if I delete this method then I am getting error at build time not not at run time. Everything works fine without the code of this method.

Here is compilation error page

RobSiklos
  • 8,348
  • 5
  • 47
  • 77
shashwat
  • 7,851
  • 9
  • 57
  • 90
  • What is the type of _url? Also, have you tried deleting the method? – It'sNotALie. Jun 24 '13 at 19:02
  • @newStackExchangeInstance Its `string`. And Yes I have tried deleting this method. On deleting it gives error while building but not at run time – shashwat Jun 24 '13 at 19:04
  • 1
    Could you show **all** the exception details? – It'sNotALie. Jun 24 '13 at 19:06
  • 2
    How can you "get compilation error at run time"? That seems like a strange wording. Are you using the `dynamic` type with which the binding happens late? – Jeppe Stig Nielsen Jun 24 '13 at 19:06
  • @JeppeStigNielsen One can get Compilation Error at run time if there are syntax errors and you are trying to run the application. Same with me but except I am getting it only at run time not while builing – shashwat Jun 24 '13 at 19:12
  • It seems like you somehow have two copies of the project/assembly in which your `Extension` class is defined, at _binding-time_. – Jeppe Stig Nielsen Jun 24 '13 at 19:13
  • Interesting. Does changing the method name do anything? – Ben Reich Jun 24 '13 at 19:13
  • Have you read the thread http://stackoverflow.com/questions/1282636/the-call-is-ambiguous-between-the-following-methods-or-properties-bug — it seems to be a duplicate? – Jeppe Stig Nielsen Jun 24 '13 at 19:17
  • @BenReich Yes I have tried this too. That time ambiguity will be find b/w `'myIGNOU.Extension.GetTLD(string)' and 'myIGNOU.Extension_NewName.GetTLD(string)'` – shashwat Jun 24 '13 at 19:18
  • @newStackExchangeInstance I've added error details in question – shashwat Jun 24 '13 at 19:25
  • Are you somehow including the same assembly twice? I suspect that you have an outdated version of your assembly stashed away somewhere that you are building against. – Eric Lippert Jun 24 '13 at 19:26
  • @EricLippert I really don't know much about that. Where can I find these..? For now I have just one AssemblyInfo.cs file in Properties folder. I think it at run time it is compiling my project with the old copy of Code1.cs file where this class is written. – shashwat Jun 24 '13 at 19:32
  • @EricLippert changing server to the development server from IIS has same behavior. – shashwat Jun 24 '13 at 19:34
  • I've had this problem before, and it was because my project had two references to the same assembly (one regular assembly reference, and the other a project reference). Check the project references in Visual Studio to make sure this isn't happening for the assembly where `GetTLD()` is defined. – RobSiklos Jun 24 '13 at 19:54
  • Delete obj/bin folders, clean solution and rebuild. – asawyer Jun 24 '13 at 20:03
  • @RobSiklos I haven't added any reference but few third party libraries like HtmlAgility and Newtonsoft.Json. All other references are default. – shashwat Jun 24 '13 at 20:07
  • Can you try deleting the contents of all your "Temporary ASP.NET Files" folders (`%windir%\Microsoft.NET\Framework\\Temporary ASP.NET Files`) – RobSiklos Jun 24 '13 at 20:29
  • @RobSiklos I had already tried this. No luck – shashwat Jun 24 '13 at 20:39

6 Answers6

78

I had the same problem but for me it solved the problem to remove the own project from the project references. Resharper accidentally added a reference to the compiled binary of the same project. That way I had the same extension class 2 times within my project. During building it couldn't distinguish between the source-version or the binary-version of the extension class.

So basically: Check your project references if it contains a reference to itself.

Answer provided by @shashwat is also a case.

shashwat
  • 7,851
  • 9
  • 57
  • 90
Danielku15
  • 1,490
  • 1
  • 13
  • 29
  • 8
    I had "ambiguous" message on compilation too and your answer solved it for me. The project refer to itself when I check under References. removing it enable it to compile again. – Hafiz Feb 07 '14 at 09:14
  • Thanks a lot! This was my case as well, but interesting part that I don't use Resharper, I have Devexpress Code Rush, but I can not tell how did that self-reference appear... no proof to blame Code Rush. – Prokurors Jun 26 '15 at 07:40
  • This condition often arises from copying/pasting a class - or a folder(s) of classes - within the same project. – galaxis Dec 05 '15 at 23:50
  • 1
    Excellent : Check your project references if it contains a reference to itself. – Farhad Manafi Feb 12 '17 at 07:51
  • ok So Removing the reference to itself works. However as soon as the site compliles it comes back. Hitting Debug for example add it back in every time. It does not need to be there so how do I prevent it from re appearing – Mike Mar 14 '17 at 18:35
  • I had the same issue in Rider.Net - I think Rider.Net referenced a project to itself when I refactored some code. – boggy Jan 17 '19 at 23:33
16

This is not a real solution and I can't explain how did it work but it worked.

I was trying everything in Project Properties, References, bin, obj but nothing helped me really. And I was just trying every option and chose 'Convert to Web Application' from the context menu. Although It was a Web Application itself before. It showed me a warning saying that .designer.cs files will be added to each aspx file and I just clicked OK.

Everything remained same except App_Code folder (where all these classes were) was renamed to Old App_Code and I'd build the project. And now I am not getting compilation error at run time.

I know App_Code folder are meant to Website Project and I was having no issue with this till this time in WAP. But I just realize I should not have App_Code folder in WAP as it is discussed here in a wrox forum (1st point). Author has said here that

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.

I have tried it renaming too before conversion but that time it did not work.

shashwat
  • 7,851
  • 9
  • 57
  • 90
2

I had the same problem. After some research and experimenting, I found that (for whatever reason) visual studio doesn't like extension methods in the App_Code folder. Easiest solution I found was to just change the name of the folder.

1

I had the same situation. I had a custom grid control for which there was an extension method. The method was only declared in one place. The control had been in use in multiple places throughout my solution with no problems. Then one day, I copied a control that implemented the custom grid control by literally copying and pasting within the same project. Then I just renamed the properties as appropriate. After that the solution wouldn't build quoting the same error as above. I suspect the copy and paste added to the project a reference to itself.

Deleting it resolved the problem.

lee
  • 11
  • 1
0

My issue was similar to what shashwat describes. In my case I put the code file in the App_Code dir, but in order to get intellisense, I set the file itself to compile which likely means that the file was getting compiled at both runtime and compile time. I just moved the file out of App_Code.

b_levitt
  • 7,059
  • 2
  • 41
  • 56
0

I have found this solution in the comments above. And it helped me.

I have the reference to project A in the project A. By mistake.

Removeing helps =)

Anton Semenov
  • 411
  • 5
  • 11