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 = "https://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
https://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 :-)