LotusShiv 0 Newbie Poster

Scenario: I have a ASP.Net 3.5 (VS.Net 2008/C#) application. I have a webservice which I have marked with [ScriptService] attribute and also made sure that the <compilation debug=trus /> setting is in place in the webservice web.config configuration settings. I add the Script reference in the codebehind of my aspx page, instead of setting it directly on the aspx markup. Reason being I want to be able to read the webservice path off of my web.config of the website.

Test case 1: I build and deploy my website, where in the webservice assigned happens to be the webservice deployed on localhost. I have some page control events for which the event handler I have assigned calls the appropriate webservice method and I get the results.

Test case 2: I try to now run my application itself (instead of the deployed website) as I want to debug, that is where the issue arises. As I step through the AJAX generated code, when it tries to invoke the webmethod ( the invoke method - first parameter being this._get_path()) here it seems to strip the localhost and just returns /MyVirtualDir/Myservice.asmx as the path and when I proceed further, I get a 12030 error and nothing is returned for the webmethod call. And when I interrupt at this point before proceeding and reset in the QuickCheck window the path value to be the fully qualified one, I get the access denied error. Mind you my webservice has anonymous access (the default setting after deploying the webservice). FYI, the webmethod that I tried to debug is the same method for which I get the results in Test case 1.

I need to know what assumptions I am making are wrong in the second case and what I need to be doing. Any help is appreciated.

-- LotusShiv

Dani AI

Generated

This looks like a classic “debug host vs deployed host” problem rather than a bug in the service itself. When you run the page under Visual Studio’s development server the page’s origin (scheme/host/port) can be different from the IIS site where the ASMX lives; a script-generated proxy that ends up with an application-root path will be resolved against the page origin, not the deployed host, so the browser may be trying the wrong address and the request gets aborted (WinInet 12030). (learn.microsoft.com)

Practical ways to fix or debug this quickly:

  • Run the page and the service under the same origin so the browser sees a single host/port (use Local IIS or configure your project to use the same URL/virtual directory you deploy to). That gives you the same runtime behavior when debugging as when deployed. (learn.microsoft.com)
  • If you cannot use the same origin, add a small server-side proxy (an .ashx or MVC action) in the web app that forwards client AJAX calls to the ASMX; the browser talks only to the same origin proxy.
  • If you control the service and want cross-origin calls, enable CORS on the service (or implement JSONP with care). Note that ASP.NET’s AJAX/ASMX stack intentionally restricts GET/JSONP and requires JSON content-type/security checks, so enabling cross-origin access often needs explicit server changes. (weblogs.asp.net)

Quick snippets to try while debugging (replace names with your paths):

// Global.asax - allow CORS for debugging only
void Application_BeginRequest(object sender, EventArgs e) {
  var ctx = HttpContext.Current;
  ctx.Response.AddHeader("Access-Control-Allow-Origin", "*");
  if (ctx.Request.HttpMethod == "OPTIONS") {
    ctx.Response.AddHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
    ctx.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
    ctx.Response.End();
  }
}
// build a same-origin URL at runtime
var svcUrl = location.protocol + '//' + location.host + '/virtualDir/YourService.asmx';

Debug checklist: watch the Network tab (or Fiddler) to confirm the exact request URL/Host/Port and response code, verify whether the server returns 401/403 vs a connection abort, and avoid using wildcard CORS in production. These steps will quickly tell whether the problem is origin/port mismatch or a server-side reject.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.