通过代码动态创建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();
}
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;
}
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);
}
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();
}
通过代码动态创建IIS站点的更多相关文章
- 使用appcmd命令创建iis站点及应用程序池
参考文章:iis7 appcmd的基础命令及简单用法 验证环境:Windows 7 IIS7 AppCmd.exe工具所在目录 C:\windows\sytstem32\inetsrv\目录下, ...
- C# 创建iis站点以及IIS站点属性,iis不能启动站点
DontLog = False是否将客户端的请求写入日志文件 2011年04月09日 #region CreateWebsite 新增网站 public string CreateWebSite(st ...
- unity3d通过代码动态创建销毁游戏对象
只能动态创建内部提供的游戏对象,代码如下: //按下C后创建 if (Input.GetKeyDown (KeyCode.C)) { GameObject s1 = GameObject.Create ...
- C#实现动态发布IIS站点帮助类
准备工作: 1.引用 System.DirectoryServices 系统程序集 2.引用 Microsoft.Web.Administration 程序集,类库位置在 C:\Windows\Sys ...
- 代码动态创建checkbox
根据数据库的内容动态创建Checkbox控件并显示在Panel上 dataset ds=new dataset(); CheckBox[ ] cb=new CheckBox[ds.tables[0]. ...
- [2015-11-23]分享一个批处理脚本,创建iis站点及程序池
建站批处理 batch_createSites.bat @echo off rem 以管理员身份执行本脚本,可添加多条call 以建立多个站点 call path\to\createSites.bat ...
- C#创建IIS站点及相应的应用程序池,支持IIS6.0+Windows Server 2003. 使用Builder设计模式
测试项目结构: PS:IIS6UtilsBuilder, IIS7UtilsBuilder,IISUtilsBuilder以及IISDirector为Builder设计模式实现的核心代码.Progra ...
- 针对windowsserver 创建iis站点访问出错的解决方案(HTTP 错误 500.19 - Internal Server Error)
错误如下: 服务器错误 Internet信息服务 7.0 错误摘要HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详 ...
- cmd 批处理创建 IIS 站点
windows 创建站点命令 appcmd C:\Windows\System32\inetsrv\appcmd.exe SITE 虚拟站点的管理 APP 管理应用程序 VDIR 管理虚拟目录 APP ...
随机推荐
- 【hdu6035】 Colorful Tree dfs序
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题目大意:给你一棵树,树上每个节点都有一个颜色. 现在定义两点间的距离为两点最短路径上颜色集合 ...
- vue-devtools必备工具
1.github下载地址:https://github.com/vuejs/vue-devtools 2.下载安成之后打开cmd进入vue-devtools文件夹把依赖装好npm install 之后 ...
- javac之Inferring Type Arguments Based on Actual Arguments
We use the following notational conventions in this section: Type expressions are represented using ...
- Architecture of a Java Compiler
Architectural Overview A modern optimizing compiler can be logically divided into four parts: Th ...
- visual studio 2008 快捷键
Ctrl+m+Crtr+o折叠所有大纲Ctrl+M+Crtr+P: 停止大纲显示Ctrl+K+Crtr+C: 注释选定内容Ctrl+K+Crtr+U: 取消选定注释内容Ctrl+J : 列出成员 智能 ...
- 机器学习--聚类系列--DBSCAN算法
DBSCAN算法 基本概念:(Density-Based Spatial Clustering of Applications with Noise) 核心对象:若某个点的密度达到算法设定的阈值则其为 ...
- freepbx13通话无声音通话自动挂断
在阿里云上用脚本一键搭建好了freepbx13,但是在创建好sip分机之后,打电话没有声音,双方都听不到对方的声音.这个是nat问题. 这里有个坑我提醒下大家,就是我们最好不要用台式电脑进行测试通话. ...
- Linux时间命令
Linux一般有系统时间和硬件时间之分,date命令是显示和操作系统时间:hwclock用来操作硬件时间(日期).日期和时间很重要,比如错误的日期和时间会导致你不能编译程序. 1 date 用法: ...
- Java Date SimpleDateFormat
public static void main(String[] args) { long millis = 1492741275301L; Calendar calendar = Calendar. ...
- VS中调试查看DataTable和DataSet时未能加载此自定义查看器解决方法
在网上找了几个方法,感觉不太实用,最后自己找到了问题所在 VS2017中选择调试-选项-常规中的使用托管兼容模式取消勾选.之后就可以了