0

我有一个 asp.net 网络表单,它将大约 25-30 个项目(当用户从​​表单发出后续请求时需要信息)写入自定义缓存。目前所有这些都在主线程上同步发生。但是在更高的负载下,addcache 正在成为一个瓶颈。

如何在后台运行此任务而不消耗 asp.net 工作进程线程池中的线程。

4

1 回答 1

4

Alternatives:

Completely async:

Partially async:

The call from the client to the server will be sync which means the response won't be returned to the client until the whole process ends, but the real code will be executed async releasing the thread used by ASP.Net increasing scalability

  • Execute the page async. You need to implement the IHttpAsyncHandler interface in your ASPX code behind. This is an example:

        public partial class _Default : System.Web.UI.Page, IHttpAsyncHandler
        {
            public void EndProcessRequest(IAsyncResult result)
            {
                var context = (result as AsyncOperation).Context;
    
                context.Response.Write(string.Format("<p>End Process Request on {0}</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
            }
    
            public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
            {
                var operation = new AsyncOperation(cb, this.Context, extraData);
                operation.StartAsync();
    
                this.Context.Response.Write(string.Format("<p>Begin Process Request on: {0}...</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
    
                return operation;
            }
        }
    
        public class AsyncOperation : IAsyncResult
        {
            private AsyncCallback asyncCallback;
    
            public AsyncOperation(AsyncCallback asyncCallback, HttpContext context, object state)
            {
                this.AsyncState = state;
                this.asyncCallback = asyncCallback;
                this.Context = context;
    
                this.IsCompleted = false;
                this.AsyncWaitHandle = null;
                this.CompletedSynchronously = false;
            }
    
            public HttpContext Context { get; private set; }
            public object AsyncState { get; private set; }
            public WaitHandle AsyncWaitHandle { get; private set; }
            public bool CompletedSynchronously { get; private set; }
            public bool IsCompleted { get; private set; }
    
            public void StartAsync()
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncOperation), this.AsyncState);
            }
    
            public void StartAsyncOperation(object workItemState)
            {
                // place here the async logic
    
                this.Context.Response.Write(string.Format("<p>Long Async operation started on: {0}</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
                Thread.Sleep(2000);
                this.Context.Response.Write(string.Format("<p>Long Async operation ended on: {0}</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
                this.IsCompleted = true;
                this.asyncCallback(this);
            }
        }
    

    Output

    enter image description here

  • Create an HttpAsyncHandler. You need to create a custom HttpHandler implementing the IHttpAsyncHandler interface. Example:

    public class AsyncHandler : IHttpAsyncHandler
    {
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            var operation = new AsyncOperation(cb, context, extraData);
            operation.StartAsync();
    
            context.Response.Write(string.Format("<p>Begin Process Request on: {0}...</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
    
            return operation;
        }
    
        public void EndProcessRequest(IAsyncResult result)
        {
            var context = (result as AsyncOperation).Context;
    
            context.Response.Write(string.Format("<p>End Process Request on {0}</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
        }
    
        public bool IsReusable
        {
            get { return false; }
        }
    
        public void ProcessRequest(HttpContext context)
        {
            throw new NotImplementedException();
        }
    }
    
于 2012-07-17T15:22:00.513 回答