12

I'm trying to deploy an ASP.NET application using Inno Setup.

I need to perform the following tasks:

  1. Create an IIS application.
  2. Create a new IIS application pool and set it's .NET version to 4.
  3. Set the application pool of the new application to the new application pool.

I have found a script to create a virtual directory, but I need an application and application pool:

procedure CreateIISVirtualDir();
var
  IIS, WebSite, WebServer, WebRoot, VDir: Variant;
  ErrorCode: Integer;
begin
  { Create the main IIS COM Automation object }

  try
    IIS := CreateOleObject('IISNamespace');
  except
    RaiseException(
      'Please install Microsoft IIS first.'#13#13'(Error ''' +
      GetExceptionMessage + ''' occurred)');
  end;

  { Connect to the IIS server }

  WebSite := IIS.GetObject('IIsWebService', IISServerName + '/w3svc');
  WebServer := WebSite.GetObject('IIsWebServer', IISServerNumber);
  WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');

  { (Re)create a virtual dir }

  try
    WebRoot.Delete('IIsWebVirtualDir', 'eipwebv4');
    WebRoot.SetInfo();
  except
  end;

  VDir := WebRoot.Create('IIsWebVirtualDir', 'eipwebv4');
  VDir.AccessRead := True;
  VDir.AccessScript := TRUE;
  VDir.AppFriendlyName := 'Easy-IP Web Client';
  VDir.Path := ExpandConstant('{app}');
  try
    VDir.AppPoolId := 'Classic .NET AppPool';
  except
  end;

  VDir.AppCreate(True);
  VDir.SetInfo();
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
norgepaul
  • 6,013
  • 4
  • 43
  • 76

2 Answers2

8

The question was asked a long time ago but maybe somebody will find this script for IIS6/IIS7 useful:

var
  global_AppCmdFilePath :String;
  global_IsIIS7 :Boolean;
  global_WebSites :SiteList;
  global_WebSiteName :String;
  global_vDir :String;
  global_AppCmdExitCode :Integer;

const
  IISServerName = 'localhost';
  IISApplicationPoolName = 'Test Pool';

  ERROR_NOT_FOUND = 1168;
  ERROR_NOT_SUPPORTED = 50;

  MD_APPPOOL_IDENTITY_TYPE_LOCALSYSTEM = 0;
  MD_APPPOOL_IDENTITY_TYPE_LOCALSERVICE = 1;
  MD_APPPOOL_IDENTITY_TYPE_NETWORKSERVICE = 2;
  MD_APPPOOL_IDENTITY_TYPE_SPECIFICUSER = 3;

  MD_LOGON_INTERACTIVE = 0;
  MD_LOGON_BATCH = 1;
  MD_LOGON_NETWORK = 2;
  MD_LOGON_NETWORK_CLEARTEXT = 3;

function ExecAppCmd(params :String) :Boolean;
var
  execSuccessfully :Boolean;
  resultCode :Integer;
begin
  execSuccessfully := Exec('cmd.exe', '/c ' + global_AppCmdFilePath + ' ' + params, '', SW_HIDE, ewWaitUntilTerminated, resultCode);

  global_AppCmdExitCode := resultCode;

  Result := execSuccessfully and (resultCode = 0);
end;


function CreateVirtualDirectoryForIIS6(physicalPath :String) :String;
var
  IIS, webService, webServer, webRoot, vDir, vDirApp :Variant;
  appPools, appPool :Variant;
  webSiteId :String;
begin
  webSiteId := GetWebSiteIdByName(global_WebSiteName);

  { Create the main IIS COM Automation object. }
  IIS := CreateOleObject('IISNamespace');

  { Get application pools. }
  appPools := IIS.GetObject('IIsApplicationPools', 'localhost/W3SVC/AppPools');

  try
    { Check if the application pool already exists. }
    appPool := appPools.GetObject('IIsApplicationPool', IISApplicationPoolName);
  except
    { Crete the application pool. }
    try
      appPool := appPools.Create('IIsApplicationPool', IISApplicationPoolName);

      appPool.LogonMethod := MD_LOGON_NETWORK_CLEARTEXT;
      appPool.AppPoolIdentityType := MD_APPPOOL_IDENTITY_TYPE_NETWORKSERVICE;

      appPool.SetInfo();
    except
      Result := 'Failed to create an apllication pool.';
      Exit;
    end;
  end;

  { Connect to the IIS server. }
  webService := IIS.GetObject('IIsWebService', IISServerName + '/w3svc');

  { Get the website. }
  webServer := webService.GetObject('IIsWebServer', webSiteId);
  webRoot := webServer.GetObject('IIsWebVirtualDir', 'Root');

  { Delete the virtual dir if it already exists. }
  try
    webRoot.Delete('IIsWebVirtualDir', global_vDir);
    webRoot.SetInfo();
  except
    { An exception will be raised if there is not such a website. }
  end;

  { Create the virtual directory. }
  try
    vDir := WebRoot.Create('IIsWebVirtualDir', global_vDir);

    vDir.AccessRead := True;
    vDir.AccessScript := True;
    vDir.AppFriendlyName := 'Test friendly name';
    vDir.Path := physicalPath;

    vDir.AppCreate(False);

    vDir.SetInfo();
  except
    Result := 'Failed to create a virtual directory.';
    Exit;
  end;

  { Assign the application pool to the virtual directory. }
  try
    vDir := webRoot.GetObject('IIsWebVirtualDir', global_vDir);

    vDir.AppPoolId := IISApplicationPoolName;

    vDir.SetInfo();
  except
    Result := 'Failed to assign the application pool to the virtual directory.';
    Exit;
  end;
end;

function CreateVirtualDirectoryForIIS7(physicalPath :String) :String;
var
  tempFileName :String;
  appPoolList :String;
  createAppPool :Boolean;
begin
  { Delete the application if it already exists. }
  if not ExecAppCmd(Format('delete app "%s/%s"', [global_WebSiteName, global_vDir])) then
  begin
    if (global_AppCmdExitCode <> ERROR_NOT_FOUND) and (global_AppCmdExitCode <> ERROR_NOT_SUPPORTED) then
    begin
      Result := 'Failed to delete the application.  ' + GetErrorMessageByCode(global_AppCmdExitCode);
      Exit;
    end;
  end;

  { Check if the application pool already exists. }
  tempFileName := ExpandConstant('{tmp}\AppPoolNames.txt');

  ExecAppCmd(Format('list apppool "%s" > "%s"', [IISApplicationPoolName, tempFileName]));

  if (LoadStringFromFile(tempFileName, appPoolList)) then
  begin
    createAppPool := (Pos(IISApplicationPoolName, appPoolList) = 0);
  end
  else
  begin
    createAppPool := True;
  end;

  { Create the application pool. }
  if (createAppPool) then
  begin
    if not ExecAppCmd(Format('add apppool /name:"%s" /managedRuntimeVersion:v4.0', [IISApplicationPoolName])) then
    begin
      Result := 'Failed to add the application pool. ' + GetErrorMessageByCode(global_AppCmdExitCode);
      Exit;
    end;
  end;

  { Create the application. }
  if not ExecAppCmd(Format('add app /site.name:"%s" /path:"/%s" /physicalPath:"%s" /applicationPool:"%s"', [global_WebSiteName, global_vDir, physicalPath, IISApplicationPoolName])) then
  begin
    Result := 'Failed to add the application. ' + GetErrorMessageByCode(global_AppCmdExitCode);
    Exit;
  end;

  Result := '';
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Teddy Bo
  • 679
  • 8
  • 19
  • I'd first like to thank you for your response, this is exactly what I need to do, but I am having a hard time figuring things out. First, what would I need to send in as my 'physicalPath' when trying to createVirtualDirectoryForIIs7, as an example? – DTI-Matt Aug 06 '12 at 19:17
  • 1
    'physicalPath' is the path on disk where your ASP.NET application is stored. – Teddy Bo Aug 07 '12 at 15:29
  • Does this mean that I will need to include all the files that have to do with my ASP.NET application in the [Files] block in my script? Thank you again for helping me out – DTI-Matt Aug 07 '12 at 19:53
  • 1
    Yes. You should include all your APS.NET application files in section [Files]. Otherwise how are you going to deliver them to a customer machine? – Teddy Bo Aug 09 '12 at 09:48
  • Yes, it makes sense, so let's say I add my site with my top level folder and all sub folders and files like this: ["C:\Users\YOU\Installer\src\SeverSite\*"; DestDir: "{localappdata}\DT\ServerSite"; Attribs: hidden; Flags: ignoreversion recursesubdirs createallsubdirs;] Would I then proceed to call CreateVirtualDirectoryForIIS7 like this? [CreateVirtualDirectoryForIIS7('{localappdata}\DT\ServerSite');] Thank you for all your help – DTI-Matt Aug 09 '12 at 13:30
  • 1
    Yep, you I right. But there is one things to keep in mind. If you use constants like {localappdata} or {app} in [Code] section you should use the method ExpandConstant (like ExpandConstant('{app}')). By the way I'm not sure that it is a good idea to install a site in the local (nonroaming) Application Data folder. – Teddy Bo Aug 10 '12 at 06:57
0

There you go: http://learn.iis.net/page.aspx/285/provisioning-sample-in-c/

Scroll down Create Application Pool. It will show you how to create the AppPool, Application and VirtualDirectories.

Jeroen
  • 4,023
  • 2
  • 24
  • 40