ASP.NET Web 服务 WebMethod

发布于 2024-07-18 04:36:50 字数 819 浏览 5 评论 0原文

我有一个 .asmx Web 服务 webmethod,我可以从 jquery.ajax 成功调用它。 网络方法运行正常。 然而,它的执行速度非常慢。

该方法本身目前几乎没有做任何工作。 我添加了 CacheDuration = 120。这没有帮助。 我真正关心的是实际调用该方法需要多长时间。 当本地运行网站并在Webmethod的第一行断点启动VS2008调试器时,甚至需要近5秒才能到达第一行。 然后在调试器中点击继续,响应立即发生。 所以 webmethod 似乎并不是缓慢的“东西”。

有人经历过类似的行为吗? 我需要设置什么才能更快地调用网络服务/方法吗?

代码:

[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)
  {........

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)
  {........

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九八野马 2024-07-25 04:36:50

确保您的问题不仅仅是 JIT 或加载时间。 在第一次调用时,Web 服务必须编译和加载程序集,这可能会很昂贵。 如果在以后的通话中问题仍然存在,请检查您的代理设置。

我们有同样的问题。 我们的延迟比您的延迟严重得多(每次通话总计 2 分钟)。 我们从以域用户身份运行的 Windows 服务内部调用 Web 服务。 该用户帐户显然已打开其代理设置。 因此,对于本地 Web 服务的每个 HttpRequest,后备 ServicePoint 都会尝试从不存在的代理服务器获取 wpad.dat 文件。 该调用花了 20 秒才超时。 更糟糕的是,在 .net 库中获取代理的代码存在全局锁定。 因此,调用会堆积起来等待网络超时。 进行调用的服务是我们的队列进程,它处理我们所有的异步行为。 所以整个系统都备份了。

如果您想查看代码,请查看 Reflector 中的 System.Net.AutoWebProxyScriptEngine.GetProxies。

由于我没有相关用户帐户的权限,我今天通过添加解决了我们的问题

到 app.config 文件。

如果您的方法打算通过 ajax 调用,那么您必须确保浏览器设置正确。 尝试在 Connections\Lan Settings 中禁用自动检测设置。 发现问题后,我在网络上浏览了一下,显然 IE7 据报告在启用此设置时存在延迟问题。 如果您有代理服务器,请在同一对话框中手动输入服务器地址。

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.

影子是时光的心 2024-07-25 04:36:50

您可以使用 Fiddler 或 FireBug 来找出缓慢的部分。 确保在计时之前已经访问过一次 Web 服务以便对其进行编译。

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.

别挽留 2024-07-25 04:36:50

还可以在服务器端使用 JetBrains dotTrace。 一旦我这样做了,发现某个 .asmx 页面需要很长时间才能加载,因为变量在其构造函数中被初始化。 (尤其是,Web 服务引用的构建需要很长时间。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文