打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#实现一键发布IIS网站
using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace MedicalServiceMIS.Common
{
/// <summary>
/// 网站发布帮助类
/// </summary>
public class PublishWebHelper
{
private string WebName;
private int Port;
private string ServerDomainName;
public PublishWebHelper(string WebName, int Port, string ServerDomainName = null)
{
this.WebName = WebName;
this.Port = Port;
this.ServerDomainName = ServerDomainName;
}

public bool Execute(string WebsiteDirectory, out string ErrorContent)
{
ServiceController service = ServiceController.GetServices("127.0.0.1").FirstOrDefault(x => x.ServiceName == "W3SVC");

if (service is null)
{
ErrorContent = "服务器尚未安装 IIS 服务模块!";
return false;
}

if (!System.IO.Directory.Exists(WebsiteDirectory))
{
ErrorContent = "指定目录不存在!";
return false;
}
try
{
ServerManager iismanager = new ServerManager();
//判断应用程序池是否存在
if (iismanager.ApplicationPools[WebName] != null)
{
//移除应用程序池
iismanager.ApplicationPools.Remove(iismanager.ApplicationPools[WebName]);
}
//判断web应用程序是否存在
if (iismanager.Sites[WebName] != null)
{
///移除应用程序
iismanager.Sites.Remove(iismanager.Sites[WebName]);
}
//建立web应用程序(第二个参数为安装文件的地址)
iismanager.Sites.Add(WebName, WebsiteDirectory, Port);

//添加web应用程序池
ApplicationPool pool = iismanager.ApplicationPools.Add(WebName);
#region 应用程序池配置
pool.StartMode = StartMode.AlwaysRunning;//启动模式
pool.Recycling.PeriodicRestart.Schedule.Add(new TimeSpan(03, 00, 00));//凌晨回收一次资源
pool.Cpu.Limit = 50000;//限制最大CPU 50%
pool.Cpu.Action = ProcessorAction.ThrottleUnderLoad;//竞争cpu时限制使用最大cpu 百分比
//设置web应用程序池的Framework版本(注意版本号大小写问题)
pool.ManagedRuntimeVersion = "v4.0";
//设置是否启用32为应用程序
pool.Enable32BitAppOnWin64 = true;
///设置专用内存限制 1G 超出释放一次
pool.Recycling.PeriodicRestart.PrivateMemory = 512000 * 2;
#endregion
//设置web网站的应用程序池
var website = iismanager.Sites[WebName];

website.Applications[0].ApplicationPoolName = WebName;
if (!string.IsNullOrEmpty(ServerDomainName))
{
string str = website.Bindings[0].Host.Split(new char[] { '.' })[0];
string bindingInformation = $"*:{Port}:{str}{ServerDomainName}";
website.Bindings.Add(bindingInformation, "http");
}
//提交更改
iismanager.CommitChanges();
}
catch (Exception ex)
{
ErrorContent = ex.Message;
return false;
}
ErrorContent = "部署成功";
return true;
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
IIS7 自动启用应用程序池
IIS 7管理API——Microsoft.Web.Administration介绍
java类名尖括号
【关于unity对象池的用法(话痨版)(c#)】
2003装IIs
IIS7
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服