AJAX Javascript call to web service fails when (.asmx) is placed into sub directory


Problem Description

===================

AJAX Javascript call to web service fail with is ‘null or not an object’ , when Web service is placed into sub directory say Service

Error

=====

A Runtime Error has occured

Do you Wish to debug?

Line:53

Error: ‘SupportIssue.BrokenService’ is null or not an object

 

Web service code

===================

namespace SupportIssue

{

[WebService(Namespace = “http://tempuri.org/”)]

[ScriptService]

public class WorkingService : System.Web.Services.WebService

    {

        [WebMethod]

        public string HelloWorld()

        {

            return “Hello World”;

        }

    }

}

 

In Default.aspx, registering through Script manager

================

<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>

        <Services>

            <asp:ServiceReference Path=”~/WorkingService.asmx” />

           <asp:ServiceReference Path=”~/services/BrokenService.asmx” />

        </Services>

</asp:ScriptManager>

 

Calling the web service method in Javascript in Default.aspx

=============

<script language=”javascript” type=”text/javascript”>

            //This works because it is in the same directory

var x = SupportIssue.WorkingService.HelloWorld(OnSucceeded, OnFailed);

//The following line break in to error!!!

var y = SupportIssue.BrokenService.HelloWorld(OnSucceeded, OnFailed);

            function OnSucceeded(result, userContext, methodName)

            {

                alert(result);

            }

            function OnFailed(error, userContext, methodName)

            {

                alert(error);

            }

        </script>

 

The above line would fail as the web service has been placed in sub directory called services

Same line would succeed if we put out web service (.asmx) file in application Root Folder

 

— Tracking through Fiddler trace, notice we are making call to jsdebug

 

Note : The “/jsdebug” suffix is used by AJAX to get the debug release of Javascript proxy for web service and “/js” for released version

 

For GET /SupportIssue/WorkingService.asmx/jsdebug HTTP/1.1

Response:

HTTP/1.1 200 OK

For GET /SupportIssue/Service/BrokenService.asmx/jsdebug HTTP/1.1

Response:

HTTP/1.1 500 Internal Server Error

 

— Checking IIS log we got 500

2008-04-26 05:59:02 W3SVC1 127.0.0.1 GET /SupportIssue/Service/BrokenService.asmx/jsdebug – 80

FAREAST\jaskis 127.0.0.1 500 0 0

 

— Try Browsing jsdebug for failed request from IE

 

http://localhost/SupportIssue/Service/BrokenService.asmx/jsdebug

Server Error in ‘/SupportIssue’ Application.

———————————————————-

No web service found at: /SupportIssue/Service/BrokenService.asmx.

Exception Details: System.InvalidOperationException: No web service found at: /SupportIssue/Service/BrokenService.asmx.

 

 

Workaround

===========

1) Issue seems to be with Web application project and not for Web site, because for WAP Namespaces are added for before class beginning so use Web Site project instead

For example

namespace SupportIssue

{

public class WorkingService…..

}

 

2) If want to use WAP then change the namespace of classes within the sub directory corresponding to sub directory name only*

Directory/folder Name = Service

As default setting we have namespace(project name) added for WAP

namespace SupportIssue

{

  

}

Change it to

namespace SupportIssue.Service

{

}

and

var y = SupportIssue.BrokenService.HelloWorld(OnSucceeded, OnFailed);

to

var y = SupportIssue.Services.BrokenService.HelloWorld(OnSucceeded, OnFailed);

That’s it.

Hope it helps :-)

Comments (10)

  1. Nate says:

    Thanks…I ran into this same problem. Your solution worked, but only in C#. When I try it in VB.NET, I still have the same problem.  Have you tried it in vb.net?

  2. Jas says:

    Nate,

    Sorry for later reply.Yup, it indeed works with VB.Net

  3. 話題の小向美奈子ストリップを隠し撮り!入念なボディチェックをすり抜けて超小型カメラで撮影した神動画がアップ中!期間限定配信の衝撃的映像を見逃すな

  4. dt says:

    I had exactly the same problem.

    I checked all my namespaces and everything looked OK.

    The only difference was that I placed a copy of a working ASMX in a subdirectory and modified the namespaces afterwards.

    My problem was caused by the class attribute in the ASMX file. (not the code behind!!)

    NOTE: when you make a copy of an existing asmx file you have to change the class attribute in your asmx file.

    Otherwise during runtime a proxy of the wrong class is generated.

    Since doubleclicking the asmx file in Visual Studio opens the .asmx.cs file this cannot be done directly from Visual Studio!!!

  5. Joe Henderson says:

    The last comment (on June 30) kept me from pulling my last hair out!  It helped me fix a problem that had been bugging me for days.

  6. We have exactly the same problem and your post helped me to figure out the solution. Keep up the good work mate.

  7. rkv18 says:

    I have the same problem and at same location and when I keep a break point and check near the SupportIssue.BrokenService.HelloWorld(…….., ……………….)  I cannot getinto HelloWorld method. It gives Microsoft JScript runtime error: 'SupportIssue.BrokenService.HelloWorld'   is null or not an object and it doesnot go in onsucceeded or on failed methods ever.. Please help me with the issue ..

  8. rocky811 says:

    I have the same problem and at same location and when I keep a break point and check near the SupportIssue.BrokenService.HelloWorld(…….., ……………….)  I cannot getinto HelloWorld method. It gives Microsoft JScript runtime error: 'SupportIssue.BrokenService.HelloWorld'   is null or not an object and it doesnot go in onsucceeded or on failed methods ever.. Please help me with the issue ..

  9. rocky811 says:

    I have the same problem and at same location and when I keep a break point and check near the SupportIssue.BrokenService.HelloWorld(…….., ……………….)  I cannot getinto HelloWorld method. It gives Microsoft JScript runtime error: 'SupportIssue.BrokenService.HelloWorld'   is null or not an object and it doesnot go in onsucceeded or on failed methods ever.. Please help me with the issue ..

  10. Warrior says:

    Isn't it a typo in your last line:

    var y = SupportIssue.Services.BrokenService.HelloWorld(…

    Shouldn't it be:

    var y = SupportIssue.Service.BrokenService.HelloWorld(…