简体   繁体   中英

Why am I having an ambiguous type error when calling a WebMethod on ASP.NET Web Application?

The message sent to browser is as follows:

The type My.API.Class is ambiguous: it could come from assembly '[on Temporary ASP.NET Files]' or from assembly '[on bin folder]'

The problem occurs when debugging a Web App, specifically when making a request to a WebMethod of a WebService.

The project compiles just right. It generates My.Website.dll on bin folder and if I publish the Web Application. It works fine.

The asmx file is on the root of the application. The CodeBehind file is on App_Code and its marked to be compiled to generate My.Website.dll.

I should be missing something really important.

I found someone having the same issue with a possible related cause. Check it out . The way this person exposes its problem is somehow similar but I get starting to be lost when he talks about a proxy class and shared dlls I don't use.

Any help is appreciated.

According to this , the App_Code folder should be used only on Web Site projects. That's the reason the CodeBehind of the asmx was compiled at runtime too.

The initial question was made based on a Web App. But I didn't specify this Web App. was been manually changed from a Web Site project.

To solve my problem I did the following:

  1. Convert the project to Web Application. This will make App_Code to be renamed to Old_App_Code.
  2. Moved all the Old_App_Code resources to a Class Library and then referenced this library into the Web Application.

@Tony: Thank you for guiding me.

The best way to debug issues like this is to use the "Modules" window in the Debug->windows menu of Visual Studio. It will show you all loaded assemblies. In particular, you want to look for My.API.Class in the modules list more than once. Sort the list by order loaded, and look at the dll right before it (that's usually the one responsible for the assembly to be loaded). The most likely cause of this is that one of your references also references My.API assembly, but references a different version of the assembly than your site does.

You can also fix it by adding your assembly name at the end of the attribute "Class" ex :

Instead of

<%@ WebService  Language="C#" CodeBehind="~/App_Code/WebService.cs"  Class="WebService" %>

Use

<%@ WebService  Language="C#"  Class="WebService, YourProjectName" %>

When you're making a web site, the assembly name is something random starting with "App_Code" but when you change it to a Web App the assembly name will be "YourProjectName".

This problem is only on development environment, so I guess my solution is better because you won't have to rename your folder (thie could cause problem with your source control).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM