通过代码动态创建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 ...
随机推荐
- chrome inspect 远程调试H5
chrome://inspect/#devices 一个内置于chrome的远程调试指令,满足远程调试的几个必须条件 1,能够访问https://chrome-devtools-frontend.ap ...
- A Node Influence Based Label Propagation Algorithm for Community detection in networks 文章算法实现的疑问
这是我最近看到的一篇论文,思路还是很清晰的,就是改进的LPA算法.改进的地方在两个方面: (1)结合K-shell算法计算量了节点重重要度NI(node importance),标签更新顺序则按照NI ...
- .Net Core 发布WindowsServices
项目代码文件夹 执行命令 dotnet publish -c Release -r win-x64
- (转)CentOS 7 单用户模式+救援模式
原文:http://blog.51cto.com/asd9577/1931442 https://www.cnblogs.com/zhangzeyu/p/6379754.html-------Cent ...
- (转)使用 DB2 HADR 选择用于灾难恢复的 SUPERASYNC 模式
使用 DB2 HADR 选择用于灾难恢复的 SUPERASYNC 模式 Vishnu G 和 Hemant Singh2013 年 6 月 25 日发布 WeiboGoogle+用电子邮件发送本页面 ...
- python3 判断大小端的一种方法
这里用到了array.array('H', [1])来测试大小端,[1]可以转化为十六进制的0x0001,占两位,00位高位, 01位低位,通过第一位就可以判断大小端. 如果是小端,则转化为bytes ...
- Check类的validate方法解读
此方法的实现如下: public void validate(JCTree tree, Env<AttrContext> env, boolean checkRaw) { Validato ...
- docker 使用swarm overlay网络时,报“network xx not manually attachable”错误解决
当使用swarm的overlay网络,在该网络中运行容器时报“network xx not manually attachable”的错误 docker network create -d overl ...
- 删除none 的images报错 image has dependent child images 解决办法
这个错是因为在要删除的images之后创建了该images的父images 方法: docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Pa ...
- centos6 free 和 centos 7的free 的差异与对比
目录 一 centos6 free 常用参数和含义 centos6 free 命令示例 free 值讲解 计算公式 二 centos7 free 常用的参数 centos7 free 命令示例 计算公 ...