Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法从ASP.NET访问IIS内核缓存?_Asp.net_Iis 7_Caching_Iis 6 - Fatal编程技术网

有没有办法从ASP.NET访问IIS内核缓存?

有没有办法从ASP.NET访问IIS内核缓存?,asp.net,iis-7,caching,iis-6,Asp.net,Iis 7,Caching,Iis 6,这仅清除用户缓存中的项目: public static void ClearCache() { foreach (DictionaryEntry entry in HttpRuntime.Cache) { HttpRuntime.Cache.Remove(entry.Key.ToString()); } } 还有什么方法可以访问内核缓存吗 澄清:我想打印内核缓存中所有项目的密钥,作为奖励,我还希望能

这仅清除用户缓存中的项目:

    public static void ClearCache()
    {
        foreach (DictionaryEntry entry in HttpRuntime.Cache)
        {
            HttpRuntime.Cache.Remove(entry.Key.ToString());
        }
    }
还有什么方法可以访问内核缓存吗


澄清:我想打印内核缓存中所有项目的密钥,作为奖励,我还希望能够从C#方法中清除内核缓存。

从您提供的讨论链接中,似乎缓存方法或属性存在,但受保护或私有,因此您无法访问它


通常,您应该避免使用不属于公共API的方法,但如果您想访问它们,请使用反射。通过反射,您可以调用私有方法并获取或设置私有属性和字段。

是的,可以通过编程从IIS的内核缓存中枚举和删除项

注意事项:

  • 枚举需要非平凡文本解析
  • 删除时需要大量丑陋的P/Invoke
  • 此外,您至少需要中等信任度(可能是完全信任度)才能执行以下操作
  • 在IIS的集成管道模式下,删除将不起作用
  • 枚举在IIS6上可能不起作用
枚举:

据我所知,枚举IIS内核缓存的唯一有文档记录的方法是IIS7及以上版本中提供的命令行应用程序(尽管您可能能够将NETSH helper DLL从V7复制到V6系统,但尚未尝试)

有关更多详细信息,请参阅。通过执行流程并解析文本结果,可以将其转换为“API”

大警告:我从未见过这个命令行应用程序在我的服务器上实际返回任何东西,即使是在经典模式下运行的应用程序。不知道为什么——但从其他在线帖子中我可以看出,这个应用程序确实有效。(例如)

如果您对进程创建非常敏感,并且感到雄心勃勃,那么NETSH命令是由带有文档化Win32接口的DLL实现的,因此您可以编写代码,假装它是NETSH.exe,并直接调用IIS的NETSH helper DLL。您可以使用作为此方法的起点。警告:模拟NETSH非常困难,因为接口是双向的:NETSH调用DLL,DLL调用NETSH。而且您仍然需要解析文本输出,因为NETSH接口是基于文本的,而不是像PowerShell或WMI那样基于对象的。如果是我,我只会生成一个NETSH进程

将来可能会支持此功能(这意味着比上面的hack更容易编程访问),但现在只有AFAIK-only NETSH支持此功能

无效:

我有好消息和坏消息要告诉你

好消息是:一旦知道要从IIS内核缓存中提取的项目的URL,就有一个Win32 API可用于在IIS6及更高版本上删除它。这可以通过P/Unjk(硬)从C ^调用,或者通过调用托管的C++包装器DLL调用。有关详细信息,请参阅MSDN上的

我尝试了一下所需的代码(附在下面)。警告:这很难看而且未经测试——它不会使我的IIS崩溃,但(见上文)我不知道如何使缓存枚举工作,因此我无法使用有效的URL调用它以从缓存中提取。如果可以让枚举工作,那么插入有效的URL(从而测试此代码)应该很容易

坏消息是:

  • 正如您可以从代码示例中猜到的,它在IIS7的集成管道模式下不起作用,只有在经典模式(或者IIS6,当然)下,ASP.NET作为ISAPI运行,并且可以访问ISAPI函数
  • 搞乱私人领域是一个大难题,可能会出现新版本
  • P/Invoke很难处理,需要(我相信)完全信任
下面是一些代码:

using System;
using System.Web;
using System.Reflection;
using System.Runtime.InteropServices;

public partial class Test : System.Web.UI.Page
{
    /// Return Type: BOOL->int
    public delegate int GetServerVariable();

    /// Return Type: BOOL->int
    public delegate int WriteClient();

    /// Return Type: BOOL->int
    public delegate int ReadClient();

    /// Return Type: BOOL->int
    public delegate int ServerSupportFunction();

    /// Return Type: BOOL->int
    public delegate int EXTENSION_CONTROL_BLOCK_GetServerVariable();

    /// Return Type: BOOL->int
    public delegate int EXTENSION_CONTROL_BLOCK_WriteClient();

