@manhnguyenv

Welcome to my blog!

IIS Create Application and Set Application Pool (C#)

September 12, 2018 11:48

IIS - Create Application and Set Application Pool (C#) (edit)

Chú ý:

+ Mở Visual Studio 2017 với quyền Administrator

+ Cài đặt Web Management Tools

Input

+ Tên Root Application: TSR

+ Tên Application: LightServices

+ Tên Customer: 412687

Output

+ Tạo Application nếu chưa tồn tại.

+ Sử dụng XCopy để copy toàn bộ files từ một nguồn cho trước đến Physical Path của Application vừa tạo.

+ Hoặc Unzip từ một nguồn là 1 file .zip cho trước.

+ Set ApplicationPool trùng với ApplicationPool của Root Application.

Solution

public class IISHelper
{
    private static string AppLightServices = "LightServices";
    private static string AppServices = "Services";
    private static string AppWebadmin = "Webadmin";
    private static string ApplicationPoolName = string.Empty;       // TSR
    private static string AppPath = string.Empty;                   // D:\TSRLight\412687\LightServices\
    private static string VirtualRootPhysicalPath = string.Empty;   // D:\TSRLight\
    private static bool IsLightServicesNotExists = true;            // False

    // Return 0: Not run
// Return 1: Run success
// Return -1: Throw exception public static int CreateApplicationLightServices(string customerCode, string siteName = "TSR", string sourceAppPath = "") { try { using (ServerManager manager = new ServerManager()) { foreach (Site s in manager.Sites) { foreach (Application app in s.Applications) { if (s.Name.ToLower().Equals(siteName.ToLower()) && app.Path.Contains(customerCode) && app.Path.Contains(AppServices)) { ApplicationPoolName = app.ApplicationPoolName; var applicationRoot = s.Applications.Where(a => a.Path == "/").Single(); var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "/").Single(); VirtualRootPhysicalPath = virtualRoot.PhysicalPath; } if (s.Name.ToLower().Equals(siteName.ToLower()) && app.Path.Contains(customerCode) && app.Path.Contains(AppWebadmin)) { ApplicationPoolName = app.ApplicationPoolName; var applicationRoot = s.Applications.Where(a => a.Path == "/").Single(); var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "/").Single(); VirtualRootPhysicalPath = virtualRoot.PhysicalPath; } if (s.Name.ToLower().Equals(siteName.ToLower()) && app.Path.Contains(customerCode) && app.Path.Contains(AppLightServices)) { IsLightServicesNotExists = false; } } } if (IsLightServicesNotExists && !string.IsNullOrWhiteSpace(ApplicationPoolName) && !string.IsNullOrWhiteSpace(VirtualRootPhysicalPath) && Directory.Exists(VirtualRootPhysicalPath)) { AppPath = Path.Combine(VirtualRootPhysicalPath, customerCode, AppLightServices); if (!string.IsNullOrWhiteSpace(AppPath) && !Directory.Exists(AppPath)) { Directory.CreateDirectory(AppPath); if (!string.IsNullOrWhiteSpace(sourceAppPath) && Directory.Exists(sourceAppPath)) { ProcessXcopy(sourceAppPath, AppPath); } else { var filePath = Path.Combine(Directory.GetCurrentDirectory(), "LightServices.zip"); if (File.Exists(filePath)) { ZipFile.ExtractToDirectory(filePath, AppPath); } } Site defaultSite = manager.Sites[siteName]; var app = defaultSite.Applications.Add($"/{customerCode}/{AppLightServices}", AppPath); app.ApplicationPoolName = ApplicationPoolName; manager.CommitChanges(); return 1; } else { return 0; } } else { return 0; } } } catch { return -1; } } /// /// /// private static void ProcessXcopy(string sourceDirectory, string targetDirectory) { // Use ProcessStartInfo class ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; //Give the name as Xcopy startInfo.FileName = "xcopy"; //make the window Hidden startInfo.WindowStyle = ProcessWindowStyle.Hidden; //Send the Source and destination as Arguments to the process startInfo.Arguments = "\"" + sourceDirectory + "\"" + " " + "\"" + targetDirectory + "\"" + @" /e /y /I"; try { // Start the process with the info we specified. // Call WaitForExit and then the using statement will close. using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); } } catch (Exception exp) { throw exp; } } }

Tham khảo

Sử dụng thư viện Microsoft.Web.Administration.dll

Sử dụng thư viện System.IO.Compression.dllSystem.IO.Compression.FileSystem.dll

1) https://forums.iis.net/t/1207235.aspx?How+to+convert+a+physical+folder+into+an+Asp+NET+Application+using+Microsoft+Web+Administration+C+API

2) https://stackoverflow.com/questions/3228813/how-to-get-websites-physical-path-on-local-iis-server-from-a-desktop-app

3) http://www.anotherchris.net/csharp/6-ways-to-get-the-current-directory-in-csharp/

4) https://dailydotnettips.com/different-ways-of-getting-path/

5) https://www.c-sharpcorner.com/UploadFile/jawedmd/xcopy-using-C-Sharp-to-copy-filesfolders/

6) https://stackoverflow.com/questions/32236796/fastest-way-to-copy-files-from-one-directory-to-another (có sử dụng dữ liệu trong Database và Sql Server Connection)

https://www.c-sharpcorner.com/UploadFile/satisharveti/programmatically-create-iis-website-and-application-pool-usi/

https://stackoverflow.com/questions/16417578/iis-application-creation-by-using-microsoft-web-administration-library-creates-t

https://www.c-sharpcorner.com/UploadFile/225740/introduction-to-application-pool-in-iis/

https://stackoverflow.com/questions/12147721/change-apppool-of-application-programatically

Cấu hình IIS cho ứng dụng ASP.NET

June 28, 2017 22:01

Bước 01: Vào Start / Control Panel

Bước 02: Chọn Program and Features

Bước 03: Chọn Turn Windows features on or off , Chọn IIS (Internet Information Services) và chờ trong giây lát để Windows cài đặt dịch vụ

Bước 04: Đăng ký asp.net vào hệ thống với mã lệnh “aspnet_regiis –i” với quyền Administrator



Bước 05: Mở IIS bằng cách vào Start / Control Panel / Administrative Tools / chọn dịch vụ Internet Information Services (IIS) Manager

Bước 06: Tạo Application Pools phù hợp với .NET Framework v4.0.30319

Bước 07: Tạo Application để trỏ tới website ASPNET. Lưu ý: Website đã được Publish thành tập tin .DLL



Bước 08: Chạy website từ trình duyệt với 2 cách: thông qua IIS, thông qua trình duyệt Friefox, Google Chrome, …

Chúc bạn học tập tốt.

Categories

Recent posts