• asp.net 自定义节配置 (configSections下的section)


    <configuration>
      <configSections>
      <!--1.自定义一个节 CustomSection -->   
     <section name="CustomSection"
            type="CustomSection.BatchingHostingSettings, 
                  CustomSection"/>
     <!--2.为CustomSection 这个节配置子节点-->   
      <CustomSection>
        <add type="CustomSection.FooService, CustomSection"/>
        <add type="CustomSection.BarService, CustomSection"/>
        <add type="CustomSection.BazService, CustomSection"/>
      </CustomSection>
    </configuration>
    3.定义转换器
       public class AssemblyQualifiedTypeNameConverter : ConfigurationConverterBase
        {
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                string typeName = (string)value;
                if (string.IsNullOrEmpty(typeName))
                {
                    return null;
                }
                Type result = Type.GetType(typeName, false);
                if (result == null)
                {
                    throw new ArgumentException(string.Format("不能加载类型"{0}"", typeName));
                }
                return result;
            }
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                Type type = value as Type;
                if (null == type)
                {
                    throw new ArgumentNullException("value");
                }
                return type.AssemblyQualifiedName;
            }
        }
    4.配置元素集合的元素类型的配置
     public class ServiceTypeElement : ConfigurationElement
        {
            [ConfigurationProperty("type", IsRequired = true)]
            [TypeConverter(typeof(AssemblyQualifiedTypeNameConverter))]
            public Type ServiceType
            {
                get { return (Type)this["type"]; }
                set { this["type"] = value; }
            }
        }
    5.定义配置元素集合
        public class ServiceTypeElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new ServiceTypeElement();
            }
            protected override object GetElementKey(ConfigurationElement element)
            {
                ServiceTypeElement serviceTypeElement = (ServiceTypeElement)element;
                return serviceTypeElement.ServiceType.MetadataToken;
            }
        }
    6.自定义节对应的类的定义
     public class BatchingHostingSettings : ConfigurationSection
        {
            [ConfigurationProperty("", IsDefaultCollection = true)]
            public ServiceTypeElementCollection ServiceTypes
            {
                get { return (ServiceTypeElementCollection)this[""]; }
            }
    
            public static BatchingHostingSettings GetSection()
            {
                return ConfigurationManager.GetSection("CustomSection")
                            as BatchingHostingSettings;
            }
        }
    7. 对应要解析的类
     public class FooService
        {
        }
        public class BarService
        {
        }
        public class BazService
        {
        }
    8.ServiceHostCollection
     public class ServiceHostCollection : Collection<ServiceHost>, IDisposable
        {
    
            public ServiceHostCollection(params Type[] serviceTypes)
            {
    
                BatchingHostingSettings settings = BatchingHostingSettings.GetSection();
                foreach (ServiceTypeElement element in settings.ServiceTypes)
                {
                    this.Add(element.ServiceType);
                }
    
                if (null != serviceTypes)
                {
                    Array.ForEach<Type>(serviceTypes, serviceType => this.Add(new ServiceHost(serviceType)));
                }
            }
            public void Add(params Type[] serviceTypes)
            {
                if (null != serviceTypes)
                {
                    Array.ForEach<Type>(serviceTypes, serviceType => this.Add(new ServiceHost(serviceType)));
                }
            }
            public void Open()
            {
                foreach (ServiceHost host in this)
                {
                    host.Open();
                }
            }
            public void Dispose()
            {
                foreach (IDisposable host in this)
                {
                    host.Dispose();
                }
            }
        }
    9.演示一个服务寄宿的例子
     protected void Page_Load(object sender, EventArgs e)
            {
                ServiceHostCollection list = new ServiceHostCollection();
                foreach (ServiceHost host in list)
                {
                    host.Opened += (sender1, arg1) =>
                    {
                     System.Diagnostics.Debug.WriteLine("服务{0}开始监听",
                   (sender1 as ServiceHost).Description.ServiceType);
                    };
                }
                list.Open();
                Console.Read();
    
            }
  • 相关阅读:
    递归函数的原理
    html布局-子div浮动后,父容器撑不开解决
    权限设计——控制显示字段——设计思考
    winform上传文件到服务器——资料整理
    windows server自动化发布——技术积累与整理
    Docker——概念学习
    javascript中的var,let,const关键字
    Web前端面试图
    上传与预览图片
    每个程序员都应该知道延迟数—Latency Numbers Every Programmer Should Know
  • 原文地址:https://www.cnblogs.com/kexb/p/6603528.html
Copyright © 2020-2023  润新知