    /// Return Type: BOOL->int
    public delegate int EXTENSION_CONTROL_BLOCK_ReadClient();

    /// Return Type: BOOL->int
    public delegate int EXTENSION_CONTROL_BLOCK_ServerSupportFunction();

    public static readonly int HSE_LOG_BUFFER_LEN = 80;

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
    public struct EXTENSION_CONTROL_BLOCK
    {
        /// DWORD->unsigned int
        public uint cbSize;

        /// DWORD->unsigned int
        public uint dwVersion;

        /// DWORD->unsigned int
        public uint connID;

        /// DWORD->unsigned int
        public uint dwHttpStatusCode;

        /// CHAR[HSE_LOG_BUFFER_LEN]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 80 /*HSE_LOG_BUFFER_LEN*/)]
        public string lpszLogData;

        /// LPSTR->CHAR*
        public System.IntPtr lpszMethod;

        /// LPSTR->CHAR*
        public System.IntPtr lpszQueryString;

        /// LPSTR->CHAR*
        public System.IntPtr lpszPathInfo;

        /// LPSTR->CHAR*
        public System.IntPtr lpszPathTranslated;

        /// DWORD->unsigned int
        public uint cbTotalBytes;

        /// DWORD->unsigned int
        public uint cbAvailable;

        /// LPBYTE->BYTE*
        public System.IntPtr lpbData;

        /// LPSTR->CHAR*
        public System.IntPtr lpszContentType;

        /// EXTENSION_CONTROL_BLOCK_GetServerVariable
        public EXTENSION_CONTROL_BLOCK_GetServerVariable GetServerVariable;

        /// EXTENSION_CONTROL_BLOCK_WriteClient
        public EXTENSION_CONTROL_BLOCK_WriteClient WriteClient;

        /// EXTENSION_CONTROL_BLOCK_ReadClient
        public EXTENSION_CONTROL_BLOCK_ReadClient ReadClient;

        /// EXTENSION_CONTROL_BLOCK_ServerSupportFunction
        // changed to specific signiature for invalidation callback
        public ServerSupportFunction_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK ServerSupportFunction;
    }
    /// Return Type: BOOL->int
    ///ConnID: DWORD->unsigned int
    ///dwServerSupportFunction: DWORD->unsigned int
    ///lpvBuffer: LPVOID->void*
    ///lpdwSize: LPDWORD->DWORD*
    ///lpdwDataType: LPDWORD->DWORD*
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public delegate bool ServerSupportFunction_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK(
        uint ConnID, 
        uint dwServerSupportFunction, // must be HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK
        out Callback_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK lpvBuffer, 
        out uint lpdwSize, 
        out uint lpdwDataType);

    public readonly uint HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK = 1040;

    // typedef HRESULT (WINAPI * PFN_HSE_CACHE_INVALIDATION_CALLBACK)(WCHAR *pszUrl);
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public delegate bool Callback_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK(
        [MarshalAs(UnmanagedType.LPWStr)]string url);

    object GetField (Type t, object o, string fieldName)
    {
        FieldInfo fld = t.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
        return fld == null ? null : fld.GetValue(o);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // first, get the ECB from the ISAPIWorkerRequest
        var ctx = HttpContext.Current;
        HttpWorkerRequest wr = (HttpWorkerRequest) GetField(typeof(HttpContext), ctx, "_wr");
        IntPtr ecbPtr = IntPtr.Zero;
        for (var t = wr.GetType(); t != null && t != typeof(object); t = t.BaseType)
        {
            object o = GetField(t, wr, "_ecb");
            if (o != null)
            {
                ecbPtr = (IntPtr)o;
                break;
            }
        }

        // now call the ECB callback function to remove the item from cache
        if (ecbPtr != IntPtr.Zero)
        {
            EXTENSION_CONTROL_BLOCK ecb = (EXTENSION_CONTROL_BLOCK)Marshal.PtrToStructure(
                ecbPtr, typeof(EXTENSION_CONTROL_BLOCK));
            uint dummy1, dummy2;

            Callback_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK invalidationCallback;
            ecb.ServerSupportFunction(ecb.connID,
                    HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK,
                    out invalidationCallback,
                    out dummy1,
                    out dummy2);

            bool success = invalidationCallback("/this/is/a/test");
        }
    }
}

这里进一步讨论了这个问题:(见评论)。有人有比上面提到的更好的解决方案吗?
using System;
using System.Web;
using System.Reflection;
using System.Runtime.InteropServices;

public partial class Test : System.Web.UI.Page
{
    /// Return Type: BOOL->int
    public delegate int GetServerVariable();

    /// Return Type: BOOL->int
    public delegate int WriteClient();

