Wednesday, August 20, 2008

How do I – Solve the “Could not create type WebService.Name” error when trying to deploying a web service?

OK, so you wrote a web service and you may even have tested it and everything worked fine. So the you did some "cleanup" and tried to deploy the web service. It compiles and deploys just fine, but when you try to invoke and hit the service you are presented with the following error:

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 'WebServices.Candidates'.
Source Error:

Line 1: <%@ WebService Language="C#" CodeBehind="Candidates.asmx.cs" Class="WebServices.Candidates" %>

Source File: /Candidates.asmx Line: 1

So just how do you solve this problem?

Here's my checklist of things to check when having trouble deploying web services:

  1. Check that IIS is using ASP.NET 2.0.

    1. Begin by starting up the Internet Information Services manager.
    2. Expand "Web Sites".
    3. Expand your web site e.g. "Default Web Site".
    4. Locate the Virtual Directory to which the web service was deployed and right click it.

    1. On the popup menu, click "Properties".
    2. On the Properties window, select the "ASP.NET" tab.
    3. Ensure that the APS.NET Version is set to 2.0.x and not 1.1.x.
    4. If it is set to 1.1.x, set it to 2.0.x and retry your web service.
  2. Ensure your Visual Studio build properties are set correctly.

    1. Locate the .asmx file in your Solution Explorer pane and right click it.

    1. On the popup menu, click "Properties".
    2. Ensure that the Build Action is set to "Content".

    1. Locate the code behind (.cs or .vb) file in the Solution Explorer and right click it.

    1. On the popup menu, click "Properties".
    2. Ensure the Build Action is set to "Compile".

    1. If either of these weren't set correctly, make the correction, recompile, redeploy and retest.
  3. Ensure ASP.NET 2.0 is an Allowed web services extension.

    1. Open the IIS Manager.
    2. Expand your computer.
    3. Click the "Web Service Extensions" option.
    4. In the right hand pane, locate the "ASP.NET v2.0.x" entry and ensure that its Status is "Allowed". If it is not allowed, the "Allow" button on the left will be enabled. Click it to change the setting.

    5. If it was not allowed, correct the setting and retry your service.
  4. Check for the WebMethod attribute.

    1. Review your code and ensure that you have the [WebMethod] attribute set for all web methods in the service. This does not mean that all methods have to be web methods, but the ones you are trying to consume should have this attribute in its declaration.

  5. Ensure the .asmx and code behind file match definitions.

    1. Open your code behind (.cs or .vb) file.
    2. Look at the web service declaration at the top.

    1. In our example, ensure that #2 and #3 match. If they don't, correct the problem.
    2. Next note the namespace at #1.
    3. Now open the .asmx file. This can usually be done by double clicking it in the Solution Explorer, but if you're not able to get it to open through the IDE, open Windows Explorer, navigate to the file, right click it and select Open in Visual Studio to work around the problem.
    4. Now look at the code.

    1. Note the value of Class in this case is "WebServices.Candidates". The Class value should be the full namespace value as well as the class name. Given our code behind file's values, in this case, the Class value should have been "Crowe.PartnerNomination.WebServices.Candidates", but instead it is just "WebServices.Candidates". This is a common issue when reworking already written web services into a namespace hierarchy. For some reason the .asmx file does not always reflect the changes made which then causes the error message at the beginning of this post.
    2. Change the Class value, recompile, redeploy and retest.

    Taken from: http://www.cjvandyk.com/blog/Lists/Posts/Post.aspx?ID=133

7 comments:

Unknown said...

also i recommend to change app pool

the best way it is create new for your application

and than set that app pool @ home directory and set up Asp.net v2

Jazmatician said...

My issue was getting all the names the same, including the class name matching the markup. Thanks for the exhaustive list of possibilities to screw up!

Anonymous said...

God bless you with this awesome checklist!

Anonymous said...

This rocks - helped me a lot. Thanks!

Unknown said...

Addition:
Make sure your projects output path is set to bin instead of bin\Debug or bin\Release

Unknown said...

@Pascal Klarenbeek, if you set output path to debug or release it won't build at all

Anonymous said...

In my case, for the .asmx file changing the property, Copy to Output Directory to Copy if newer helped.