asp.net 读取RedisSessionStateProvider配置

最近项目遇到需要读取RedisSessionStateProvider配置文件applicationName节点,如:

asp.net 读取RedisSessionStateProvider配置_第1张图片


读取的方法有很多:

1直接读取web.config文件,

  void test1()
        {
            SessionStateSection config = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");
            string providerName = config.CustomProvider;
            ProviderSettings ps = config.Providers[providerName];
            string str = ps.Parameters["applicationName"];
        }

2通过SessionStateStoreProviderBase来访问对象属性, 可惜的是对应的属性及类都是内部的,所以我们需要用到反射。

asp.net 读取RedisSessionStateProvider配置_第2张图片

asp.net 读取RedisSessionStateProvider配置_第3张图片

所以编码为如下:

 void test2()
        {
            var type = typeof(RedisSessionStateProvider);
            //获取RedisSessionStateProvider的静态configuration字段
            var configField = type.GetField("configuration", BindingFlags.Static | BindingFlags.NonPublic);
            var configValue = configField.GetValue(new RedisSessionStateProvider());
            type = configField.FieldType;
            //获取configuration的ApplicationName属性
            var prop = type.GetProperty("ApplicationName");
            var obj = prop.GetValue(configValue).ToString();
        }

        T GetSessionStateStoreProviderBase() where T : SessionStateStoreProviderBase, new()
        {
            SessionStateSection config = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");
            string providerName = config.CustomProvider;
            ProviderSettings ps = config.Providers[providerName];
            ProviderBase provide = ProvidersHelper.InstantiateProvider(ps, typeof(T));
            return (provide as T);
        }

3 次方法和方法2一样好,直接放问实例或对象的属性,ProviderConfiguration和configuration属性都是内部的,我们可以不通过修改源码(或者用mono ceil 来修改dll文件)使ProviderConfiguration类和configuration属性成为public。 那么code就很简单了

string applicationName1 = RedisSessionStateProvider.configuration.ApplicationName;


4 用mono ceil 来修改dll文件,方法3是找到源码直接修改,这里方法4是没有源码,用monon修改的。我们新建一个控制台应用程序,添加


    using System;
    using System.IO;
    using System.Linq;
    using Mono.Cecil;
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Microsoft.Web.RedisSessionStateProvider.dll");
            AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(filePath);
            TypeDefinition[] types = asm.MainModule.Types.ToArray();

            //修改ProviderConfiguration为public
            TypeDefinition typeConfiguration = types.FirstOrDefault(x => x.Name == "ProviderConfiguration");
            typeConfiguration.IsPublic = true;

            //修改ProviderConfiguration的字段为public
            TypeDefinition typeRedisProvide = types.FirstOrDefault(x => x.Name == "RedisSessionStateProvider");
            FieldDefinition filedConfiguration = typeRedisProvide.Fields.ToArray().FirstOrDefault(x => x.Name == "configuration");
            filedConfiguration.IsPublic = true;

            //修改程序集的签名
            foreach (ModuleDefinition item in asm.Modules)
            {
                var assemblyName = item.Assembly.Name;
                assemblyName.PublicKey = GetPublicKey();
                assemblyName.PublicKeyToken = GetPubliKeyToken();
            }          
            //保存dll文件
            filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Microsoft.Web.RedisSessionStateProvider2.dll");
            asm.Write(filePath);
            
            //Console.ReadLine();
        }
        private static byte[] GetPublicKey()
        {
            string s = @"00240000048000009400000006020000002400005253413100040000010001005b0b4a219a91bee970338aadc8890eb73db3a87500c9b82cf605bebdb64f74769a213fbdf0476641c728281ffb5bb700c7f0a55f5407bfe984eb4abfd947ddbc6476c8ea4c5207421750dc2385fd99163cbb12df87190793552334614985258c830b8d960b5e21c5a0cc6be0941b3d1777c8a09d22db62ed59e5540822c6289b";
            return StrToHexBytes(s);
        }
        private static byte[] GetPubliKeyToken()
        {
            string s = "f68305d2ab29aee8";
            return StrToHexBytes(s);
        }
        private static byte[] StrToHexBytes(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
    }
GetPublicKey 和GetPubliKeyToken 字符串来源于

asp.net 读取RedisSessionStateProvider配置_第4张图片


asp.net 读取RedisSessionStateProvider配置_第5张图片

先修改Microsoft.Web.RedisSessionStateProvider.dll 文件 另存为Microsoft.Web.RedisSessionStateProvider2.dll,在放到特定目录下 改为Microsoft.Web.RedisSessionStateProvider.dll 对其修改签名。

代用方式和方法3一样 就一行code   string applicationName1 = RedisSessionStateProvider.configuration.ApplicationName;

http://blog.csdn.net/ma_jiang/article/details/7936235

整个项目需要用nuget 来安装Microsoft.Web.RedisSessionStateProvider。至于用mono ceil 来修改dll文件, 网上有你很多, 一定给要记住修改签名哦哦

当然偶尔也有写入Config的需求:比如我想把redis 连接串放到一个单独的config文件里面如:



那么什么时候可以把它覆盖到我们配置对象中了:

       protected void Application_Start(object sender, EventArgs e)
        {
            SessionStateSection config = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");
            string providerName = config.CustomProvider;
            ProviderSettings ps = config.Providers[providerName];
            // string str = ps.Parameters["applicationName"];
            string connStr = ConfigurationManager.AppSettings["RedisConnectionString"];
            ps.Parameters["connectionString"] = connStr;
        }

读取有几种方式那么 写入也就有对应的几种方式。


你可能感兴趣的:(ASP.NET)