| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:ServiceHostCollection.cs
- * 2.功能描述:服务集合
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/09/16 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections.ObjectModel;
- using System.ServiceModel;
- using Dongke.IBOSS.PRD.WCF.Services.ConfigSetting;
- namespace Dongke.IBOSS.PRD.WCF.Services
- {
- /// <summary>
- /// 服务集合
- /// </summary>
- public class ServiceHostCollection : Collection<ServiceHost>, IDisposable
- {
- #region 属性
- /// <summary>
- /// 服务名
- /// </summary>
- public string ServiceName
- {
- get
- {
- return "DKService";
- }
- }
- /// <summary>
- /// IP
- /// </summary>
- public string IP
- {
- get;
- set;
- }
- /// <summary>
- /// 端口
- /// </summary>
- public string Port
- {
- get;
- set;
- }
- #endregion
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- public ServiceHostCollection()
- {
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 初始化
- /// </summary>
- public void Init()
- {
- BatchingHostingSettings settings = BatchingHostingSettings.GetSection();
- foreach (ServiceTypeElement element in settings.ServiceTypes)
- {
- this.AddServiceHost(element.ServiceType);
- }
- }
- /// <summary>
- /// 追加服务类型
- /// </summary>
- /// <param name="serviceTypes"></param>
- public void AddServiceHosts(params Type[] serviceTypes)
- {
- if (null != serviceTypes)
- {
- Array.ForEach<Type>(serviceTypes, serviceType => AddServiceHost(serviceType));
- }
- }
- /// <summary>
- /// 追加服务类型
- /// </summary>
- /// <param name="serviceType"></param>
- public void AddServiceHost(Type serviceType)
- {
- Uri baseAddress = new Uri(string.Format("http://{0}:{1}/{2}/{3}", this.IP, this.Port, this.ServiceName, serviceType.Name));
- ServiceHost host = new ServiceHost(serviceType, baseAddress);
- this.Add(host);
- }
- /// <summary>
- /// 启动服务
- /// </summary>
- public void Open()
- {
- foreach (ServiceHost host in this)
- {
- host.Open();
- }
- }
- /// <summary>
- /// 销毁
- /// </summary>
- public void Dispose()
- {
- foreach (IDisposable host in this)
- {
- host.Dispose();
- }
- this.Clear();
- }
- #endregion
- }
- }
|