客户端和服务器之间的通信层

问题描述 投票:3回答:2

我想知道是否有任何技术来控制Web应用程序中的客户端和服务器之间的通信(ASP.NET)

例:

  • 请求数量
  • 检查是否重复请求
  • 检查是否已执行操作

工作流

  1. 客户端发送请求“A”
  2. 服务器接收请求“A”,并作出响应
  3. 服务器将请求“A”标记为已应答
  4. 客户端重新发送请求“A”
  5. 服务器回答请求“A”已应答
c# asp.net client-server communication
2个回答
2
投票

您可以在Global.asax文件中使用以下方法拦截请求:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var request = ((System.Web.HttpApplication)(sender)).Context.Request;
        //here you can evaluate and take decisions about the request
    }

0
投票

在任何ASP.NET应用程序中,您都可以使用HttpApplication事件来跟踪所需的更改。 例如,您可以使用BeginRequest和/或EndRequest事件跟踪它:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if(MyGlobalFlags.TrackingRequests){
        //  do stuff
    }
}

protected void Application_EndRequest(object sender, EventArgs e)
{
    if(MyGlobalFlags.TrackingRequests){
        //  do stuff
    }
}

根据个人意见,我会使用全球旗帜,如果我愿意,我可以轻松关闭。

如果您正在讨论ASP.NET MVC应用程序,我还建议您在要跟踪的操作中使用ActionFilters 。 您可以实现自己的ActionFilter类并跟踪OnActionExecuted和/或OnResultExecuted的更改。 我仍然会使用全局标志来关闭跟踪而不更改代码。

public class MyTrackingActionFilter: ActionFilterAttribute{
    public override OnActionExecuted(ActionExecutedContext filterContext)
    {
           if(MyGlobalFlags.TrackingRequests){
            //  do stuff
        }
    }

    public override OnResultExecuted(ActionExecutedContext filterContext)
    {
           if(MyGlobalFlags.TrackingRequests){
            //  do stuff
        }
    }
}

作为一个说明,我不会尝试在这些事件中做大事。 如果轨道需要可以并行运行的大量数据库操作,我建议您在使用线程池时使用队列系统。

© www.soinside.com 2019 - 2024. All rights reserved.