c# - 调用特定网络场

标签 c# asp.net

一些背景。 目前有一个 asp.net 网站,它包含在 2 个负载平衡的网络服务器上,即网络农场。

我想要一些代码,允许我调用一个特定的服务器并在其上执行一个方法。我想这样做,以便我可以强制所有网络服务器通过站点上的网页刷新它们的缓存。

用例是:管理员用户登录站点并对缓存的设置进行更改,然后单击“刷新 Web 服务器缓存”按钮,然后调用每台服务器上的更新缓存方法。这是为了防止我每次更改缓存设置时都必须重新启动应用程序池。

最佳答案

Jamiec 的回答是正确的;每台机器仍然有自己的IP。我使用 ASHX 处理程序来接收重置缓存的请求。网络场中的任何机器都可以发起请求。

这是一个相当完整的示例,但有一些我没有包含的辅助方法和配置设置。

   <!-- 

    URL of user web application that can be used to clear caches.  
    Delimit multiple URLS with pipes: url1|url2|url3 

    -->
    <add key="UserServers" value="http://123.456.789.001/|http://123.456.789.002" />

下面是在执行缓存重置的每个站点上实际调用处理程序的代码。我建议在机器之间使用某种共享密码并单独保护处理程序,以便它不能被公开访问。

        /// <summary>
        /// Calls for a reset of caches on one or more user sites serving reports. 
        /// Allows for multiple urls to be processed; configure the reset targets
        /// using AppSettings["UserCacheResetUrl"], delimited with pipes if there
        /// are multiple sites. 
        /// </summary>
        public static void ClearAllUserCaches()
        {
            //
            // clear local user caches
            ClearUserCaches();

            //
            // clear out of process user caches

            string[] urls = AppSettings.UserServers;

            for( int i = 0; i < urls.Length; i++ )
            {
                string url = urls[i] + AppSettings.UserCacheResetPath + "&sharedPassword=" + AppSettings.SharedPassword;

                WebRequest request = null;
                HttpWebResponse response = null;

                try
                {
                    request = WebRequest.Create( url );
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch( WebException ex )
                {
                    Log.LogException( ex );
                }
                finally
                {
                    request = null;
                }

                if( response == null || response.StatusCode != HttpStatusCode.OK )
                {
                    if( response != null )
                    {
                        response.Close();
                        response = null;
                    }
                }
            }
        }

处理程序代码本身(抱歉,长度)。

    /// <summary>
    /// Exposes an interface for trusted callers to request that this
    /// instance of the application perform an action.
    /// </summary>
    public class AdministrationRequestHandler : IHttpHandler
    {
        /// <summary>
        /// Processes an incoming request and performs an action specified via the "action"
        /// parameter passed on the query string.  Only local callers will be allowed, and
        /// only callers who pass a shared password via the "sharedPassword" query string
        /// parameter.
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest( HttpContext context )
        {
            //
            // get the action from the query string, and check that
            // it actually contains a value.
            string action = context.Request.QueryString["action"].ToSafeString().ToUpper( CultureInfo.InvariantCulture );

            if( string.IsNullOrEmpty( action ) )
            {
                //
                // Dump out an error message and return--we can't do anything
                // without an action and this request may have malicious
                // origins.
                context.Response.Write( "Missing action." );
                return;
            }

            //
            // Now validate the shared password that all web applications in this
            // solution should be using.  This password will NEVER be placed on a user's
            // query string or ever passed over a public network.
            string sharedPassword = context.Request.QueryString["sharedPassword"].ToSafeString();

            if( string.IsNullOrEmpty( sharedPassword ) )
            {
                context.Response.Write( "Missing shared password." );
                return;
            }

            //
            // check that the shared password is actually valid
            if( sharedPassword != AppSettings.SharedPassword )
            {
                context.Response.Write( "Invalid shared password." );
                return;
            }

            //
            // perform the specified action
            if( action == "CLEAR_CACHE" )
            {
                AppContext.ClearUserCaches();
            }
        }

        /// <summary>
        /// Specifies whether or not the instance is reusable.
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

关于c# - 调用特定网络场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3823230/

相关文章:

c# - 在 WPF C# 应用程序中将 XPS 转换为 PDF

asp.net - Oracle中数百个实体(表)的CRUD操作

.Net 问题 "Illegal characters in path."

c# - Ninject: Controller 的实体上下文

java - 构建Antlr时,Visual Studio在未命名文件的情况下给出错误 "cannot find the file specified"

c# - 版本 7 与 8.2 中的 CustomTableProvider

c# - C# 中的泛型列表和静态变量行为

c# - 为每个 Page_Load 运行代码

asp.net - 在客户端 ASP .Net C# 上运行非托管 C++ 函数

javascript - 如何基于 JWT token 登录后将用户重定向到另一个页面?