zoukankan      html  css  js  c++  java
  • .net操作IIS,新建网站,新建应用程序池,设置应用程序池版本,设置网站和应用程序池的关联

    ServerManager类用来操作IIS,提供了很多操作IIS的API。使用ServerManager必须引用Microsoft.Web.Administration.dll,具体路径为:%windir%System32InetsrvMicrosoft.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%System32InetsrvMicrosoft.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 = "8998";
            private static string webPath = @"C:WorkPublishedSelfHostedIPSRemoteWeb";
    
            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;
                }
            }
        }
    }
  • 相关阅读:
    C#操作注册表
    redis在Windows下以后台服务一键搭建哨兵(主从复制)模式(多机)
    redis在Windows下以后台服务一键搭建哨兵(主从复制)模式(单机)
    redis在Windows下以后台服务一键搭建集群(多机器)
    redis在Windows下以后台服务一键搭建集群(单机--伪集群)
    Redis集群维护、运营的相关命令与工具介绍
    Protocol Buffer学习教程之类库应用(四)
    ArcGIS镶嵌数据集的应用-栅格显示范围控制
    QGIS加载与编辑视图图层
    ArcGIS属性域Domain探索
  • 原文地址:https://www.cnblogs.com/zhengshuangliang/p/4303267.html
Copyright © 2011-2022 走看看