1

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?

OrElse
  • 9,709
  • 39
  • 140
  • 253
  • The binding info is invalid ":80:something". You need to fix that. Try "*:80:something". – Lex Li Jan 24 '16 at 03:25
  • @LexLi Thank you, I did it as requested, but the issue remains – OrElse Jan 24 '16 at 09:57
  • Your code does not pose only one issue, but many. You will have to find a suitable sample to get started. – Lex Li Jan 24 '16 at 09:59
  • I agree with @LexLi . The question posed here has been answered. Tacking on supplemental questions in the comments is not in the spirit of the site, you should ask these separately, and if need be make reference to this question. I'd be happy to assist but certainly not in the comments. – Kev Jan 25 '16 at 13:58

2 Answers2

1

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

Community
  • 1
  • 1
PhillipH
  • 6,182
  • 1
  • 15
  • 25
  • ERROR ( message:The identifier is not supported in the current command usage. You specified "test.example.com". Any thoughts? – OrElse Jan 23 '16 at 22:46
  • Did you get that error when running the appcmd command line application ? In which case I'd think it was complaining about "test.example.com" as your application pool name. I dont know precisely what the naming constraints are on app pools, but I've never seen one with periods in it. Try naming you app pool something more simple as a test, like "myAppPool" instead of "test.example.com" – PhillipH Jan 24 '16 at 17:22
1

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();
Kev
  • 118,037
  • 53
  • 300
  • 385
  • That was really helpful, but i still cannot understand why the Application pool is set to "ApplicationPoolIdentity" – OrElse Jan 24 '16 at 08:26
  • @OrElse `ApplicationPoolIdentity` is the identity that the pool will execute requests under. In the olden days of Windows 2003 this would be `Network Service`. `ApplicationPoolIdentity` is a synthesized identity created on the fly when your pool is running. You'll never see it in the windows user manager. A question about `ApplicationPoolIdentity`'s would be a whole separate thing. Thus far I think my answer provides a solution to your current problem. – Kev Jan 24 '16 at 08:34