Thursday, 10 March 2011

Problems and solutions whilst creating an ASP.NET Web Service

Problems setting up an ASP.NET Web Service

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.

Parser Error

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'.

Source Error:


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:
http://localhost/MathService/MathService.asmx

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:
http://localhost:1271/MathService.asmx

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.asmx
instead it was located at
http://localhost:1271/MathService.asmx
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.


3 comments:

zoser0 said...

The file 'src' is not a valid here because it doesn't expose a type.
I have the following error massage:
.................................................. .........
Server Error in '/' Application.
Parser Error
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: The file 'src' is not a valid here because it doesn't expose a type.

Source Error:


[No relevant source lines]


Source File: none Line: 0

Version Information: Microsoft .NET Framework Version:2.0.50727.5466; ASP.NET Version:2.0.50727.5456
...............................................
http://www.tropicanagroupegypt.com/g...D=360&LangID=1


but i can't understand what happen ,, the website is working since two years with no problem << recently i can't open the gallery link ,, and i have the above error massage.

any one can help?

zoser0 said...

The file 'src' is not a valid here because it doesn't expose a type.
I have the following error massage:
.................................................. .........
Server Error in '/' Application.
Parser Error
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: The file 'src' is not a valid here because it doesn't expose a type.

Source Error:


[No relevant source lines]


Source File: none Line: 0

Version Information: Microsoft .NET Framework Version:2.0.50727.5466; ASP.NET Version:2.0.50727.5456
...............................................
http://www.tropicanagroupegypt.com/g...D=360&LangID=1


but i can't understand what happen ,, the website is working since two years with no problem << recently i can't open the gallery link ,, and i have the above error massage.

any one can help?

Unknown said...

You give readers a lot to think about and I appreciate that kind of writing.
articles about the web marketing