zoukankan      html  css  js  c++  java
  • 动态创建IIS站点

    对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

  • 相关阅读:
    1123 Is It a Complete AVL Tree (30分)---如何建立平衡二叉搜索树(LL型RR型LR型RL型)+如何判断完全二叉树
    1021 Deepest Root (25 分)(经典搜索)
    PAT甲 1020 Tree Traversals (树的后序中序->层序)
    (数据结构)如何根据树的后序中序遍历求树的前序遍历
    习题2.3 数列求和-加强版 (模拟)
    PAT甲级 1051 Pop Sequence (25) && 2019天梯赛 L2-032 彩虹瓶 (25 分) (模拟+栈)
    PAT甲级 Are They Equal (25) (恶心模拟)
    PAT甲级1059 Prime Factors (25)(素数筛+求一个数的质因子)
    IO 模型
    Nginx 反向代理
  • 原文地址:https://www.cnblogs.com/hofmann/p/12174723.html
Copyright © 2011-2022 走看看