Add WebService to my project (ASMX + CS) in Visual Studio

I’ve never found on my Visual Studio an option to directly add a new WebService file so I looked for a solution and I got this one, I hope it’ll help you:
1. From anywhere in your Solution Explorer (where you want to add the WebService), create a “New Item”.
2. In the “New Item” Window, select “General” category and “Text File” as the type of file. In the name TextBox write your WebService name and instead of “.txt” as an extension, write “.asmx”.


3. Now, add a “New Item” again, selecting “Class” as a type of file, inside “Code” category. Use the same name as you used to create the ASMX file.
4. Once both created, you’ve to connect them to each other and specify some stuff
5. In the ASMX file, add this line:

<%@ WebService Language="C#" Class="MyProject.Layouts.MyLayoutsFolder.MyWebService, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=TOKEN" %>

Note: “MyProject.Layouts.MyLayoutsFolder.WsHerramientas” must be replaced by your project Path (In my case I worked with a SharePoint project, so I added it to my Layouts folder). “TOKEN” must be the public token of your project. If you don’t know your public token you can follow this steps: Find Public Key Token of my Project in Visual Studio 2012.
6. Add the Web Services Reference to the project:

6.1. Right button click in your project -> "Add Reference".
6.2. Search "System.Web.Services" and add it.

7. Add the inheritance of WebService by:

7.1. Add this on top: "using System.Web.Services;".
7.2. Add the inheritance of the class:
     "public class MyWebService : WebService".

8. Add your methods to the CS File:

public class MyWebService : WebService
{
     [WebMethod]
     public string MyWebServiceMethod()
     {
          return "Ok";
     }
}

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *