How to write an asmx web service without using app_code directory?
Excuse the title, but it's best I just explain the problem.
I have 2 projects in my solution
- A Class Library
- A Web Application, which consists of a web service (asmx).
the web service has code sitting in the app_code folder, with a file [webservicename].cs
Inside the webservice code behind class, I have a web method here is a sample example (its simplified):
[WebMethod]
publi开发者_Python百科c EnumTaskExportState ProcessTask()
{
var tm = new UploadTaskManager();
return tm.ProcessTask();
}
Now at design time, in visual studio (2010 or 2008), when I right click on UploadTaskMananger, and then select "Go to definition". I get taken to AppData\Temp[some folder structure]...etc.... and it displays the public class definition.
Instead I would like to have complete integration, so that I get taken directly to the actual class in the class library project.
My guess is, this is happening because I am using the app_code route, and not a compiled file for the web service class. But I don't know any other way to do this.
How can I fix this? Possibly do away with the need for the app_code directory?
You must be creating a web service by using File->New Web Site. I strongly recommend that you never build a web service in a web site.
Instead, create your web service by using File->New Project and selecting "WCF Service Application". If you're stuck using ASMX web services (which Microsoft now considers to be "legacy technology"), then select "ASP.NET Web Service Application".
These will both be Web Application Projects, and will behave like all other project types in Visual Studio.
Among other things, there will be no App_Code folder.
Its pretty easy
Step 1... when you generate a new web service , the app_code/.cs file gets generated too. Copy the signature from that class, it will be used as a template in step 4.
Step 2... delete the app_code/.cs file that got generated
Step 3. In your class file, create a namespace and folder structure where you will include the web service classes containing web methods. For example Class1.WebServices
Step 4. in Namespace Class1.WebServices (as per above example), create a new class, then replace the default class signature with the copied class signature from step 1.
Step 5. Edit the web service now, and create your web methods
Step 6. In the web application, edit the asmx file to reference the class without code behind for example
<%@ WebService Language="C#" Class="Class1.WebServices.WebServiceClass" %>
Step 7, test!
精彩评论