码迷,mamicode.com
首页 > 其他好文 > 详细

动态创建IIS站点

时间:2020-01-10 10:22:45      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:always   max   ring   ges   功能   rect   bsp   period   主机   

对WebApi进行单元测试时,一般需要一个IIS站点,一般的做法,是通过写一个批处理的bat脚本来实现,其实通过编码,也能实现该功能。

   主要有关注三点:应用程序池、Web站点、绑定(协议类型:http、https,IP地址,端口,主机名)

 1.总体代码

技术图片
  var webSite = app.WebSite;
 
            using (var sm = new ServerManager())
            {
                //创建应用程序池
                var appPool = sm.ApplicationPools.FirstOrDefault(ap => ap.Name.Equals(webSite.PoolName));
 
                if (appPool == null)
                {
                    CreateAppPool(sm.ApplicationPools, webSite.PoolName);
                }
 
                //创建Web站点
                var site = sm.Sites.FirstOrDefault(s => s.Name.Equals(webSite.SiteName));
 
                if (site == null)
                {
                    CreateWebSite(sm.Sites, webSite, app.InstallPath);
                }
 
                sm.CommitChanges();
            }
View Code

2.创建应用程序池:

技术图片
 /// <summary>
        /// 创建应用程序池
        /// </summary>
        /// <param name="appPools"></param>
        /// <param name="appPoolName"></param>
        private void CreateAppPool(ApplicationPoolCollection appPools, string appPoolName)
        {
            var appPool = appPools.Add(appPoolName);
 
            //是否自启动
            appPool.AutoStart = true;
            //队列长度
            appPool.QueueLength = 10000;
            //启动模式
            appPool.StartMode = StartMode.AlwaysRunning;
            //回收时间间隔
            appPool.Recycling.PeriodicRestart.Time = new TimeSpan();
            //闲置超时
            appPool.ProcessModel.IdleTimeout = new TimeSpan();
            //最大工作进程数
            appPool.ProcessModel.MaxProcesses = 1;
        }
View Code

3.创建站点

技术图片
  /// <summary>
        /// 创建Web站点
        /// </summary>
        /// <param name="sites"></param>
        /// <param name="webSite"></param>
        /// <param name="physicalPath"></param>
        private void CreateWebSite(SiteCollection sites, WebSite webSite, string physicalPath)
        {
            Site site = null;
            bool isSiteCreated = false;
 
            foreach (var binding in webSite.Bindings)
            {
                var bingdingInfo = ConstructBindingInfo(binding);
 
                if (!isSiteCreated)
                {
                    site = sites.Add(webSite.SiteName, binding.BindingType, bingdingInfo, physicalPath);
 
                    //是否自启动
                    site.ServerAutoStart = true;
 
                    isSiteCreated = true;
                }
                else
                {
                    site.Bindings.Add(bingdingInfo, binding.BindingType);
                }
            }
 
            var root = site.Applications["/"];
 
            //设置应用程序池
            root.ApplicationPoolName = webSite.PoolName;
            //设置虚拟目录
            //  root.VirtualDirectories["/"].PhysicalPath = pathToRoot;
            //预加载
            root.SetAttributeValue("preloadEnabled", true);
        }
View Code

4.创建绑定

技术图片
  /// <summary>
        /// 构建绑定信息
        /// </summary>
        /// <param name="binding"></param>
        /// <returns></returns>
        private string ConstructBindingInfo(WebSiteBinding binding)
        {
            var sb = new StringBuilder();
 
            if (!string.IsNullOrEmpty(binding.IP))
            {
                sb.Append(binding.IP);
            }
            else
            {
                sb.Append("*");
            }
 
            sb.Append(":");
 
            sb.Append(binding.Port);
 
            sb.Append(":");
 
            if (!string.IsNullOrEmpty(binding.HostName))
            {
                sb.Append(binding.HostName);
            }
            else
            {
                sb.Append(string.Empty);
            }
 
            return sb.ToString();
        }
View Code

 

转载于:https://www.cnblogs.com/liugh/p/8684696.html

动态创建IIS站点

标签:always   max   ring   ges   功能   rect   bsp   period   主机   

原文地址:https://www.cnblogs.com/hofmann/p/12174723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!