Пример #1
0
        public void ProviderFiltering()
        {
            var settings = @"<Config>
            <configProtectedData>
            <providers>
            <add name='ClearingProvider' type='System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' keyContainerName='SampleKeys' useMachineContainer='true' />
            <clear/>
            <add name='RsaProvider' type='System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' keyContainerName='SampleKeys' useMachineContainer='true' />
            <add name='MissedProvider' type='System.Configuration.DpapiProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' />
            <add name='DpapiProvider' type='System.Configuration.DpapiProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' />
            </providers>
            </configProtectedData>
            </Config>".ToXmlSettings().ToAppSettings();

            var names = new List<string>();

            var providers = new ProviderLoader()
                .SubscribeLoading((s, e) =>
                {
                    names.Add(e.Name);
                    if (e.Name == "MissedProvider")
                        e.Canceled = true;
                })
                .LoadAppSettings(settings)
                .Providers;

            CollectionAssert.AreEqual(new string[] { "ClearingProvider", "RsaProvider", "MissedProvider", "DpapiProvider" }, names);
            Assert.IsNull(providers.Get("MissedProvider"));
            Assert.IsNull(providers.Get("ClearingProvider"));
            Assert.IsNotNull(providers.Get("RsaProvider"));
            Assert.IsNotNull(providers.Get("DpapiProvider"));
        }