views:

790

answers:

3

I have a .asmx web service webmethod that I am successfully calling from jquery.ajax. The webmethod is functioning properly. However, it performs very slowly.

The method itself does very little work right now. I have added CacheDuration = 120. That did not help. My real concern is how long it takes to actually call the method. When running the website local and starting VS2008 debugger with a breakpoint on the first line of the Webmethod, it takes nearly 5 seconds to even get to the first line. Then hitting continue in the debugger, the response happens immediately. So the webmethod does not seem to be the slow "thing".

Has anyone experienced similar behavior? Is there something I need to set for the webservice/method to be called faster?

Code:

[WebService(Namespace = "http://intranet/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class DocumentService : System.Web.Services.WebService
{

  [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  [System.Web.Services.WebMethod(EnableSession = true, CacheDuration = 120)]
  public Tree[] GetDocumentTree(string root)
  {........
A: 

You can use Fiddler or FireBug to figure out what the slow part is. Make sure you have hit the webservice once for it to be compiled before timing it.

David
Thanks for the suggestion. I am actually using Fiddler to time it. I have timed it by hitting the service directly and by letting my page hit it. It is the same time...in debug or release mode. And I have let it compile as well. Any other thoughts?
A: 

Also use JetBrains dotTrace on the server side. Once I did this, and discovered that a certain .asmx page took a long time to load because of variables being initialized in its constructors. (Web service references take a long time to construct, in particular.)

Olema
+1  A: 

Make sure your problem isn't just JIT or load time. On the first call the webservice has to compile and load assemblies which can be expensive. If the problem persists in later calls then check your proxy settings.

We had the same problem. Our delay was much more significant then yours (2 minutes per call total). We were invoking a web service from inside a windows service that was running as a domain user. That user account apparently has it's proxy settings turned on. So for each HttpRequest to the local web service the backing ServicePoint tried to fetch the wpad.dat file from a non-existant proxy server. That call took 20 seconds to time out. Worse there is a global lock around the code that fetches the proxy in the .net libraries. So calls stack up waiting for web timeout. The service making the calls is our queue process which handles all of our async behavior. So the entire system backed up.

If you want to see the code look at System.Net.AutoWebProxyScriptEngine.GetProxies in Reflector.

Since I don't have rights to the user account in question I fixed our problem today by adding to the app.config file.

If your method is intended to be invoked with ajax then you have to get the browser settings correct. Try disabling Automatically Detect Settings in the Connections\Lan Settings. I looked around the web a bit after I found the problem and apparently IE7 is reported to have latency problems when this setting is on. If you do have a proxy server then manually enter the server address in the same dialog.

csaam