I was recently following a guide to setting up a .NET web service in Visual Studio 2010 and I ran into a few problems.
The guide I was using was Microsoft's own Knowledge Base article which runs through the setting up of a very simple Maths based web service.
Missing Web Service Project Template
The first problem was trying to find the ASP.NET Web Service project template on the start up page of Visual Studio. Step 2 of the guide says:
On the File menu, click New and then click Project. Under Project types click Visual C# Projects, then click ASP.NET Web Service under Templates. Type MathService in the Location text box to change the default name (WebService1) to MathService.
However in Visual Studio 2010 this option was missing. I found out that the reason was due to the default Framework being set to .NET 4.0. The solution is to change the framework to .NET 3.5 and low and behold the Web Service template option was available to select.
I don't know the reasons behind this so don't ask and maybe it's possible to create a web service in .NET 4.0 some other way. If I find out I will update this article.
How to resolve "Could not create type MathServices.service1" error
The second problem I ran into was due to the advice given in step 3 which was to rename the default web service to something else.
Change the name of the default Web service that is created from Service1.asmx to MathService.asmx.This is fine until you actually build the project and try and access the service on your localhost as it says to do in Step 7.
Browse to the MathService.asmx Web service page to test the Web service. If you set the local computer to host the page, the URL is http://localhost/MathService/MathService.asmx.When the page loads I was met with an ASP.NET error along the following lines:
Server Error in '/' Application.
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not create type 'MathService.Service1'.
Line 1: <%@ WebService Language="C#" CodeBehind="MathService.asmx.cs" Class="MathService.Service1" >
Source File: /MathService.asmx Line: 1
The reason behind this was that even though I had changed the name of the service I had not changed the code in the .ASMX file to point to the new codebehind. Therefore you need to right click on the .ASMX file in the project explorer and choose the "View Markup" option. Once that is open just change the code to point to the new class like so.
<@ WebService Language="C#" CodeBehind="MathService.asmx.cs" Class="MathService.MathService" %>
Incorrect location for the web service
The knowledge base guide I was following said that the web service would be located on my local machine at the following location:
However when trying this gave me a 404 error. So instead after building the application I clicked the play button which took me to the following location:
As you can see this differs due to the port number and missing sub directory. I am pretty sure I haven't missed any steps out along the way and the namespace for my Web Service was setup correctly so I am not sure why this differs from the example.
Creating a Consumer application for the web service
The previous step was important because the next part of the guide was on creating a consumer console application to make use of the web service.
In Visual Studio you select the "Add Service Reference" option from Project menu and then click the "Advanced" button.
On the next page choose the "Add Web Reference" button at the bottom and then on the next page you need to enter the location of your web service in the "URL" box at the top and then hit the little green arrow to the side of it. This will search for the reference at the specified location.
Make sure you know where the web service is located before trying this as the location of my own local web service was not where the knowledge base article said it would be:
http://localhost/MathService/MathService.asmxinstead it was located at
Once the web service is found you can add it in the normal manner.
After that change, a re-save and a re-build everything worked fine.
Hopefully this might help others out there who are following the same guide if they run into the same problems.