首页 技术 正文
技术 2022年11月17日
0 收藏 528 点赞 2,168 浏览 2976 个字

ServerManager类用来操作IIS,提供了很多操作IIS的API。使用ServerManager必须引用Microsoft.Web.Administration.dll,具体路径为:%windir%\System32\Inetsrv\Microsoft.Web.Administration.dll

以下代码展示了如何将自己的ASP.NET网站自动部署到IIS的过程,其中包括,新建网站-》新建应用程序池-》设置网站和应用程序池的关联-》重启网站和应用程序池

具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;namespace AutoConfigureIIS
{
internal class Program
{
//don't forget to add a reference to %windir%\System32\Inetsrv\Microsoft.Web.Administration.dll private static ServerManager serverMgr = new ServerManager();
private static string applicationPoolName = "IPSRemoteApplicationPool";
private static string applicationPoolVersion = "v4.0";
private static string websiteName = "IPSRemoteSite";
private static string port = "";
private static string webPath = @"C:\Work\Published\SelfHostedIPSRemoteWeb"; private static void Main(string[] args)
{
DeployIPSWeb(); Console.Read();
} /// <summary>
/// Auto Configure IPS WebSite to IIS
/// </summary>
private static void DeployIPSWeb()
{
//create Application Pool
if (!IsApplicationPoolExists(applicationPoolName))
{
CreateApplicationPool(applicationPoolName);
}
//create website
if (!IsWebsiteExists(websiteName))
{
CreateWebSite(websiteName, port, webPath);
} Site site = FindSiteBySiteName(websiteName); // get site by Index or by siteName
ApplicationPool appPool = FindApplicationPoolByName(applicationPoolName);
// get appPool by Index or by appPoolName site.Stop();
site.ApplicationDefaults.ApplicationPoolName = appPool.Name; foreach (var item in site.Applications)
{
item.ApplicationPoolName = appPool.Name;
} serverMgr.CommitChanges(); // this one is crucial!!! see MSDN:
// Updates made to configuration objects must be explicitly written to the configuration
// system by using the CommitChanges method!!
site.Start();
appPool.Recycle();
} /// <summary>
/// Create WebSite sitename to IIS with the arguments
/// </summary>
/// <param name="sitename"></param>
/// <param name="portstr"></param>
/// <param name="webPathstr">website code path</param>
public static void CreateWebSite(string sitename, string portstr, string webPathstr)
{
serverMgr.Sites.Add(sitename, "http", "*:" + portstr + ":", webPathstr); serverMgr.CommitChanges();
} public static void CreateApplicationPool(string poolname)
{
ApplicationPool newPool = serverMgr.ApplicationPools.Add(poolname);
newPool.ManagedRuntimeVersion = applicationPoolVersion;
serverMgr.CommitChanges();
} public static bool IsWebsiteExists(string strWebsitename)
{
Site site = FindSiteBySiteName(strWebsitename);
if (site == null)
return false;
else
return true;
} public static Site FindSiteBySiteName(string strWebsitename)
{
SiteCollection sitecollection = serverMgr.Sites;
foreach (Site site in sitecollection)
{
if (site.Name == strWebsitename.ToString())
{
return site;
}
}
return null;
} public static ApplicationPool FindApplicationPoolByName(string poolName)
{
ApplicationPoolCollection poolcollection = serverMgr.ApplicationPools;
foreach (ApplicationPool pool in poolcollection)
{
if (pool.Name == poolName.ToString())
{
return pool;
}
}
return null;
} public static bool IsApplicationPoolExists(string appPool)
{
ApplicationPool pool = FindApplicationPoolByName(appPool);
if (pool == null)
{
return false;
}
else
{
return true;
}
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903