利用C#建立IIS website
資訊日新月異,身為一個技術人員該如何自處?
首先如果你是從ASP.PHP等語言起家的程式員,你一定有非常硬的底子因為你總是手動一行一行的寫程式,甚至筆記本開起來就可以開始撰寫了,又因為公司總想要一人多用,你還得學習架站跟管理server,必要的時候機房你也得負責,想當然爾,寫php的對linux必須非常熟悉,如果不熟,就必須想盡辦法找套件找資料把他架在windows上,相當麻煩,而如果你是寫microsoft系列的語言,你又必須把windows作業系統熟透了吧?
Microsoft 對我而言,他給我很多學習的"機會",從asp.net2.0 3.5 到4.0,從server 2003到2008,SQL2005 2008,vs2005 2008 2010..,iis 567,IE678,甚至word都有2003 2007 2010,你說是不是有點麻煩?好了,現在課題來了,要把使用vs2005 with SQL2005開發的網站移植到使用server2008 (IIS7)的作業系統上運作,怎樣做最快,最省工,最不易出錯?
1.花時間買新書研究IIS7的設定,找資料解決asp.net各版本的組態問題
2.如果你跟我一樣不會用IIS7又不想被搞破頭的話,可以參考筆者使用的這個方案。
從程式端下手,我們可以利用C#的ADSI的方式,利用目錄服務存取生成一個站台,
DirectoryEntry Services = new DirectoryEntry("IIS://localhost/W3SVC");
然後再對站台進行相關存取及設定(屬性設定可以參考IIS Metabase)。
參考網站 http://www.codeproject.com/KB/cs/iismanager.aspx
接下來是建立站台的程式碼,記得此方法依舊沒有排除Asp.net那該死的版本問題,所以建立後還是要手動修改設定喔!
/// <summary> 02 /// create a new website
03 /// </summary>
04 /// <param name="name">website name</param>
05 /// <param name="port">website port</param>
06 /// <param name="rootPath">root path</param>
07
/// <returns></returns> 08
public static IISWebsite CreateWebsite(string name, int port, string rootPath, string appPool) 09
{ 10
// validate root path 11
if (System.IO.Directory.Exists(rootPath) == false) 12
{ 13
throw new DirNotFoundException(rootPath); 14
} 15
16
// get directory service 17
DirectoryEntry Services = new DirectoryEntry("IIS://localhost/W3SVC"); 18
19
// get server name (index) 20
int index = 0; 21
foreach (DirectoryEntry server in Services.Children) 22
{ 23
if (server.SchemaClassName == "IIsWebServer") 24
{ 25
if (server.Properties["ServerComment"][0].ToString() == name) 26
{ 27
throw new Exception("website:" + name + " already exsit."); 28
} 29
30
if (Convert.ToInt32(server.Name) > index) 31
{ 32
index = Convert.ToInt32(server.Name); 33
} 34
} 35
} 36
index++; // new index created 37
38
// create website 39
DirectoryEntry Server = Services.Children.Add(index.ToString(), IIsWebServer); 40
Server.Properties["ServerComment"].Clear(); 41
Server.Properties["ServerComment"].Add(name); 42
Server.Properties["Serverbindings"].Clear(); 43
Server.Properties["Serverbindings"].Add(":" + port + ":"); 44
45
// create ROOT for website 46
DirectoryEntry root = Server.Children.Add("ROOT", IISWebVirturalDir.IIsVirtualDir); 47
root.Properties["path"].Clear(); 48
root.Properties["path"].Add(rootPath); 49
50
// create application 51
if (string.IsNullOrEmpty(appPool)) 52
{ 53
root.Invoke("appCreate", 0); 54
} 55
else 56
{ 57
// use application pool 58
root.Invoke("appCreate3", 0, appPool, true); 59
} 60
61
root.Properties["AppFriendlyName"].Clear(); 62
root.Properties["AppIsolated"].Clear(); 63
root.Properties["AccessFlags"].Clear(); 64
root.Properties["FrontPageWeb"].Clear(); 65
root.Properties["AppFriendlyName"].Add(root.Name); 66
root.Properties["AppIsolated"].Add(2); 67
root.Properties["AccessFlags"].Add(513); 68
root.Properties["FrontPageWeb"].Add(1); 69
70
// commit changes 71
root.CommitChanges(); 72
Server.CommitChanges(); 73
74
// return the newly created website 75
IISWebsite website = new IISWebsite(Server); 76
return website; 77
}
如此一來站台變輕鬆建立好了,快速省時!!
如果我寫的不夠仔細,請參考MSDN的Using ADSI to Configure IIS
http://msdn.microsoft.com/en-us/library/ms525389

|