    /// Return Type: BOOL->int
    public delegate int ReadClient();

    /// Return Type: BOOL->int
    public delegate int ServerSupportFunction();

    /// Return Type: BOOL->int
    public delegate int EXTENSION_CONTROL_BLOCK_GetServerVariable();

    /// Return Type: BOOL->int
    public delegate int EXTENSION_CONTROL_BLOCK_WriteClient();

    /// Return Type: BOOL->int
    public delegate int EXTENSION_CONTROL_BLOCK_ReadClient();

    /// Return Type: BOOL->int
    public delegate int EXTENSION_CONTROL_BLOCK_ServerSupportFunction();

    public static readonly int HSE_LOG_BUFFER_LEN = 80;

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
    public struct EXTENSION_CONTROL_BLOCK
    {
        /// DWORD->unsigned int
        public uint cbSize;

        /// DWORD->unsigned int
        public uint dwVersion;

        /// DWORD->unsigned int
        public uint connID;

        /// DWORD->unsigned int
        public uint dwHttpStatusCode;

        /// CHAR[HSE_LOG_BUFFER_LEN]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 80 /*HSE_LOG_BUFFER_LEN*/)]
        public string lpszLogData;

        /// LPSTR->CHAR*
        public System.IntPtr lpszMethod;

        /// LPSTR->CHAR*
        public System.IntPtr lpszQueryString;

        /// LPSTR->CHAR*
        public System.IntPtr lpszPathInfo;

        /// LPSTR->CHAR*
        public System.IntPtr lpszPathTranslated;

        /// DWORD->unsigned int
        public uint cbTotalBytes;

        /// DWORD->unsigned int
        public uint cbAvailable;

        /// LPBYTE->BYTE*
        public System.IntPtr lpbData;

        /// LPSTR->CHAR*
        public System.IntPtr lpszContentType;

        /// EXTENSION_CONTROL_BLOCK_GetServerVariable
        public EXTENSION_CONTROL_BLOCK_GetServerVariable GetServerVariable;

        /// EXTENSION_CONTROL_BLOCK_WriteClient
        public EXTENSION_CONTROL_BLOCK_WriteClient WriteClient;

        /// EXTENSION_CONTROL_BLOCK_ReadClient
        public EXTENSION_CONTROL_BLOCK_ReadClient ReadClient;

        /// EXTENSION_CONTROL_BLOCK_ServerSupportFunction
        // changed to specific signiature for invalidation callback
        public ServerSupportFunction_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK ServerSupportFunction;
    }
    /// Return Type: BOOL->int
    ///ConnID: DWORD->unsigned int
    ///dwServerSupportFunction: DWORD->unsigned int
    ///lpvBuffer: LPVOID->void*
    ///lpdwSize: LPDWORD->DWORD*
    ///lpdwDataType: LPDWORD->DWORD*
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public delegate bool ServerSupportFunction_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK(
        uint ConnID, 
        uint dwServerSupportFunction, // must be HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK
        out Callback_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK lpvBuffer, 
        out uint lpdwSize, 
        out uint lpdwDataType);

    public readonly uint HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK = 1040;

    // typedef HRESULT (WINAPI * PFN_HSE_CACHE_INVALIDATION_CALLBACK)(WCHAR *pszUrl);
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public delegate bool Callback_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK(
        [MarshalAs(UnmanagedType.LPWStr)]string url);

    object GetField (Type t, object o, string fieldName)
    {
        FieldInfo fld = t.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
        return fld == null ? null : fld.GetValue(o);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // first, get the ECB from the ISAPIWorkerRequest
        var ctx = HttpContext.Current;
        HttpWorkerRequest wr = (HttpWorkerRequest) GetField(typeof(HttpContext), ctx, "_wr");
        IntPtr ecbPtr = IntPtr.Zero;
        for (var t = wr.GetType(); t != null && t != typeof(object); t = t.BaseType)
        {
            object o = GetField(t, wr, "_ecb");
            if (o != null)
            {
                ecbPtr = (IntPtr)o;
                break;
            }
        }

        // now call the ECB callback function to remove the item from cache
        if (ecbPtr != IntPtr.Zero)
        {
            EXTENSION_CONTROL_BLOCK ecb = (EXTENSION_CONTROL_BLOCK)Marshal.PtrToStructure(
                ecbPtr, typeof(EXTENSION_CONTROL_BLOCK));
            uint dummy1, dummy2;

            Callback_HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK invalidationCallback;
            ecb.ServerSupportFunction(ecb.connID,
                    HSE_REQ_GET_CACHE_INVALIDATION_CALLBACK,
                    out invalidationCallback,
                    out dummy1,
                    out dummy2);

            bool success = invalidationCallback("/this/is/a/test");
        }
    }
}