Web 服务性能问题

发布于 2024-07-11 18:08:14 字数 343 浏览 5 评论 0原文

我有一个简单的 asmx Web 服务,只需将一些信息记录到事务数据库中。 但是对于客户端来说已经超时了。 更新数据库的调用仅调用 1 个存储过程,我不相信它可以进一步优化以获得更好的性能。 我已经简化为仅使用 log4net 记录运行,然后通过更新数据库的单独进程读取日志。

我想知道是否有更好的方法来做到这一点。 我想知道是否有办法让我的代码执行以下操作:

public bool method(...)
{
  LogRun(...)

  Asynchronously call method to insert transaction

  return true;  
}

I've got a simple asmx web service that just needs to log some information to a transactional database. However it is timing out for the client. The call to update the database just calls 1 stored procedure and I don't beleive it could be optimized further for better performance. I've been reduced to just logging the run using log4net and then reading the log in by a separate process that updates the database.

I was wondering if there is a better way to do this. I was wondering if there is a way to make my code do something like:

public bool method(...)
{
  LogRun(...)

  Asynchronously call method to insert transaction

  return true;  
}

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

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

发布评论

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

评论(5

酷炫老祖宗 2024-07-18 18:08:14

编辑:我对BackgroundWorker的看法是错误的。 于是我把它换成Thread版本,进行测试。

如果你希望异步完成工作,你可以研究如何启动另一个线程。

public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public void Log(int foo, int bar)
    {
        Thread a = new Thread(new ThreadStart(delegate()
        {
            // Do some processing here
            // For example, let it sleep for 10 secs
            Thread.Sleep(10000);
        }));
        a.Start();
    }
}

如果 Thread.Sleep(10000) 行位于 Log 方法本身中,则 Log 方法需要 10 秒才能完成处理。 然而,对于线程a,Log方法将在调用后立即返回。

另请注意,对于这种异步调用方式,没有简单的方法可以保证调用客户端插入操作是否完成。

EDIT: I was wrong about BackgroundWorker. So I chaged it to the Thread version, tested.

If you want the work done asynchronously, you can study how to start another thread.

public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public void Log(int foo, int bar)
    {
        Thread a = new Thread(new ThreadStart(delegate()
        {
            // Do some processing here
            // For example, let it sleep for 10 secs
            Thread.Sleep(10000);
        }));
        a.Start();
    }
}

It would take 10 seconds for Log method to finish processing if the Thread.Sleep(10000) line is in the Log method itself. However, with Thread a, The Log method will return immediately after call.

Also note that, there is no easy way to guarantee the calling client if the insert operation is completed or not, with this style of asynchronous call.

蘑菇王子 2024-07-18 18:08:14

您可能想要尝试的一件事是跟踪

One thing you may want to try is Tracing.

聊慰 2024-07-18 18:08:14

如果查询无法进一步优化,您可以增加 SQL 客户端的超时值(假设您可能使用 SQL Server)。

在使用 Web 服务的客户端上,如果您希望客户端继续执行其他操作,则可以异步使用该方法。 当 Web 方法完成时,它将触发一个事件。 有一个很好的小例子您可以在这里阅读 如果您认为这可能有帮助。

If the query cannot be optimized any further you could increase the timeout value for your SQL client assuming you might be using SQL server.

On the client side that consumes the web service, you can use that method asynchronously if you would like the client to continue performing other operations. When the web method is finished it will trigger an event. There is a nice little example you can read here if you think it might help.

逆流 2024-07-18 18:08:14

虽然您可以考虑在 webmethod 中执行异步操作,但您正在使用 ASP.NET 中的线程和/或线程池,它已经具有多线程操作。 虽然技术上可行,但您也可能会无意中抢占系统资源,因为 httpruntime 会在向您的服务提供请求时管理资源。

写入本地 log4net 文件并在离线例程中导入该数据比实时日志记录(异步或其他方式)具有更高的可用性,因为您的 sql 服务器可以离线并且您的服务仍然可用。 如果除了日志记录之外不需要 sql server 进行任何操作,最好将数据库操作排除在 webmethod 之外。

While you can look at doing asynchronous operations inside your webmethod, you're working with threads and/or the threadpool inside asp.net, which already has multi-threading operations in play. While technically feasible, you can also inadvertently rob the system of resources as the httpruntime manages resources in serving requests to your service.

Writing to the local log4net file and importing that data in an offline routine is higher availability than real-time logging, asynch or otherwise, since your sql server can be offline and your service is still available. If you don't require sql server for anything other than logging, prefer keeping db operations out of the webmethod.

仙女 2024-07-18 18:08:14

如果这只是对客户端的“即发即忘”调用,您的 Web 服务只需将详细信息添加到队列中并返回给客户端即可。 我已经使用 MSMQ 来实现类似于您所描述的内容。 它不能修复数据库超时,但可以消除客户端的错误消息。

If it's just a fire-and-forget call for the client, your web service could just add the details to a queue and return to the client. I've used MSMQ to implement something similar to what you're describing. It doesn't fix a database timeout but it does take the error message away from your clients.

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