In OOTB SharePoint, you do not have anything which will allow you to create new application pools in IIS and assign them to applications. Also nothing was found in STSADM.
If the application pool does not exist you can create it using the following code snippet:
1: SPFarm farm = SPFarm.Local;
2: SPWebService service = farm.Services.GetValue<SPWebService>();
3: SPApplicationPool appPool = new SPApplicationPool("App Pool Title", service);
4: appPool.CurrentIdentityType = IdentityType.SpecificUser;
5: appPool.Username = "domain\\username";
6: appPool.Password = "password";
7: appPool.Update();
8: appPool.Deploy();
Be aware that application pools you created in IIS directly cannot be used with the SharePoint object model. The reason is that these are not known in the Config DB and they could also be with different configuration on different WFE's in the farm.
So you have to ensure that the application pool has been created using the method above. Once the application pool is created or it already exists you can look it up using the following code snippet and assign to the SharePoint application:
3: SPApplicationPool appPool = service.ApplicationPools["App Pool Title"];
4:
5: SPSite site = new SPSite("http://url-to-your-sitecollection");
6: SPWebApplication webApp = site.WebApplication;
7: webApp.ApplicationPool = appPool;
8: webApp.Update();
9: webApp.ProvisionGlobally();
Hope this helps you to elevate some issues… Happy Coding…
Thank's for this post. Your answer to this problem is great! I tried to understand this strange behavior because it seems to me it is a black hole in the ASP.NET architecture.
When you associate a new Application Pool to an existing SharePoint Web App Through IIS, you get an HTTP 500 error when browsing SharePoint sites on this WebApp. If you activate the Failed Request tracing within IIS, then the exact error message is : Module : SharePoint14Module not found ! Quite strange because the SharePoint14Module MUST be found ... unless it has been loaded in another worker process (the one which is specified in the config and-or content Db). I think this could be a possible reason for this problem. Again thanks !
While investigating why the SharePoint14Module is not loaded, I opened the ApplicationHost.config (IIS configuration file) and then surprise : in the section <globalModules> the section <add name="SharePoint14Module"> references, in the preCondition attribute, every IIS Application Pool where it can be loaded. This section is not modified when you manually associate in IIS a SharePoint Web App with a new Application Pool (keeping the same managed account as the Identity of this Application Pool). I guess that the first line 8 of your post runs the code in the SharePoint API that updates the <globalModules> section in the ApplicationHost.config on each WFE in the SharePoint Farm.