C#在asp.net项目中以编程方式在IIS中创建站点

时间:2014-09-01 17:40:42

标签: c# asp.net iis iis-7

我正在尝试使用C#中的Microsoft.Web.Administration dll在IIS中创建一个站点。我正在使用以下代码

using (var serverManager = new ServerManager())
        {
            ApplicationPool pool = serverManager.ApplicationPools.Add(ClientDomain);
            pool.ManagedRuntimeVersion = "v4.0";
            Site site = serverManager.Sites.Add(SiteName, httpProtocol, httpBindingInformation, physicalPath);
            site.Bindings.Add(httpsBindingInformation, httpsProtocol);
            serverManager.CommitChanges();
        }

我遇到异常"方法或操作没有实现"当我从asp.net项目运行代码但是如果从控制台项目运行相同的代码,它工作正常。以下是异常的详细信息

   at Microsoft.Web.Administration.Interop.IAppHostProperty.get_Value()
   at Microsoft.Web.Administration.ConfigurationElement.GetPropertyValue(IAppHostProperty property)
   at Microsoft.Web.Administration.ConfigurationElement.GetAttributeValue(String attributeName)
   at Microsoft.Web.Administration.Binding.get_CertificateHash()
   at Microsoft.Web.Administration.BindingCollection.Add(Binding binding)
   at Microsoft.Web.Administration.BindingCollection.Add(String bindingInformation, String bindingProtocol)
   at TestProject.BLL.CRMSiteManagement.CRMSiteIISSetup.Process() in e:\CRMSiteManagement\CRMSiteIISSetup.cs:line 35
   at TestProject.BLL.CRMSiteManagement.CRMSiteService.CreateNewCRMSite(CRMSite newSite) in e:CRMSiteManagement\CRMSiteService.cs:line

似乎是一个权利问题,但我不知道如何解决它。 更新1: 错误发生在localhost上的以下代码行

site.Bindings.Add(httpsBindingInformation, httpsProtocol); 

1 个答案:

答案 0 :(得分:1)

我认为您正在尝试两次设置相同的绑定:

  1. 通过调用Add方法
  2. printf("%s\n", gl->wsk_u->imie);

    1. 然后再次添加相同的绑定:
    2. Site site = serverManager.Sites.Add(SiteName, httpProtocol, httpBindingInformation, physicalPath);