2016-01-23 85 views
1

下面有一些可怕的错误,但我无法弄清楚什么。 虽然该网站创建为一种魅力,但应该与其关联的应用程序池根本不会创建。为什么IIS应用程序池完全没有创建?

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; 
      } 
     } 

我在做什么错在这里?

+0

绑定信息无效“:80:东西”。你需要解决这个问题。尝试“*:80:某事”。 –

+0

@LexLi谢谢你,我按要求做了,但问题仍然存在 – OrElse

+0

你的代码并不是只有一个问题,而是很多。你将不得不找到合适的样本开始。 –

回答

1

这里发生的事情是,当你创建你的网站,它会自动被分配到DefaultAppPool

你需要做的是更换您的网站的Application/),并在您刚才创建的应用程序池指向它。

要做到这一点,最简单的方法是首先明确你的新网站的Application集合,然后添加一个新的应用程序指向您的应用程序池。

考虑您的代码段我把它改成如下:

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(); 
+0

这真的很有帮助,但我仍然不明白为什么应用程序池设置为“ApplicationPoolIdentity” – OrElse

+0

@OrElse ApplicationPoolIdentity是池的标识会在Windows 2003下执行请求。在Windows 2003以前,这将是'Network Service'。'ApplicationPoolIdentity'是一个当你的池运行时动态创建的综合身份G。你永远不会在Windows用户管理器中看到它。一个关于'ApplicationPoolIdentity'的问题将是一个完全独立的事情。到目前为止,我认为我的答案为您当前的问题提供了解决方案。 – Kev

1

我不希望应用程序池名称中有标点符号。将域添加为应用程序池名称的一部分有点不同寻常 - 也许这就是源代码。这里讨论基本方法,以及appcmd语法,以使命令行上发生同样的事情 - 尝试在cmd行上创建应用程序池以查看您的参数是否可接受。

Create an application pool that uses .NET 4.0

+0

错误(消息:标识符在当前命令用法中不被支持,您指定了“test.example.com”。是否有任何想法? – OrElse

+0

运行appcmd命令行应用程序时是否出现错误?在这种情况下,我会认为它正在抱怨“test.example.com”作为您的应用程序池名称,我不确切知道应用程序池中的命名约束是什么,但是我从来没有看到过其中有周期的命名约束。作为一个测试,像“myAppPool”而不是“test.example.com” – PhillipH

相关问题