OrElse
OrElse

Reputation: 9969

Why the IIS application pool is not created at all?

There is something terribly wrong below but i just cannot figure out what. Although the website is created like a charm, the Application pool that should be associated with it, is not created at all.

public string Create(string sitename)
        {
            try
            {
                using (ServerManager serverMgr = new ServerManager())
                {
                    string strhostname = sitename + "." + domain;
                    string bindinginfo = ":80:" + strhostname;

                    if (!IsWebsiteExists(serverMgr.Sites, strhostname))
                    {
                        Site mySite = serverMgr.Sites.Add(strhostname, "http", bindinginfo, "C:\\admin\\" + domain);

                        ApplicationPool newPool = serverMgr.ApplicationPools.Add(strhostname);
                        newPool.ManagedRuntimeVersion = "v4.0";
                        newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;

                        serverMgr.CommitChanges();
                        return "Website  " + strhostname + " added sucessfully";
                    }

                    else
                    {
                        return "Name should be unique, " + strhostname + " already exists.";
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

What am i doing wrong here?

Upvotes: 1

Views: 1645

Answers (2)

Kev
Kev

Reputation: 119846

What's happening here is that when you create your site it automatically gets assigned to the DefaultAppPool.

What you need to do is replace your site's root Application (/) and point it at the application pool you just created.

The easiest way to do this is to first clear your new site's Application collection, then add a new root application that points to your application pool.

Taking your code snippet I changed it to the following:

Site mySite = serverMgr.Sites.Add(strhostname, "http", bindinginfo, "C:\\admin\\" + domain);

// Clear Applications collection
mySite.Applications.Clear();

ApplicationPool newPool = serverMgr.ApplicationPools.Add(strhostname);
newPool.ManagedRuntimeVersion = "v4.0";
newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;

// Create new root app and specify new application pool
Application app = mySite.Applications.Add("/", "C:\\admin\\" + domain);
app.ApplicationPoolName = strhostname;

serverMgr.CommitChanges();

Upvotes: 1

PhillipH
PhillipH

Reputation: 6222

I wouldnt expect the App Pool name to have punctuation in it. Adding the domain as part of the app pool name is a little unusual - perhaps thats the source. The basic method is discussed here, along with the appcmd syntax to make the same thing happen on the command line - try creating your app pool on the cmd line to see if your parameters are acceptable.

Create an application pool that uses .NET 4.0

Upvotes: 1

Related Questions